有没有办法使用 start-process 将可序列化对象传递给 PowerShell 脚本? [英] Is there a way to pass serializable objects to a PowerShell script with start-process?

查看:26
本文介绍了有没有办法使用 start-process 将可序列化对象传递给 PowerShell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 PowerShell 作业,但我想使用 start-process 并将可序列化对象传递给新的 powershell 进程.有没有办法做到这一点?

I know about PowerShell jobs, but I want to use start-process and pass a serializable object to the new powershell process. Is there any way to do this?

似乎使用 start-process 必须提供一个字符串参数列表,它不会为我剪切它.我正在尝试将 PSCredential 从一个进程获取到另一个进程(或 SecureString,我将采用其中任何一个).也许这会规避安全性.

It seems that using start-process you have to provide a string argument list which won't cut it for me. I'm trying to get a PSCredential from one process to another (or a SecureString, I'll take either one). Maybe this circumvents security.

更新 - 添加我在看到其他人的帮助后使用的解决方案(使用来自@PetSerAl 的解决方案)

UPDATE - adding the solution I used after seeing help from others (using solution from @PetSerAl)

我写了两个测试脚本:一个父脚本和一个子脚本.父脚本调用子脚本.

I wrote two test scripts: a parent script and a child script. The parent script calls the child script.

父脚本:

$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:
eposTestPowerShell ScriptsKirkTestChild.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

推荐答案

是的.正如 PetSerAl 在评论中所写,您可以使用 PSSerializer 类来处理:>

Yes. As PetSerAl wrote in a comment, you can use the PSSerializer class to handle that:

$ser = [System.Management.Automation.PSSerializer]::Serialize($credential)

$cred = [System.Management.Automation.PSSerializer]::Deserialize($ser)

我不知道您的流程是否会理解 CliXML 格式;您没有指定要启动的流程.

I don't know if your process will understand the CliXML format though; you don't specify what process you're starting.

由于这将生成多行的 XML,因此将其作为参数传递给进程可能不起作用,这就是 PetSerAl 包含对生成的 XML 进行 Base64 编码的代码的原因.

Since this will produce XML which is multi-line, it may not work to pass this to a process as a parameter which is why PetSerAl is including the code to Base64 encode the resulting XML.

您也可以通过 STDIN 而不是 Base64 编码传递 XML,这也确保您不会意外达到命令行大小限制.

You might also be able to pass the XML over STDIN instead of Base64 encoding, which also ensures that you won't accidentally hit the command line size limit.

这篇关于有没有办法使用 start-process 将可序列化对象传递给 PowerShell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