Passing variables as parameters to Get-WMIObject

by billd at 2013-04-05 09:00:32

Greetings,
I’m new to PS, and I’m having trouble getting my script to work properly.

I have an input file:

ServerName;Drive;MinFreeGB
server1;C:;2
server1;D:;5
server2;C:;5

For each entry in the file, I want to check the appropriate server disk space on the appropriate drive.

My script is as follows:



$servers = Import-CSV -path $filename -Delimiter ";"

foreach ($server in $servers) {
$server.ServerName
$server.Drive

Get-WMIObject -ComputerName "$server.ServerName" Win32_LogicalDisk -filter "Name=‘$server.Drive’"
}



The first two lines in the foreach loop are just for debugging purposes (output them to screen).

When I run the script, I’m getting the following results:


PS C:\Scheduled Tasks> .\checkDiskSpace.ps1
server
C:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scheduled Tasks\checkDiskSpace.ps1:9 char:15
+ Get-WMIObject <<<< -ComputerName "$server.ServerName" Win32_LogicalDisk -filter "Name=‘$server.Drive’"
+ CategoryInfo : InvalidOperation: (:slight_smile: [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

server1
D:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scheduled Tasks\checkDiskSpace.ps1:9 char:15
+ Get-WMIObject <<<< -ComputerName "$server.ServerName" Win32_LogicalDisk -filter "Name=‘$server.Drive’"
+ CategoryInfo : InvalidOperation: (:slight_smile: [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

server2
C:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scheduled Tasks\checkDiskSpace.ps1:9 char:15
+ Get-WMIObject <<<< -ComputerName "$server.ServerName" Win32_LogicalDisk -filter "Name=‘$server.Drive’"
+ CategoryInfo : InvalidOperation: (:slight_smile: [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand



My difficulty is with passing variables to Get-WMIObject. If I run the command manually at the Powershell prompt, it works fine for each of the servers and drives (when the values are hard-coded).

Any suggestions?

Thanks much,

Bill
by mjolinor at 2013-04-05 09:19:51
Does this help?

Get-WMIObject -ComputerName "$server.ServerName" Win32_LogicalDisk -filter "Name='$($server.Drive)'"
by billd at 2013-04-05 11:03:37
Thanks for the reply.
No, that doesn’t seem to help:


Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scheduled Tasks\checkDiskSpace.ps1:7 char:15
+ Get-WMIObject <<<< -ComputerName "$server.ServerName" Win32_LogicalDisk -filter "Name=‘$($server.Drive)’"
+ CategoryInfo : InvalidOperation: (:slight_smile: [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand


Thanks again,

Bill
by mjolinor at 2013-04-05 11:19:56
My mistake. I think you’ll need to do both of those:

Get-WMIObject -ComputerName "$($server.ServerName)" Win32_LogicalDisk -filter "Name='$($server.Drive)'"
by billd at 2013-04-05 11:58:52
Ah, yes, that’s much better. I thought I tried that after your first post, but I must have done something wrong. In any case, the above works nicely.
Thanks much!

Bill