【问题标题】:One-liner PowerShell script单行 PowerShell 脚本
【发布时间】:2012-05-10 14:53:54
【问题描述】:

我创建了这个基本的单行 PowerShell 脚本,如果我为 AD 运行 ad cmdlet,然后运行其余查询,就会执行该脚本。但是尝试将它们一起运行时,它似乎只加载了 cmdlet,并没有执行 cmd 的其余部分。

powershell.exe -command "&{Import-Module ActiveDirectory; Get-AdGroup -Server DC.Mydomain.com -filter 'name -eq "xxxx"'| set-Adgroup -Replace @{wWWHomePage='10.24.218.194'}}"

为什么不像这样一起运行?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    答案是转义双引号:

    powershell.exe -noprofile -command "&Import-Module ActiveDirectory; Get-AdGroup -Server server.mydomain.com -filter 'name -eq *\"xxxx\"*'| set-Adgroup -Replace @{wWWHomePage='10.10.10.10'}"
    

    基本上,我从 SQL 运行此命令来更新 DSADD 无法访问的 ActiveDirectory 属性。

    【讨论】:

      【解决方案2】:

      这看起来像是一个引用问题。尝试用大括号替换周围的过滤器引号:

      -filter {name -eq "xxxx"}
      

      为了避免这种情况,当你有很长的命令要执行时,我建议你把命令放在一个脚本文件中,并将其路径传递给-File参数。

      【讨论】:

        【解决方案3】:
        # Start-Run, type:
        powershell.exe -noprofile -command "[guid]::newguid()|Set-Clipboard"
        

        【讨论】:

          【解决方案4】:

          如果对复杂的命令有疑问,您可以尝试使用 Base64 编码并使用-EncodedCommand。示例见powershell /?

          直接在 PowerShell 中输入该行是否按预期工作?

          【讨论】:

          • 当我直接在 powershell 中使用它时,该行确实有效。如果导入 AD 模块,然后运行它的其余部分,它每次都可以工作。
          猜你喜欢
          • 2018-09-20
          • 1970-01-01
          • 1970-01-01
          • 2014-01-06
          • 1970-01-01
          • 1970-01-01
          • 2021-11-06
          • 2012-09-18
          • 2012-06-14
          相关资源
          最近更新 更多