简体   繁体   English

我们可以检查Powershell脚本所需的Powershell / WPF / .Net版本吗?

[英]Can we check for needed Powershell/WPF/.Net Version for Powershell Scripts?

I would like to be able to see how far back my scripts are compatible with earlier versions of software so, I'd like to see what the minimum version of Powershell, .Net and WPF that my scripts would require to run. 我希望能够看到我的脚本与较早版本的软件兼容的程度,因此,我想了解脚本需要运行的最低Powershell,.Net和WPF版本。 I think I would be able to accomplish this if I knew how to retrieve information about each .Net class and PowerShell cmdlet and what version they were introduced in. 如果我知道如何检索有关每个.Net类和PowerShell cmdlet的信息以及它们引入的版本,我想我将能够完成此任务。

EDIT: 编辑:

I probably wasn't have been clear enough so here are some examples found manually online. 我可能还不太清楚,所以这里有一些在线手动找到的例子。 However, I would like to be able to get this information via PowerShell. 但是,我希望能够通过PowerShell获得此信息。 But as Ansgar Wiechers stated this information may not be available very easily. 但是正如Ansgar Wiechers所说,此信息可能不太容易获得。 I was just hoping someone might've already done this or knew how to obtain that information. 我只是希望有人可能已经做到了,或者知道如何获取这些信息。

For example. 例如。

[System.Net.NetworkInformation.Ping] - Available since .Net 2.0 (Found at https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(v=vs.110).aspx#Anchor_8 ) [System.Net.NetworkInformation.Ping]-从.Net 2.0开始可用(位于https://msdn.microsoft.com/zh-cn/library/system.net.networkinformation.ping(v=vs.110).aspx #Anchor_8

Get-CIMInstance - available since Powershell 3.0 (Found https://blogs.msdn.microsoft.com/powershell/2012/08/24/introduction-to-cim-cmdlets/ in the first paragraph) Get-CIMInstance-自Powershell 3.0起可用(在第一段中找到https://blogs.msdn.microsoft.com/powershell/2012/08/24/introduction-to-cim-cmdlets/

Edited based on the OP's clarification 根据OP的说明进行编辑

If you are looking for cmdlets released per WMF version as noted by your links... 如果您正在寻找每个WMF版本发布的cmdlet(如链接所述)...

'Available since .Net 2.0' 'available since Powershell 3.0' “自.Net 2.0起可用”,“自Powershell 3.0起可用”

... then, TechNet provides that here: ...然后,TechNet在这里提供:

PowerShell 1.0 Cmdlets These are the list of cmdlets which can be used in the Windows PowerShell 1.0. PowerShell 1.0 Cmdlet这些是可以在Windows PowerShell 1.0中使用的cmdlet列表。 https://social.technet.microsoft.com/wiki/contents/articles/13769.powershell-1-0-cmdlets.aspx https://social.technet.microsoft.com/wiki/contents/articles/13769.powershell-1-0-cmdlets.aspx

PowerShell 2.0 Cmdlets These are the list of cmdlets which can be used in the Windows PowerShell 2.0. PowerShell 2.0 Cmdlet这些是可在Windows PowerShell 2.0中使用的cmdlet列表。 https://social.technet.microsoft.com/wiki/contents/articles/13876.powershell-2-0-cmdlets.aspx https://social.technet.microsoft.com/wiki/contents/articles/13876.powershell-2-0-cmdlets.aspx

PowerShell 3.0 Cmdlets These are the cmdlets, aliases, and functions available in the Windows 8 Developer Preview. PowerShell 3.0 Cmdlet这些是Windows 8 Developer Preview中可用的cmdlet,别名和功能。
https://social.technet.microsoft.com/wiki/contents/articles/4694.powershell-3-cmdlets.aspx https://social.technet.microsoft.com/wiki/contents/articles/4694.powershell-3-cmdlets.aspx

Note there is no such listing like the above for v4 - v6 (PowerShellCore) that I've ever found. 请注意,我没有找到像上面针对v4-v6(PowerShellCore)的清单。

But there is this: This topic lists the system requirements for Windows PowerShell 3.0, Windows PowerShell 4.0 and Windows PowerShell 5.0, and for special features, such as Windows PowerShell Integrated Scripting Environment (ISE), CIM commands, and workflows. 但是有一个问题:本主题列出了Windows PowerShell 3.0,Windows PowerShell 4.0和Windows PowerShell 5.0以及特殊功能(例如Windows PowerShell集成脚本环境(ISE),CIM命令和工作流)的系统要求。 https://docs.microsoft.com/en-us/powershell/scripting/setup/windows-powershell-system-requirements?view=powershell-5.1 https://docs.microsoft.com/zh-cn/powershell/scripting/setup/windows-powershell-system-requirements?view=powershell-5.1

Considering what you are after. 考虑你在追求什么。 If this were me. 如果这是我。 My beginning approach would be to stand up a pristine set of VM clients (or hand off to someone we'd know who had them) with the appropriate OS's, installed with the RTM WMF version for the OS and run the below, saved to a file. 我的开始方法是用适当的操作系统站起一组原始的VM客户端(或移交给我们知道的虚拟机客户端),并安装适用于该操作系统的RTM WMF版本并运行以下内容,并保存到文件。

Then use that file as the base compare to the code. 然后使用该文件作为与代码进行比较的基础。 Meaning, use a Select-String on all my modules, functions and scripts and matching on cmdlet name to get the other properties in the compare file into a code compare report. 意思是,在我所有的模块,函数和脚本上使用Select-String,并在cmdlet名称上进行匹配,以将比较文件中的其他属性获取到代码比较报告中。

Any cmdlet name matches, could be the baseline indicator the code scanned had a cmdlet used from a particular PoSH version. 任何cmdlet名称匹配,都可能是基线指示,表明所扫描的代码具有特定PoSH版本中使用的cmdlet。 Thus giving the assumption that the code scanned was written either on the OS WMF RTM version or written for a given WMF version using the -version switch. 因此,假定扫描的代码是在OS WMF RTM版本上编写的,或者是使用-version开关针对给定WMF版本编写的。

Report host OS, WMF and CLR version information

$OSVersion = (Get-WmiObject -Class Win32_OperatingSystem).Caption
Get-Command | Where CommandType -Match cmdlet | 
Select Name, Version,
@{Name = 'PSCompatible';Expression = {$PSVersionTable.PSCompatibleVersions}},
@{Name = 'CLR';Expression = {$PSVersionTable.CLRVersion}},
@{Name = 'WSMan';Expression = {$PSVersionTable.WSManStackVersion}},
@{Name = 'Remoting';Expression = {$PSVersionTable.PSRemotingProtocolVersion}},
@{Name = 'OS';Expression = {$OSVersion}} | 
Sort-Object Version | Format-Table -AutoSize


Name                        Version     PSCompatible            CLR             WSMan Remoting OS                      
----                        -------     ----------              ---             ----- -------- --                      
Enable-SqlAlwaysOn          1.0         {1.0, 2.0, 3.0, 4.0...} 4.0.30319.42000 3.0   2.3      Microsoft Windows 10 Pro
Set-SqlAuthenticationMode   1.0         {1.0, 2.0, 3.0, 4.0...} 4.0.30319.42000 3.0   2.3      Microsoft Windows 10 Pro
Disable-SqlAlwaysOn         1.0         {1.0, 2.0, 3.0, 4.0...} 4.0.30319.42000 3.0   2.3      Microsoft Windows 10 Pro
Set-RuleOption              1.0         {1.0, 2.0, 3.0, 4.0...} 4.0.30319.42000 3.0   2.3      Microsoft Windows 10 Pro
Set-HVCIOptions             1.0         {1.0, 2.0, 3.0, 4.0...} 4.0.30319.42000 3.0   2.3      Microsoft Windows 10 Pro
...


Get summary cmdlets by cmdlet version
Get-Command | Group-Object Version | Sort-Object Name -Descending | Format-Table -AutoSize


Count Name        Group                                                                                                                               
----- ----        -----                                                                                                                               
   37 4.2.3       {Add-NTFSAccess, Add-NTFSAudit, Clear-NTFSAccess, Clear-NTFSAudit...}                                                               
   33 4.0.6       {Add-AssertionOperator, AfterAll, AfterEach, AfterEachFeature...}                                                                   
  196 3.1.0.0     {ConvertFrom-SddlString, Format-Hex, Get-FileHash, Import-PowerShellDataFile...}                                                    
   97 3.0.0.0     {Add-History, Add-PSSnapin, Clear-History, Connect-PSSession...}                                                                    
...

My last thought on this however, is, why this effort? 但是,我最后的想法是,为什么要这样做?

We know the legacy code will work in later versions and later version specific module/cmdlets/switches will most likely not work natively in legacy OS / PS versions. 我们知道旧版代码将在更高版本中运行,而更高版本特定的模块/ cmdlet /开关很可能在旧版OS / PS版本中无法正常运行。 Well unless you manually, hack at stuff, copy them there or proxy them there using remoting, doing stuff like the below. 好吧,除非您手动操作,破解,将其复制到那里或使用远程代理将其复制到那里,然后执行以下操作。

 Use using current version cmdlets while on a legacy OS, i.e, Win7SP1 needing to use Test-NetConnection

$RemotePoSHModuleUNCFolder = '\\WS2012r2\$C\Windows\System32\WindowsPowerShell\v1.0\Modules'
$LocalhostPoSHModuleUNCFolder = 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules'
'NetTCPIP','DnsClient','NetSecurity' `
| % {Copy-Item -Path $RemotePoSHModuleUNCFolder\$_ -Destination $LocalhostPoSHModuleUNCFolder}

'NetTCPIP','DnsClient','NetSecurity' `
| % {Import-Module -Name "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\$_" -Verbose}

# Use cmdlet proxy, import module from remote machine WS201R2
$W2K12RemoteSession = New-PSSession -ComputerName 'WS2K12'
Import-Module NetTCPIP -PSSession $W2K12RemoteSession

With the MS announced depreciation of v2, and v2 having issues if .Net 4x is on the host as per this: 随着MS宣布v2贬值,并且如果按以下方式在主机上安装.Net 4x,则v2会出现问题:

"Using PowerShell 2.0 with .NET Framework 4.0 is not supported. This is due to some changes in the runtime activation policy of CLR 4 which prevent applications built against CLR 2 from automatically rolling forward to CLR 4." “不支持将PowerShell 2.0与.NET Framework 4.0一起使用。这是由于CLR 4的运行时激活策略发生了一些更改,这些更改会阻止针对CLR 2构建的应用程序自动前滚到CLR 4。

With further detail here: https://msdn.microsoft.com/en-us/magazine/ee819091.aspx 此处有更多详细信息: https : //msdn.microsoft.com/en-us/magazine/ee819091.aspx

Unless you have legacy OS's with V2 and below, I am not sure what comparing for would buy you. 除非您使用的是V2及以下版本的旧版操作系统,否则我不确定是否可以与您相比。

But you post sounds like a good idea for an addition to PSScriptAnalyzer. 但是您发布声音对于PSScriptAnalyzer来说是个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM