【发布时间】:2019-03-02 14:56:23
【问题描述】:
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$True)]
[securestring]
$securityKey
)
powershell.exe -File $PSCommandPath -thisAppDomain @PSBoundParameters
此代码引发以下错误:
无法处理参数转换 参数“安全密钥”。无法将“System.String”类型的“System.Security.SecureString”值转换为“System.Security.SecureString”类型
如果我检查securityKey 的类型,它在喷出之前是一个 SecureString。我认为它出于某种原因对其进行了序列化。如何防止这种情况发生?
编辑
这个用例可能看起来很奇怪,所以我会提供一些上下文。我需要确保在管道到此脚本时加载特定版本的程序集。我正在使用thisAppDomain 参数在新的应用程序域中重新启动以尝试完成此操作。更大的例子:
.\FirstScript.ps1 | .\SecondScript.ps1
安全字符串按预期输入,但在重新启动时转换为字符串。这就是我重新启动的方式:
if(-not $thisAppDomain)
{
Write-Host "Invoking script in a new app domain"
powershell.exe -File $PSCommandPath -thisAppDomain @PSBoundParameters
return;
}
【问题讨论】:
-
这个脚本怎么称呼?你能举个例子吗?主要是想看看
-thisAppDomain可能会期待什么。你的powerShell版本是什么 -
powershell.exe 不是 powershell cmdlet,所以我看不出 splatting 是如何工作的。这是一个命令行程序,只在命令行上接受字符串输入。
-
如果你想直接执行命令,你可以试试:& $PSCommandPath -thisAppDomain @PSBoundParameters(避免使用新的powershell.exe)
-
添加了更多细节。
-
您的代码不是一个功能齐全、工作正常的最小示例,因此很难重现和测试一个想法。将securestring转换为base64编码的字符串/结构,将其传递到第二个并转换回来。脚本中可能需要c#代码……不知道……没试过。
标签: powershell