Welcome › Forums › General PowerShell Q&A › How to put all result in 1 table
- This topic has 3 replies, 2 voices, and was last updated 1 week, 6 days ago by
Participant.
-
AuthorPosts
-
-
January 12, 2021 at 10:39 am #285460
Hi.
I am trying to put below result into 1 table.PowerShell12345678Server test.txt------ --------ServerA ExistServer test.txt------ --------ServerB Not ExistBelow is the Code
PowerShell1234567891011121314151617181920212223242526$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent$Servers = Get-Content -Path C:\Users\suhail_asrulsani-ops\Desktop\PS\serverlist.txt$dt = (Get-Date).ToString("ddMMyyyy_HHmmss")$MultiSession = New-PSSession -ComputerName $Servers$MyCommands ={$Server = hostname$Path = "C:\test.txt"If (Test-Path -Path $Path){$Status = "Exist"}Else{$Status = "Not Exist"}$ourObject = New-Object -TypeName PsObject$ourObject | Add-Member -MemberType NoteProperty -Name Server -Value $Server$ourObject | Add-Member -MemberType NoteProperty -Name test.txt -Value $Status$ourObject | Select-Object Server, test.txt | Format-Table -AutoSize}Invoke-Command -Session $MultiSession -ScriptBlock $MyCommands -
January 12, 2021 at 11:16 am #285469
Format-Table is what is really breaking things. There is much easier ways to be build an object, take a look at this example:
PowerShell1234567891011121314151617$Servers = Get-Content -Path C:\Users\suhail_asrulsani-ops\Desktop\PS\serverlist.txt$MultiSession = New-PSSession -ComputerName $Servers$MyCommands ={$Path = "C:\test.txt"[PSCustomObject]@{Date = (Get-Date).ToString("ddMMyyyy_HHmmss")Server = $env:COMPUTERNAMEPath = $PathExists = Test-Path -Path $Path}}$results = Invoke-Command -Session $MultiSession -ScriptBlock $MyCommands$results | Format-Table-
January 12, 2021 at 4:43 pm #285547
Thanks Rob That works.
One last question,How can I catch the error for this line of code. In case of the one of the server is offline.
I would like to catch the error for that offline server while continuing the script for the rest of the server in the list.PowerShell1$MultiSession = New-PSSession -ComputerName $Servers
-
-
January 13, 2021 at 10:52 am #285640
In order to catch errors for each server, you need to process them in a for loop rather than sending them as a batch. When you send a batch as a string array to a cmdlet, that cmdlet is processing them foreach($session in $ComputerName) {blah}, so you move it into your own loop to capture each failure. Recommend using a similar PSObject schema you can filter and find the failures and ensure that -ErrorAction stop is specified.
PowerShell12345678910111213141516171819202122232425262728293031323334$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent$Servers = Get-Content -Path C:\Users\suhail_asrulsani-ops\Desktop\PS\serverlist.txt$results = foreach ($server in $servers) {try {$session = New-PSSession -ComputerName $Server -ErrorAction Stop$MyCommands ={$Path = "C:\test.txt"[PSCustomObject]@{Date = (Get-Date).ToString("ddMMyyyy_HHmmss")Server = $env:COMPUTERNAMEPath = $PathExists = Test-Path -Path $PathStatus = 'Success'}}Invoke-Command -Session $session -ScriptBlock $MyCommands -ErrorAction Stop}catch {[PSCustomObject]@{Date = (Get-Date).ToString("ddMMyyyy_HHmmss")Server = $serverPath = $nullExists = $nullStatus = 'Fail: {0}' -f $_}}}$results
-
-
AuthorPosts
- You must be logged in to reply to this topic.