0

This is not the exact scenario of what I'm trying to accomplish but is a good example of where I am stuck.

$Path = "\\192.168.1.1\config"
Get-CimInstance -Namespace ROOT\CIMV2 -ClassName Win32_MappedLogicalDisk -Filter "ProviderName=$Path"

This produces an "Invalid Query" error as it's trying to use \\ in the query

I know I can do this manually by using:

Get-CimInstance -Namespace ROOT\CIMV2 -ClassName Win32_MappedLogicalDisk -Filter "ProviderName='`\\`\\192.168.1.1`\\config'"

This doesn't solve my problem as I need to pass the value from a variable

Any help would be greatly appreciated

1
  • I also don't want to query the whole class and then filter after. e.g. Get-CimInstance -Namespace ROOT\CIMV2 -ClassName Win32_MappedLogicalDisk | where {$_.ProviderName -eq $Path} as it will slow down the script massively Commented Sep 23, 2020 at 5:35

1 Answer 1

1

You can do an inline replace to escape the backslashes:

$Path = "\\192.168.1.1\config"
Get-CimInstance -Namespace ROOT\CIMV2 -ClassName Win32_MappedLogicalDisk -Filter "ProviderName='$($Path -replace '\\','\\')'"

Note: Don't forget to put single quotes around the value part of the filter.

$() is the subexpression operator. Since -replace uses regex, a single \ must also be escaped for the match expression.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.