将带有复杂参数的ScriptBlock传递给新的PowerShell实例
我想用复杂的参数执行以下脚本。例如,使用[Securestring]。
$text = "'This is a test message.'"
$ArgumentList = @( $text, $PID ) -join ", "
$cmd = { param([string]$msg, [int]$proc ); Write-Host "$msg FROM PID: $proc" }
$Command = "Invoke-Command -ScriptBlock {$cmd} -ArgumentList $ArgumentList"
Start-Process -Filepath powershell -ArgumentList "-noexit -command ( $Command )"
这个脚本很好。
我将此脚本转换为新脚本,
$text = "'This is a test message.'"
$Cred = get-credential
$ArgumentList = @( $text, $PID, $credential ) -join ", "
$cmd = { param([string]$msg, [int]$proc, $Credential ); Write-Host "$msg FROM PID: $proc, cred: $( $Credential.username )" }
$Command = "Invoke-Command -ScriptBlock {$cmd} -ArgumentList $ArgumentList"
Start-Process -Filepath powershell -ArgumentList "-noexit -command ( $Command )"
我有一个错误。 如何通过论据?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如 @mathias ,我会考虑在整个过程中运行整个过程 ”
无论如何,您也可以序列化(全部)您的参数,然后将其转换为 base64 base64 base64 当您传递它时,多个解释器:
注意: 默认使用Windows Data Protection API,并且用于加密密码的密钥是特定于用户和机器的代码正在运行。
As suggested by @Mathias, I would consider to run the whole process under the specific credentials
Anyways, you might also serialize (all) your arguments and convert it to Base64 as you passing it trough multiple interpreters:
Note: that
Get-Credential
by default uses the Windows data protection API, and the key used to encrypt the password is specific to both the user and the machine that the code is running under.