Script: Export-Uptime

by 1 user

91 Downloads, 211 Views

Download Embed code

By: Jesper Strandberg 15 Apr 2012 03:19 PM UTC in the category: Advanced Event 6

Description:

Collects information about uptime and last reboot date from one or more computers and exports result to a csv-file located in logged on users "Documents" special folder. If this file already exists (the command has been run earlier the same day) it will append the new information to the same file.
<#
.SYNOPSIS
    Collects uptime information and exports to csv-file.
.DESCRIPTION
    Collects information about uptime and last reboot date from one or more computers and exports result to a csv-file located in logged on users "Documents" special folder. If this file already exists (the command has been run earlier the same day) it will append the new information to the same file.
.PARAMETER ComputerName
    Gets information from specified computers. Type the name of one or more computers in a comma-separated list. Default is localhost.
.PARAMETER Credential
    Specifies credentials that has permission to perform WMI-queries on remote machines. Use the object returned by the Get-Credential cmdlet as argument. For local access impersonation level 3 is always used (see Get-WMIObject). Default is current user.
.LINK
    2012 Scripting Games:
    https://2012sg.poshcode.org
.NOTES
    Author : Jesper Strandberg
    Date   : 2012-04-15
#>
function Export-Uptime {
    [CmdletBinding()]
    param (
        [parameter(ValueFromPipeline = $true)]
        [string[]]$ComputerName = $Env:COMPUTERNAME,
        
        [Management.Automation.PSCredential]
        $Credential
    )
    
    begin {
        $LocalMachineAliases = $Env:COMPUTERNAME,'localhost','127.0.0.1','::1','.'
        $Result = @()
    }
    
    process {
        foreach ($Computer in $ComputerName) {
            Write-Verbose "Building parameters for $Computer"
            $WmiParam = @{ Class = "Win32_OperatingSystem";
                           Property = "LastBootUpTime";
                           ComputerName = $Computer }
            
            if (-not ($LocalMachineAliases -contains $Computer) -and $Credential) {
                Write-Verbose "Adding credentials for $Computer"
                $WmiParam.Add("Credential", $Credential)
            }
            
            Write-Verbose "Accessing $Computer"
            try { $OS = Get-WmiObject @WmiParam -ErrorAction Stop }
            catch { Write-Warning $_; continue }
            
            Write-Verbose "Calculating uptime"
            $BootUpTime = $OS.ConvertToDateTime($OS.LastBootUpTime)
            $Uptime = New-TimeSpan -Start $BootUpTime -End "8 am"
            if ($Uptime -lt 0) { $Uptime = New-TimeSpan }
            
            Write-Verbose "Building custom object"
            $Properties = @{ ComputerName = $Computer;
                             Days = $Uptime.Days;
                             Hours = $Uptime.Hours;
                             Minutes = $Uptime.Minutes;
                             Seconds = $Uptime.Seconds;
                             Date = (Get-Date -Format d -Date $BootUpTime) }
            $Result += New-Object PSCustomObject -Property $Properties
        } # foreach
    } # process
    
    end {
        Write-Verbose "Exporting results to CSV"
        $CSV = $Result | Select-Object -Property ComputerName,Days,Hours,Minutes,Seconds,Date |
               ConvertTo-Csv -NoTypeInformation -Delimiter ';'
        
        $OutFile = "$([System.Environment]::GetFolderPath('Personal'))\$(Get-Date -UFormat %Y%m%d)_Uptime.csv"
        try {
            if (-not (Test-Path $OutFile)) {
                Write-Verbose "Creating $OutFile"
                $CSV | Out-File $OutFile -Encoding ASCII -ErrorAction Stop
            } else {
                Write-Verbose "Appending to $OutFile"
                $CSV | Select-Object -Skip 1 | Out-File $OutFile -Encoding ASCII -Append -ErrorAction Stop
            }
        } # try
        catch { Write-Warning $_ }
    } # end
} # function
Top

Comments: