ADMT命令行迁移含双引号OU的组失败,求C#解决方案
ADMT命令行迁移含双引号OU的组失败解决方法
问题背景
使用ADMT命令行测试组迁移时,当目标OU名称包含双引号,迁移失败。执行命令如下:
ADMT GROUP /n "TestGroup" /sd:Child.A.COM /sdc:CHILD.A.COM /td:.COM /tdc:A.COM /to:"ParentOU/TEST!@#$%^&*()_+{}|:\"<>?[]\;',./" /intraforest:yes
- CMD执行报错:
> was unexpected at this time - PowerShell执行时持续等待输入参数
尝试过""、^"、`"等转义方式均无效,仅""转义时命令可运行,但仍报错:
Error: Unable to migrate groups. Unable to bind to container
'ParentOU/TEST!@#$%^&()+{}|:<>?[];',./ /intraforest:yes'. Unable to
get distinguished name for
'A.COM/ParentOU/TEST!@#$%^&;;()+{}|:<>?[];',./ /intraforest:yes'. :
The parameter is incorrect. (0x80070057)
不含双引号的同名OU可正常迁移。
解决方案
1. 针对CMD/PowerShell的正确转义方式
CMD环境
CMD中需要用^转义特殊字符,双引号用^",&用^&,且整个/to参数需用双引号包裹:
ADMT GROUP /n "TestGroup" /sd:Child.A.COM /sdc:CHILD.A.COM /td:.COM /tdc:A.COM /to:"ParentOU/TEST!@#$%^^&*()_+{}|:^""<>?[]\;',./" /intraforest:yes
PowerShell环境
PowerShell中用`(反引号)转义双引号和&,或用单引号包裹路径避免转义:
# 方式1:双引号包裹,内部特殊字符转义 ADMT GROUP /n "TestGroup" /sd:Child.A.COM /sdc:CHILD.A.COM /td:.COM /tdc:A.COM /to:"ParentOU/TEST!@#$%^`&*()_+{}|:`"<>`?[]\;',./" /intraforest:yes # 方式2:拆分成参数数组,避免手动转义 $admtArgs = @( "GROUP", "/n", "TestGroup", "/sd:Child.A.COM", "/sdc:CHILD.A.COM", "/td:.COM", "/tdc:A.COM", "/to:ParentOU/TEST!@#$%^&*()_+{}|:`"<>`?[]\;',./", "/intraforest:yes" ) & "ADMT.exe" $admtArgs
2. 使用OU的可分辨名称(DN)
直接使用目标OU的DN替代路径,彻底避免转义问题。例如OU的DN为OU=TEST!@#$%^&*()_+{}|:\"<>?[]\;',./,OU=ParentOU,DC=A,DC=COM,命令如下:
ADMT GROUP /n "TestGroup" /sd:Child.A.COM /sdc:CHILD.A.COM /td:.COM /tdc:A.COM /to:"OU=TEST!@#$%^^&*()_+{}|:^""<>?[]\;',./,OU=ParentOU,DC=A,DC=COM" /intraforest:yes
3. C#实现方案
用ProcessStartInfo传递参数,.NET会自动处理转义,无需手动操作:
using System.Diagnostics; ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "ADMT.exe", Arguments = @"GROUP /n ""TestGroup"" /sd:Child.A.COM /sdc:CHILD.A.COM /td:.COM /tdc:A.COM /to:""ParentOU/TEST!@#$%^&*()_+{}|:\""<>\?[]\;',./"" /intraforest:yes" }; Process.Start(startInfo);
或使用参数数组传递,更清晰可靠:
using System.Diagnostics; using System.Linq; string[] args = { "GROUP", "/n", "TestGroup", "/sd:Child.A.COM", "/sdc:CHILD.A.COM", "/td:.COM", "/tdc:A.COM", "/to:ParentOU/TEST!@#$%^&*()_+{}|:\"<>?[]\\;',./", "/intraforest:yes" }; Process.Start("ADMT.exe", string.Join(" ", args.Select(arg => $"\"{arg.Replace("\"", "\\\"")}\"")));
内容的提问来源于stack exchange,提问作者Ammu Shiri K
相关产品推荐
相关产品推荐

