I have some code which uses a WMI query, but i'm running into an issue where the variable i'm using has a ' (single quote), which causes the code to malfunction
Here is an example:
$path = "\\SERVER1\Mike O'Leary$"
$servername = $path.Split('\')[2].Split('\')[0]
$sharename = $path -replace ".*\\" -replace "'", "`'"
Get-WmiObject Win32_share -computer $servername -filter "name='$sharename'" | Select Name,Path
The problem is that the share name contains a ' character so it errors out. Paths without ' work fine
I tried using the replace seen above but this doesn't help
I've tried various combinations of quotes but I can't get it right, can anyone assist?
Thanks Ben
Oops, turns out I should've been using \ instead of `
$path = "\\SERVER1\Mike O'Leary$"
$servername = $path.Split('\')[2].Split('\')[0]
$sharename = $path -replace ".*\\" -replace "'", "\'"
Get-WmiObject Win32_share -computer $servername -filter "name='$sharename'" | Select Name,Path
Mystery solved!