停止单引号转义 WMI 查询 powershell 中的字符串 [英] Stop a single quote from escaping a string in a WMI query powershell
本文介绍了停止单引号转义 WMI 查询 powershell 中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些使用 WMI 查询的代码,但我遇到了一个问题,我使用的变量有一个 '(单引号),这会导致代码出现故障
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
这是一个例子:
$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?
谢谢本
推荐答案
糟糕,原来我应该使用 \ 而不是 `
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
谜团解开!
这篇关于停止单引号转义 WMI 查询 powershell 中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文