Author Posts

May 1, 2017 at 4:24 am

Hi Guys,

I have a really simple script to get my serial numbers for my HP servers and it works with no problem and I have some error handling built into that as well. But when the scripts finishes I am getting an additional error which I cannot account for and it reads something like this.... "Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\powershell\Scripts\SerialNumber\serialnumber.ps1:4 char:40
+ Get-WmiObject win32_bios -ComputerName $server -ErrorAction SilentlyC ..."

My script is below:

$servers = Get-Content C:\powershell\Scripts\SerialNumber\servers.txt
foreach ($server in $servers){
Get-WmiObject win32_bios -ComputerName $server -ErrorAction SilentlyContinue -ErrorVariable ProcessError | Select-Object pscomputername,serialnumber | Sort-Object PSComputername 
if($ProcessError) {
Write-Warning -Message "Something went wrong, with the server $server!!"
}
}

As you can see it's a simple enough script but looks like its having issues reading from my text file? At the end I do get what i require. I can get all the serial numbers that I need, except for a couple of machines.

-A

May 1, 2017 at 1:34 pm

My first guess would be that you have a blank line at the end of your text file.
you can always add the "$error" variable into your warning message to see what the actual error is.

May 1, 2017 at 1:40 pm

There is a free e-book on Powershell Error Handling in the eBooks menu above. This is an example of proper error handling:

$servers = Get-Content C:\powershell\Scripts\SerialNumber\servers.txt

$results = foreach ($server in $servers){
    try {
        Get-WmiObject -Class Win32_BIOS -ComputerName $server -ErrorAction Stop | 
        Select-Object -Property pscomputername,serialnumber
    }
    catch {
        $msg = "Error occured on server {0}!! {1}" -f $server, $_
        Write-Warning -Message $msg
    }
}

$results | 
Sort-Object -Property PSComputername 

As for the error message, an educated guess is that there is a blank line in the text file which is passed to Get-WMIObject as a NULL computername. The error handling above will account for it, or you can do something like this:

$servers = Get-Content C:\powershell\Scripts\SerialNumber\servers.txt | Where{$_}

The Where will return only non-null objects.

May 2, 2017 at 12:45 pm

Hi David,

Yes, you are right. It was something to do with my text file and a blank line. I figured it out soon after I posted this question, but unfortunately as it takes a while for a question to be approved, I could not cancel it.

Thanks for responding though!

-A