【问题标题】:Powershell different Credentials with encapsulated arguments具有封装参数的 Powershell 不同凭据
【发布时间】:2017-06-16 13:07:35
【问题描述】:

在尝试了几种可能性后,我不确定如何继续。

我需要执行一个脚本"C:\test1\script.ps1" 并且我还需要用这个脚本传递一些参数。这些参数(-Hashtag $Hashtag -Name $Name 等等...)存储在一个名为 $arguments 的变量中。

所以最终调用应该是这样的:C:\test1\script.ps1 $arguments

这没问题,但现在我无法设法工作。

我的目标是以不同的用户身份执行此操作。登录凭据存储在$cred

与论坛中的相关主题相比,我已经尝试了以下内容:

  1. 命令

    Invoke-Command -Credential $cred -Authentication Credssp -ComputerName home -ScriptBlock {C:\test1\script.ps1 $arguments}
    

    Powershell 结果:某些参数(在本例中为 Hashtag)为空。我不确定为什么会发生这种情况,因为我只是用所有的东西填充了 $arguments 变量。

  2. 命令

    Start-Process -FilePath "C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe" -ArgumentList "C:\test1\script.ps1 $arguments -Credential $cred -WorkingDirectory 'C:\\Windows\System32'
    

    这导致脚本不会被执行。

有没有人有更多关于处理该结构的信息?也许我可以将凭据传递给另一个命令?

另外,我不能使用凭据提示,因此考虑到 UAC 的任何解决方案都不适合我的问题。

【问题讨论】:

  • $arguments 需要是 splatted 例如。 @arguments
  • 我按照说明进行操作,但它也不起作用。 $arguments= @{ Argument1 = $Argument1 Argument2 = $Argument2 }
  • 我按照说明进行操作,但它也不起作用。 $arguments= @{ Argument1 = $Argument1 Argument2 = $Argument2 } 执行所需的脚本,没问题。但是给我一个参数为空的错误......当我打印@arguments时,我需要的所有数据。所以飞溅应该可以正常工作。但实际上这些参数 son#t 正确地传递给了脚本。我现在这样调用脚本:-ScriptBlock {C:\test1\script.ps1 @ausfuehrung} 对吗?
  • 如果你这样传递它,没有参数块或使用参数列表,那么你需要使用using: 范围。看到这个question
  • 感谢您的建议,但我得到了它的运行:start powershell.exe -Argumentlist "C:\test1\script.ps1 $arguments" -credential $credential 现在只是缺少返回值。但我正试图自己解决这个问题。非常感谢您的帮助:) 我学到了很多关于泼溅的知识,这是我以前从未听说过的。谢谢!

标签: powershell credentials invoke-command start-process


【解决方案1】:
Invoke-Command -Credential $cred -Authentication Credssp -ComputerName home -ScriptBlock {C:\test1\script.ps1} -ArgumentList 'argument1','argument2','argument3'

确保在脚本中定义了位置参数。这样,参数 1 将分配给脚本中的第一个参数,依此类推。

【讨论】:

  • 好吧,如果我使用这种语法,Powershell 会告诉我 ParameterName “Arguments”没有参数。我是这样写的,因为参数动态变化我需要使用变量:-ScriptBlock {C:\test1\script.ps1} -Arguments $Argument1, $Argument2...
  • 我的错,参数应该是-ArgumentList
  • 感谢您的建议,但我得到了它的运行:start powershell.exe -Argumentlist "C:\test1\script.ps1 $arguments" -credential $credential 现在只是缺少返回值。但我正试图自己解决这个问题。非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多