powershell replace命令失败,因为该字符串包含字符' \'在里面

时间:2015-08-26 14:40:56

标签: powershell-v3.0

我从WMI中提取机器的本地登录用户,并将其存储在变量中,如下所示:

$user = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $hostname).username

这很有效。问题是WMI以

格式返回信息

contoso \ username

我需要将变量插入到Get-ADUser命令中,该命令不会接受

" contoso \"

当我尝试在powershell中使用replace命令时,由于存在' \'在命令中:

$user = $user -replace "CONTOSO\"

The regular expression pattern CONTOSO\ is not valid.
At line:1 char:1
+ $user = $user -replace "CONTOSO\"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (CONTOSO\:String) [],        RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression

是否有其他方法可以删除这些字符,以便将其插入Get-ADUser?在将其插入Get-ADUser之前,可能会清除 gwmi 的输出?或者,是否存在某种字符转义序列?

0 个答案:

没有答案