Uptime (LastBootTime): Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
 
(3 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt)
Zeile 7: Zeile 7:
 
oder
 
oder
 
<source lang="powershell"> Invoke-Command -ComputerName <Hostname> -Credential (Get-Credential) -ScriptBlock {Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime}</source>
 
<source lang="powershell"> Invoke-Command -ComputerName <Hostname> -Credential (Get-Credential) -ScriptBlock {Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime}</source>
 +
oder
 +
<source lang="powershell">
 +
$LastBootTime = (Get-CimInstance -ClassName win32_operatingsystem -CimSession <Hostname>).lastbootuptime
 +
New-TimeSpan -Start $LastBootTime -End (Get-Date) | ft
 +
</source>
  
  
  
 
=== Uptime Funktion ===
 
=== Uptime Funktion ===
 +
<span style="font-size:20px;"><span style="color:red">'''-> This function/script is moved to [https://github.com/R-Studio/PSTools GitHub]!'''</span></span><br>
 +
 +
 
<source lang="powershell">
 
<source lang="powershell">
 
<#
 
<#
 
.SYNOPSIS
 
.SYNOPSIS
 
Outputs the last bootup time and uptime for one or more computers.
 
Outputs the last bootup time and uptime for one or more computers.
 
+
 
.DESCRIPTION
 
.DESCRIPTION
 
Outputs the last bootup time and uptime for one or more computers.
 
Outputs the last bootup time and uptime for one or more computers.
 
+
 
.PARAMETER ComputerName
 
.PARAMETER ComputerName
 
One or more computer names. The default is the current computer. Wildcards are not supported.
 
One or more computer names. The default is the current computer. Wildcards are not supported.
 
+
 
.PARAMETER Credential
 
.PARAMETER Credential
 
Specifies credentials that have permission to connect to the remote computer. This parameter is ignored for the current computer.
 
Specifies credentials that have permission to connect to the remote computer. This parameter is ignored for the current computer.
 
+
 
.EXAMPLE
 
.EXAMPLE
 
Get-Uptime -ComputerName <Hostname>
 
Get-Uptime -ComputerName <Hostname>
 
+
 
.OUTPUTS
 
.OUTPUTS
 
PSObjects containing the computer name, the last bootup time, and the uptime.
 
PSObjects containing the computer name, the last bootup time, and the uptime.
 
#>
 
#>
 
+
 
