简体   繁体   English

Powershell escaping 单引号

[英]Powershell escaping single quotes

How can I escape the single quote in the below Powershell command?如何在下面的 Powershell 命令中转义单引号?

Get-Recipient -Filter "Members -eq 'CN=Jane D'Amico (Contoso),OU=fourthcoffee.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizations,DC=NAMPR1AA001,DC=PROD,DC=OUTLOOK,DC=COM'"

I suspect this is failing due to the D'Amico .我怀疑这是由于D'Amico而失败的。 Not a powershell person so please go easy on me.不是 powershell 人,所以请 go 对我放心。

Cannot bind parameter 'Filter' to the target.无法将参数“过滤器”绑定到目标。 Exception setting "Filter": "Invalid filter syntax.异常设置“过滤器”:“过滤器语法无效。

As long as you're just passing a string (which you are), you can use doublequotes again, and escape it like so:只要你只是传递一个字符串(你是),你可以再次使用双引号,并像这样转义它:

Get-Recipient -Filter "Members -eq `"CN=Jane D'Amico (Contoso),OU=fourthcoffee.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizations,DC=NAMPR1AA001,DC=PROD,DC=OUTLOOK,DC=COM`""

To escape quotes you need to double them up, so D'Amico needs to become D''Amico.为了避免引用,您需要将它们加倍,因此 D'Amico 需要成为 D'Amico。

A good way to handle this would be to assign your user string to a variable first处理此问题的一个好方法是首先将您的用户字符串分配给一个变量

$user = "CN=Jane D'Amico (Contoso),OU=fourthcoffee.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizations,DC=NAMPR1AA001,DC=PROD,DC=OUTLOOK,DC=COM"

then replace any single quotes inside before using it然后在使用之前替换里面的任何单引号

$user = $user -replace "'", "''"    
Get-Recipient -Filter "Members -eq '$user'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM