我正在更新一个管理一些. net程序集的PowerShell脚本。该脚本是为在。net 2(与PowerShell运行的框架相同版本)上构建的程序集编写的,但现在需要使用。net 4程序集以及。net 2程序集。
由于. net 4支持运行基于旧版本框架构建的应用程序,因此似乎最简单的解决方案是在需要针对. net 4程序集运行PowerShell时,使用。net 4运行时启动PowerShell。
如何使用。net 4运行时运行PowerShell ?
我正在更新一个管理一些. net程序集的PowerShell脚本。该脚本是为在。net 2(与PowerShell运行的框架相同版本)上构建的程序集编写的,但现在需要使用。net 4程序集以及。net 2程序集。
由于. net 4支持运行基于旧版本框架构建的应用程序,因此似乎最简单的解决方案是在需要针对. net 4程序集运行PowerShell时,使用。net 4运行时启动PowerShell。
如何使用。net 4运行时运行PowerShell ?
当前回答
只需运行powershell.exe,并将COMPLUS_version环境变量设置为v4.0.30319。 例如,从cmd.exe或。bat-file:
set COMPLUS_version=v4.0.30319
powershell -file c:\scripts\test.ps1
其他回答
如果你不想修改注册表或app.config文件,另一种方法是创建一个简单的。net 4控制台应用程序,它模仿PowerShell.exe的功能并托管PowerShell ConsoleShell。
参见选项2 -自己托管Windows PowerShell
首先,添加一个对System.Management.Automation和Microsoft. powershell . consolehost程序集的引用,它们可以在%programfiles%\ reference assemblies \Microsoft\WindowsPowerShell\v1.0下面找到
然后使用下面的代码:
using System;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell;
namespace PSHostCLRv4
{
class Program
{
static int Main(string[] args)
{
var config = RunspaceConfiguration.Create();
return ConsoleShell.Start(
config,
"Windows PowerShell - Hosted on CLR v4\nCopyright (C) 2010 Microsoft Corporation. All rights reserved.",
"",
args
);
}
}
}
其他的答案来自2012年之前,他们专注于“破解”PowerShell 1.0或PowerShell 2.0,以瞄准更新版本的。net框架和公共语言运行时(CLR)。
However, as has been written in many comments, since 2012 (when PowerShell 3.0 came) a much better solution is to install the newest version of PowerShell. It will automatically target CLR v4.0.30319. This means .NET 4.0, 4.5, 4.5.1, 4.5.2, or 4.6 (expected in 2015) since all of these versions are in-place replacements of each other. Use $PSVersionTable or see the Determine installed PowerShell version thread if you are unsure of your PowerShell version. Edit: Also thread Which .NET version is my PowerShell script using?
在撰写本文时,PowerShell的最新版本是4.0,可以通过Windows管理框架(谷歌搜索链接)下载。
如果您只需要在。net 4中执行单个命令、脚本块或脚本文件,请尝试使用。net 4中的激活配置文件,使用CLR版本4仅启动单个PowerShell实例。
详情:
http://blog.codeassassin.com/2011/03/23/executing-individual-powershell-commands-using-net-4/
PowerShell模块示例:
https://gist.github.com/882528
如果您还停留在PowerShell 1.0或2.0版本上,下面是我对Jason Stangroome的精彩回答的改进。
创建一个powershell4。CMD在你路径的某处,包含以下内容:
@echo off
:: http://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
if exist %~dp0powershell.exe.activation_config goto :run
echo.^<?xml version="1.0" encoding="utf-8" ?^> > %~dp0powershell.exe.activation_config
echo.^<configuration^> >> %~dp0powershell.exe.activation_config
echo. ^<startup useLegacyV2RuntimeActivationPolicy="true"^> >> %~dp0powershell.exe.activation_config
echo. ^<supportedRuntime version="v4.0"/^> >> %~dp0powershell.exe.activation_config
echo. ^</startup^> >> %~dp0powershell.exe.activation_config
echo.^</configuration^> >> %~dp0powershell.exe.activation_config
:run
:: point COMPLUS_ApplicationMigrationRuntimeActivationConfigPath to the directory that this cmd file lives in
:: and the directory contains a powershell.exe.activation_config file which matches the executable name powershell.exe
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=%~dp0
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe %*
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=
这将允许您启动在。net 4.0下运行的powershell控制台实例。
通过检查从cmd运行的以下两个命令的输出,您可以看到在我的PowerShell 2.0系统上的差异。
C:\>powershell -ExecutionPolicy ByPass -Command $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
C:\>powershell4.cmd -ExecutionPolicy ByPass -Command $PSVersionTable
Name Value
---- -----
PSVersion 2.0
PSCompatibleVersions {1.0, 2.0}
BuildVersion 6.1.7601.17514
CLRVersion 4.0.30319.18408
WSManStackVersion 2.0
PSRemotingProtocolVersion 2.1
SerializationVersion 1.1.0.1
实际上,您可以让PowerShell使用。net 4运行而不影响其他。net应用程序。我需要这样做来使用新的HttpWebRequest“Host”属性,然而改变“OnlyUseLatestCLR”破坏了Fiddler,因为它不能在。net 4下使用。
PowerShell的开发人员显然预见到了这种情况,他们添加了一个注册表项来指定应该使用哪个版本的框架。一个小问题是,您需要在更改注册表项之前获得它的所有权,因为即使管理员也没有访问权限。
\Software\Microsoft\Powershell\1\PowerShellEngine\RuntimeVersion(64位及32位) \Software\Wow6432Node\Microsoft\Powershell\1\PowerShellEngine\RuntimeVersion(32位在64位机器上)
将该键的值更改为所需的版本。请记住,一些snapin可能不再加载,除非它们与。net 4兼容(WASP是我唯一遇到过麻烦的一个,但我实际上并不使用它)。VMWare, SQL Server 2008, PSCX, Active Directory (Microsoft和Quest Software)和SCOM都可以正常工作。