Fellow coders, I received some great feedback from our honorary scripting guru’s Mike F. Robbins and Richard Siddaway about my little function (thanks guys! I’ll buy you a drink when we meet! 😉 ).
With their feedback I managed to improve my simple function. And that’s so great about being part of a community; people are always willing to help.
#requires -Version 2 Function Get-ComputerDetail { [cmdletbinding()] param ( [ValidateNotNullOrEmpty()] [string[]]$ComputerName = $env:COMPUTERNAME ) foreach ($node in $ComputerName) { try { $computerSystem = Get-WmiObject -Class 'Win32_ComputerSystem' -ComputerName $node -ErrorAction Stop $computerBios = Get-WmiObject -Class 'Win32_Bios' -ComputerName $node -ErrorAction Stop [pscustomobject][ordered]@{ Name = $computerSystem.Name Manufacturer = $computerSystem.Manufacturer Model = $computerSystem.Model Serial = $computerBios.SerialNumber } # End of custom object } # End of try catch { Write-Error -Message "The command failed for computer $node. Message: $_.Exception.Message" break }# End of Catch }# End of foreach }# End of function
One more suggested mod. This will process everything in the list, and then print out all the errors at the end of the run.
… not to mention, completely flatten the code. 🙂
Nice one .. Thanks for sharing