#requires -version 2.0 #define the parameters param ( [Parameter( ValueFromPipeline=$True, Position=0, Mandatory=$True, HelpMessage="Enter the name of a text file with servernames to query.")] [String[]]$File ) try { #verify file exists $servers=Get-Content $File -ea stop Write-Host "Processing computernames in $file" -foregroundcolor green foreach ($server in $servers) { try { $wmi=Get-WmiObject win32_operatingsystem -computername $server -ea Stop $wmi | select @{name="Computer";Expression={$_.CSName}},` @{name="LastBoot";Expression={$_.ConvertToDateTime($_.lastbootuptime)}},` @{name="Uptime";Expression={((get-date) - ($_.ConvertToDateTime($_.lastbootuptime)))}} }# end inner Try catch { Write-Warning "Failed to connect to $server" } } #end foreach } #end outer try catch { Write-Warning "Failed to find or open $file" } finally { Write-Host "exiting the script" -foregroundcolor green }