Hello gurus! I need to retrieve the uptime from a list of servers and output it to a csv. I have the basic script working as follows:

$outputcsv = "C:\PowerShell\uptime.csv"
$servers = "mwdvwpctxdms01", "mwdvwpdbs01"
$currentdate = Get-Date
$OutputMessage = @()

foreach($server in $servers){

    $Bootuptime = (Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $server).LastBootUpTime
   $uptime = $currentdate - $Bootuptime
   if ($uptime) {
        $OutputMessage += "$server :, $($uptime.Days) Days"
    }
   else {
   $OutputMessage +=  "$server, Offline"
   }
   
 }
 $OutputMessage |   Out-File $outputcsv -Encoding utf8 -Append

So this works perfect, except for some reason I am not sure of, I do not needs to supply credentials for the two servers listed. When I run this against a larger list of servers e.g., $servers = Get-Content C:\serverlist.txt, I run into permission issues. I have really been struggling on getting this to work with a remote session and credentials. Here is my latest effort:

$outputcsv = "C:\PowerShell\uptime.csv"
$servers = Get-Content C:\Powershell\hostlist.txt
$currentdate = Get-Date
$OutputMessage = @()
$creds = Get-Credential

foreach($server in $servers){
    Invoke-Command -ComputerName $server -ScriptBlock {

    $Bootuptime = (Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $server).LastBootUpTime
   $uptime = $currentdate - $Bootuptime
   if ($uptime) {
        $OutputMessage += "$server :, $($uptime.Days) Days"
    }
   else {
   $OutputMessage +=  "$server, Offline"
   }
   
 } -Credential  $creds }
 $OutputMessage |   Out-File $outputcsv -Encoding utf8 -Append 

In this particular code it is now telling me that -ComputerName after Invoke-Command is null. But I have verified that $servers holds the list of servers.

This is just one example of what I have tried. I am not sure if this is even going down the right path. Any pointers to a solution will be much appreciated. I have put hours into this and now need a break!

Thank you.

6 Spice ups

Sorry folks, I see the formatting in my second script block is lost. Could not figure out how to fix it. Here is is:

$outputcsv = "C:\PowerShell\uptime.csv"
$servers = Get-Content C:\Powershell\hostlist.txt
$currentdate = Get-Date
$OutputMessage = @()
$creds = Get-Credential

foreach($server in $servers){
    Invoke-Command -ComputerName $server -ScriptBlock {

    $Bootuptime = (Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $server).LastBootUpTime
   $uptime = $currentdate - $Bootuptime
   if ($uptime) {
        $OutputMessage += "$server :, $($uptime.Days) Days"
    }
   else {
   $OutputMessage +=  "$server, Offline"
   }
   
 } -Credential  $creds }
 $OutputMessage |   Out-File $outputcsv -Encoding utf8 -Append 

So I just discovered I can run this successfully without credentials, and I did on 200 servers. I need to have a better understanding of remote connections because I really thought credentials were necessary for this Cim query. I guess this no longer needs an answer.

1 Spice up