# Passing variables as parameters to Get-WMIObject

**URL:** <https://forums.powershell.org/t/passing-variables-as-parameters-to-get-wmiobject/830>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/passing-variables-as-parameters-to-get-wmiobject/830 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/passing-variables-as-parameters-to-get-wmiobject/830/1 "2011-12-31T18:30:00Z")

</div>

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.ps1serverC: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: (🙂 [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommandserver1D: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: (🙂 [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommandserver2C: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: (🙂 [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: (🙂 [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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/passing-variables-as-parameters-to-get-wmiobject/830/2 "2024-05-16T21:02:52Z")

</div>


