我知道 PowerShell 作业,但我想使用 start-process 并将可序列化的对象传递给新的 powershell 进程。有没有办法做到这一点?
似乎使用 start-process 你必须提供一个字符串参数列表,它不会为我剪掉它。我正在尝试将 PSCredential 从一个进程获取到另一个进程(或 SecureString,我将采用其中一个)。也许这会绕过安全性。
更新 - 在看到其他人的帮助后添加我使用的解决方案(使用 @PetSerAl 的解决方案)
我写了两个测试脚本:一个父脚本和一个子脚本。父脚本调用子脚本。
父脚本:
$securePassword = ConvertTo-SecureString "testpassword" -AsPlainText -Force
$cred = New-Object PSCredential("testuser", $securePassword)
$credSerial = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([Management.Automation.PSSerializer]::Serialize($cred)))
$psFile = "C:\repos\Test\PowerShell Scripts\KirkTestChild.ps1"
$p1 = "-someparam ""this is a test!!!"""
$p2 = "-cred ""$credSerial"""
$proc = Start-Process PowerShell.exe -PassThru:$true -Argument "-File ""$($psFile)""", $p1, $p2
Write-Host "ID" $proc.Id
Write-Host "Has Exited" $proc.HasExited
Start-Sleep -Seconds 15
Write-Host "Has Exited" $proc.HasExited
子脚本:
Param(
$someParam,
$cred
)
Write-Host "someParam: $($someParam)"
Write-Host "cred (raw): $($cred)"
$realCred=[Management.Automation.PSSerializer]::Deserialize([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($cred)))
Write-Host "cred user: $($realCred.UserName)"
Write-Host "start"
Start-Sleep 5
Write-Host "ending"
Start-Sleep 5