0
votes

So I have this script working except I cannot get the computer name to display when there is an error, I understand that is because the computer is offline and the first Get computername cmd is an error as well, but I have tried multiple ways to perform this function with one thing or another not working.

gc "\\server\s$\powershellscripts\LabMachines\*.txt" | ForEach-Object{
    Try{   
        $wdt = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ -Ea Stop
        $cdt = Get-Content "\\$_\C$\file\*.txt" -Ea Stop
    }
    Catch{
        $wdt= $_
        $cdt = 'Offline or NoFile'
    }
    $wdt | Select @{n='WorkStation';e={$_.PSComputerName}},@{n='Version';e={$cdt}}
} | ogv -Title 'Versions'
$host.enternestedprompt()

In a different attempt I have used this script, but this one below formats the information in rows instead of column like the first one.

Get-Content "\\server\s$\powershellscripts\LabMachines\*.txt" | ForEach-Object  {   
    Try{ 
        @{ComputerName=$_}
        @{Version = Get-Content "\\$_\C$\file\*.txt" -Ea Stop}
    }
    Catch{
        @{Version = 'Offline or NoFile' }
    }
} #| out-file \\localhost\c$\users\public\Desktop\version.csv 

Any attempt to take my script and merge it with one someone suggested to me "the second one" breaks it completely. To reiterate the top script give me the format output in columns I want but the bottom script gives the catch error "ComputerName Error" I need but has the output in harder to read row outfile.

Any suggestions? Thanks.

2
Which version of PowerShell are you using (check $PSVersionTable)?Mathias R. Jessen

2 Answers

1
votes

Inside the catch block, $_ no longer refers to the pipeline item, but to the error that was caught.

Assign $_ to a different variable and you can reuse it inside the catch block:

Get-Content "\\server\s$\powershellscripts\LabMachines\*.txt" | ForEach-Object{
    Try{   
        $ComputerName = $_
        $wdt = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName -Ea Stop
        $cdt = Get-Content "\\$_\C$\file\*.txt" -Ea Stop
    }
    Catch{
        $wdt = @{PSComputerName = $ComputerName}
        $cdt = 'Offline or NoFile'
    }
    $wdt | Select @{n='WorkStation';e={$_.PSComputerName}},@{n='Version';e={$cdt}}
}

Pipe to Export-Csv at the end if you want to output it to a CSV file:

} |Export-Csv C:\users\public\Desktop\version.csv -NoTypeInformation
0
votes

I so I wanted to throw my finished product with a few additional things I learned. Here it is.

Function Version {
$i=1
$LabMachines = "\\server\s$\powershellscripts\LabMachines\*.txt" 
$Total = 107 #a manual number for now

Get-Content $LabMachines | ForEach-Object{
   Try{ 
      $ComputerName=$_
      $wdt = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName -Ea Stop 
      $cdt = Get-Content "\\$_\C$\file\*.txt" -Ea Stop 
      }
     Catch{
          $wdt = @{PSComputerName = $ComputerName}
          $cdt = 'Offline'
          }
 $wdt | Select @{n='WorkStation';e={$_.PSComputerName}},@{n='Version';e={if($cdt){$cdt}else{"Error with Version.txt"}}} 
                $i++ 
                $percent = [Math]::Round($i / $Total*100) } | Tee-Object -file \\localhost\c$\users\public\desktop\versions.csv | 
                 Select @{n='%Complete';e={if(!($null)){$percent}else{"NULL"}}},
                 @{n='Number';e={if(!($null)){$i}else{"NULL"}}},
                 @{n='Computer';e={if(!($null)){$ComputerName}else{"NULL"}}},
                 @{n='Version';e={if($cdt){$cdt}else{"Error with Version.txt"}}} }

The changes I made in addition to the answer above was it will show progress in the console as a percent, number, computername, version txt as it completes. Also I made this into a function so we can run it and get the .csv file on our own desktop. To be fancier you could put in a line to do a line count to achieve the manual number I am using.

Thanks so much for the help I learned alot.