【问题标题】:What is the difference between PowerShell and cmd.exe command syntax?PowerShell 和 cmd.exe 命令语法有什么区别?
【发布时间】:2013-09-10 14:11:43
【问题描述】:

我在 PowerShell 中运行以下命令:

PS C:\Users\adminaccount> winrm s winrm/config/service @{AllowUnencrypted="true";
MaxConcurrentOperationsPerUser="4294967295"}
Error: Invalid use of command line. Type "winrm -?" for help.

如您所见,这给了我一个错误。但是 cmd.exe 中的相同命令可以正常工作:

C:\Users\adminaccount>winrm s winrm/config/service @{AllowUnencrypted="true";
MaxConcurrentOperationsPerUser="4294967295"}
Service
...

那么,我应该了解哪些有关 PowerShell 语法的知识才能让它在那里工作?

【问题讨论】:

  • 不应该引用"(例如用`)?
  • @PeterMortensen 我不知道,我上次接触过版本 3 的 PowerShell,并且再也没有使用过它。但是为什么你认为"应该被引用呢?

标签: powershell cmd winrm


【解决方案1】:

@{} 在 PowerShell 中定义了一个哈希表,但 winrm 需要一个字符串参数。如果您想直接在 PowerShell 中运行命令,请将该参数放在引号中:

winrm s winrm/config/service '@{AllowUnencrypted="true"; MaxConcurrentOperationsPerUser="4294967295"}'

此外,您需要管理员权限才能使用此功能。

【讨论】:

    【解决方案2】:

    或者使用特殊的--% 参数让PowerShell停止解析参数。

    winrm --% s winrm/config/service @{AllowUnencrypted="true";MaxConcurrentOperationsPerUser="4294967295"}
    

    【讨论】:

    • 这行得通,但如果那里有应该扩展的 PowerShell 则不行。示例:` + $redirectSpec` 在末尾和 $redirectSpec 包含“>> _output_2018-09-07T195607.txt”。
    【解决方案3】:

    您可以在命令前加上 cmd /c 并引用它:

    cmd /c "winrm s winrm/config/service @{AllowUnencrypted=`"true`";
    MaxConcurrentOperationsPerUser=`"4294967295`"}" 
    

    PowerShell 将执行系统中存在的可执行文件。

    【讨论】:

    • 仍然无法正常工作:“错误:命令行使用无效。键入“winrm -?”寻求帮助。”
    • 嗯,我试图解释你问的区别。您将需要处理特殊字符、转义引号等。请参阅更新的命令
    • 根据命令,您可能需要在管理员模式下启动 Powershell...
    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 2023-01-26
    • 2019-08-29
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多