Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variables in Get-WmiObject filters? 1

Status
Not open for further replies.

shinedog

MIS
Feb 24, 2004
60
US
So I think this should be pretty easily but its something I constantly trip over.

In this specific example, I am trying to compress a folder. The following code works fine:

Code:
(Get-WmiObject -class "Win32_Directory" -filter "name='C:\\Windows\\System32\\LogFiles'").Compress()

However for obvious reasons, I don't want to hard-code common paths into my scripts. So instead of C:\Windows, I'd like to use $env:systemroot. However, Get-WmiObject always seems to choke when plugging in variables into parameters. Any variation of using that variable fails with "Get-WmiObject : Invalid Query". An example of what I am trying to do is listed below:

Code:
(Get-WmiObject -class "Win32_Directory" -filter "name=$env:systemroot\\System32\LogFiles").Compress()

Is what I want to do even possible? If it is, I bet I'm overlooking something pretty simple?
 
The problem is that your env variable only has a single slash in it. To prove this run the following:
Code:
$x = "name=$env:systemroot\\System32\\LogFiles"
$x

So, try this instead
Code:
$Mypath = "name='"+$env:SystemRoot.Replace("\","\\")+"\\system32\\logfiles'";(Get-WmiObject -class "Win32_Directory" -filter "$Mypath").Compress()

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top