0

I want to be able to remotely fetch information about the Windows VM uptime in specific period of time. This is the script I found:

function Get-SystemUpTime {
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory = $false, ValueFromPipeline = $true)]
        [Alias("CN")]
        [String[]]$ComputerName = $Env:ComputerName,

        [Parameter(Position = 1, Mandatory = $false)]
        [Alias("RunAs")]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Process {
        foreach ($Name in $ComputerName) {
            Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_System -ComputerName $Name -Credential $Credential |
                Select-Object @{Name="ComputerName";Expression={$_.__SERVER}},
                    @{Name="SystemUpTime";Expression={New-TimeSpan -Seconds $_.SystemUpTime}}
        }
    }
}

Get-SystemUpTime localcomputer | Sort-Object SystemUpTime

This is the local script I wrote:

function Get-Uptime {
     $os = Get-WmiObject Win32_OperatingSystem
     $uptime = (Get-Date) - ($os.ConvertToDateTime($os.LastBootupTime))
     $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours +
                " hours, " + $Uptime.Minutes + " minutes" 
     Write-Output $Display
     Write-Output $uptime.Hours
}

Get-Uptime 

How can I modify script to fetch this information only for specific month or time period, that I can easily define in script?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
lubierzca
  • 160
  • 2
  • 14
  • 1
    the CIM/WMI object does not keep that info. you would likely need to read the event log for startup/shutdown events and add things up your self. either that, or build a database of the uptime and then calc when it changes. – Lee_Dailey Apr 24 '19 at 11:32
  • you are most welcome! glad to kinda-sorta help ... [*grin*] – Lee_Dailey Apr 24 '19 at 13:13

0 Answers0