+
Function Get-Uptime {
[CmdletBinding()]
+
    [CmdletBinding()]
param(
+
  [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
+
    $ComputerName,
+
  [System.Management.Automation.PSCredential]
+
    $Credential
+
)
+
 
+
begin {
+
  function Out-Object {
+
 
     param(
 
     param(
       [System.Collections.Hashtable[]] $hashData
+
      [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
 +
        $ComputerName,
 +
       [System.Management.Automation.PSCredential]
 +
        $Credential
 
     )
 
     )
     $order = @()
+
    $result = @{}
+
     begin {
    $hashData | ForEach-Object {
+
      function Out-Object {
      $order += ($_.Keys -as [Array])[0]
+
        param(
      $result += $_
+
          [System.Collections.Hashtable[]] $hashData
 +
        )
 +
        $order = @()
 +
        $result = @{}
 +
        $hashData | ForEach-Object {
 +
          $order += ($_.Keys -as [Array])[0]
 +
          $result += $_
 +
        }
 +
        New-Object PSObject -Property $result | Select-Object $order
 +
      }
 +
 +
      function Format-TimeSpan {
 +
        process {
 +
          "{0:00}d {1:00}h {2:00}m {3:00}s" -f $_.Days,$_.Hours,$_.Minutes,$_.Seconds
 +
        }
 +
      }
 +
 +
      function Get-Uptime {
 +
        param(
 +
          $ComputerName,
 +
          $Credential
 +
        )
 +
        # In case pipeline input contains ComputerName property
 +
        if ( $computerName.ComputerName ) {
 +
          $computerName = $computerName.ComputerName
 +
        }
 +
        if ( (-not $computerName) -or ($computerName -eq ".") ) {
 +
          $computerName = [Net.Dns]::GetHostName()
 +
        }
 +
        $params = @{
 +
          "Class" = "Win32_OperatingSystem"
 +
          "ComputerName" = $computerName
 +
          "Namespace" = "root\CIMV2"
 +
        }
 +
        if ( $credential ) {
 +
          # Ignore -Credential for current computer
 +
          if ( $computerName -ne [Net.Dns]::GetHostName() ) {
 +
            $params.Add("Credential", $credential)
 +
          }
 +
        }
 +
        try {
 +
          $wmiOS = Get-WmiObject @params -ErrorAction Stop
 +
        }
 +
        catch {
 +
          Write-Error -Exception (New-Object $_.Exception.GetType().FullName `
 +
            ("Cannot connect to the computer '$computerName' due to the following error: '$($_.Exception.Message)'",
 +
            $_.Exception))
 +
          return
 +
        }
 +
        $lastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($wmiOS.LastBootUpTime)
 +
        Out-Object `
 +
          @{"ComputerName" = $computerName},
 +
          @{"LastBootTime" = $lastBootTime},
 +
          @{"Uptime"      = (Get-Date) - $lastBootTime | Format-TimeSpan}
 +
      }
 
     }
 
     }
    New-Object PSObject -Property $result | Select-Object $order
+
  }
+
 
+
  function Format-TimeSpan {
+
 
     process {
 
     process {
       "{0:00} d {1:00} h {2:00} m {3:00} s" -f $_.Days,$_.Hours,$_.Minutes,$_.Seconds
+
       if ( $ComputerName ) {
    }
+
        foreach ( $computerNameItem in $ComputerName ) {
  }
+
          Get-Uptime $computerNameItem $Credential
 
+
        }
  function Get-Uptime {
+
       }
    param(
+
       else {
      $ComputerName,
+
         Get-Uptime "."
      $Credential
+
    )
+
    # In case pipeline input contains ComputerName property
+
    if ( $computerName.ComputerName ) {
+
      $computerName = $computerName.ComputerName
+
    }
+
    if ( (-not $computerName) -or ($computerName -eq ".") ) {
+
      $computerName = [Net.Dns]::GetHostName()
+
    }
+
    $params = @{
+
       "Class" = "Win32_OperatingSystem"
+
      "ComputerName" = $computerName
+
      "Namespace" = "root\CIMV2"
+
    }
+
    if ( $credential ) {
+
       # Ignore -Credential for current computer
+
      if ( $computerName -ne [Net.Dns]::GetHostName() ) {
+
         $params.Add("Credential", $credential)
+
 
       }
 
       }
 
     }
 
     }
    try {
 
      $wmiOS = Get-WmiObject @params -ErrorAction Stop
 
    }
 
    catch {
 
      Write-Error -Exception (New-Object $_.Exception.GetType().FullName `
 
        ("Cannot connect to the computer '$computerName' due to the following error: '$($_.Exception.Message)'",
 
        $_.Exception))
 
      return
 
    }
 
    $lastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($wmiOS.LastBootUpTime)
 
    Out-Object `
 
      @{"ComputerName" = $computerName},
 
      @{"LastBootTime" = $lastBootTime},
 
      @{"Uptime"      = (Get-Date) - $lastBootTime | Format-TimeSpan}
 
  }
 
}
 
 
process {
 
  if ( $ComputerName ) {
 
    foreach ( $computerNameItem in $ComputerName ) {
 
      Get-Uptime $computerNameItem $Credential
 
    }
 
  }
 
  else {
 
    Get-Uptime "."
 
  }
 
 
}
 
}
 
</source>
 
</source>

Aktuelle Version vom 26. Februar 2020, 10:59 Uhr

Startzeit eines Systems anzeigen

Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime


Startzeit eines Remote System anzeigen

 Get-CimInstance -ClassName win32_operatingsystem -CimSession <Hostname> | select csname, lastbootuptime

oder

 Invoke-Command -ComputerName <Hostname> -Credential (Get-Credential) -ScriptBlock {Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime}

oder

$LastBootTime = (Get-CimInstance -ClassName win32_operatingsystem -CimSession <Hostname>).lastbootuptime
New-TimeSpan -Start $LastBootTime -End (Get-Date) | ft


Uptime Funktion

-> This function/script is moved to GitHub!


<#
.SYNOPSIS
Outputs the last bootup time and uptime for one or more computers.
 
.DESCRIPTION
Outputs the last bootup time and uptime for one or more computers.
 
.PARAMETER ComputerName
One or more computer names. The default is the current computer. Wildcards are not supported.
 
.PARAMETER Credential
Specifies credentials that have permission to connect to the remote computer. This parameter is ignored for the current computer.
 
.EXAMPLE
Get-Uptime -ComputerName <Hostname>
 
.OUTPUTS
PSObjects containing the computer name, the last bootup time, and the uptime.
#>
 
Function Get-Uptime {
    [CmdletBinding()]
    param(
      [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        $ComputerName,
      [System.Management.Automation.PSCredential]
        $Credential
    )
 
    begin {
      function Out-Object {
        param(
          [System.Collections.Hashtable[]] $hashData
        )
        $order = @()
        $result = @{}
        $hashData | ForEach-Object {
          $order += ($_.Keys -as [Array])[0]
          $result += $_
        }
        New-Object PSObject -Property $result | Select-Object $order
      }
 
      function Format-TimeSpan {
        process {
          "{0:00}d {1:00}h {2:00}m {3:00}s" -f $_.Days,$_.Hours,$_.Minutes,$_.Seconds
        }
      }
 
      function Get-Uptime {
        param(
          $ComputerName,
          $Credential
        )
        # In case pipeline input contains ComputerName property
        if ( $computerName.ComputerName ) {
          $computerName = $computerName.ComputerName
        }
        if ( (-not $computerName) -or ($computerName -eq ".") ) {
          $computerName = [Net.Dns]::GetHostName()
        }
        $params = @{
          "Class" = "Win32_OperatingSystem"
          "ComputerName" = $computerName
          "Namespace" = "root\CIMV2"
        }
        if ( $credential ) {
          # Ignore -Credential for current computer
          if ( $computerName -ne [Net.Dns]::GetHostName() ) {
            $params.Add("Credential", $credential)
          }
        }
        try {
          $wmiOS = Get-WmiObject @params -ErrorAction Stop
        }
        catch {
          Write-Error -Exception (New-Object $_.Exception.GetType().FullName `
            ("Cannot connect to the computer '$computerName' due to the following error: '$($_.Exception.Message)'",
            $_.Exception))
          return
        }
        $lastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($wmiOS.LastBootUpTime)
        Out-Object `
          @{"ComputerName" = $computerName},
          @{"LastBootTime" = $lastBootTime},
          @{"Uptime"       = (Get-Date) - $lastBootTime | Format-TimeSpan}
      }
    }
 
    process {
      if ( $ComputerName ) {
        foreach ( $computerNameItem in $ComputerName ) {
          Get-Uptime $computerNameItem $Credential
        }
      }
      else {
        Get-Uptime "."
      }
    }
}