承载多个PowerShell版本
问题描述:
在this page上,PowerShell团队演示如何在您的.NET应用程序中托管PowerShell引擎并向其传递命令。承载多个PowerShell版本
有什么方法可以控制创建PowerShell引擎的哪个版本?也许使用v2的一个脚本,而v4的另一个?
我本来以为InitialSessionState.Create方法将使得它,但它不会出现这种情况。
答
我做了一个快速功能您在工作与PowerShell中执行脚本块2.0
Function Execute-Job ([scriptblock]$ScriptBlock, [array]$ArgumentList, [switch]$PassThru, [switch]$PS2) {
If ($PS2) { $Job = Start-Job -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList -PSVersion 2.0 }
Else { $Job = Start-Job -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList }
If ($PassThru) { $Job | Receive-Job -Wait -AutoRemoveJob }
Else { $Job }
} #End Function Execute-Job
$ScriptBlock = { $PSVersionTable }
Execute-Job $ScriptBlock -PassThru -Ps2
这回:
Name Value
---- -----
PSRemotingProtocolVersion 2.1
BuildVersion 6.1.7601.17514
PSCompatibleVersions {1.0, 2.0}
PSVersion 2.0 <----------
CLRVersion 2.0.50727.5485
WSManStackVersion 2.0
SerializationVersion 1.1.0.1
告诉我你的想法! ;)
发布到TechNet:https://gallery.technet.microsoft.com/Function-Execute-Job-c2f76200 –
这很好,但不是我们之后。我们需要一个.NET应用程序能够将脚本发送到PS 2以及PS 3(或PS 4)引擎,所有这些都在同一个应用程序中。 – DougN
我以为我的代码很可爱..:P好吧,所以你的PS脚本嵌入在你的.NET应用程序中,所以你不要直接调用powershell.exe来执行。对? –