【问题标题】:Can we check for needed Powershell/WPF/.Net Version for Powershell Scripts?我们可以检查 Powershell 脚本所需的 Powershell/WPF/.Net 版本吗?
【发布时间】:2018-06-10 16:29:06
【问题描述】:

我希望能够了解我的脚本与早期版本的软件兼容的时间有多长,所以我想看看我的脚本运行所需的 Powershell、.Net 和 WPF 的最低版本是多少。 如果我知道如何检索有关每个 .Net 类和 PowerShell cmdlet 的信息以及它们被引入的版本,我想我将能够做到这一点。

编辑:

我可能还不够清楚,所以这里有一些在网上手动找到的示例。但是,我希望能够通过 PowerShell 获取此信息。但正如 Ansgar Wiechers 所说,这些信息可能不容易获得。我只是希望有人可能已经这样做或知道如何获取该信息。

例如。

[System.Net.NetworkInformation.Ping] - 自 .Net 2.0 起可用(位于 https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(v=vs.110).aspx#Anchor_8

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

【问题讨论】:

  • 我不认为您正在寻找的信息是现成的。
  • 只有你有能力解析脚本并在代码中进行彻底的分析,这是可能的,但很困难。
  • 对,如果我在 cmdlet 和 .net 类中找到最低版本(版本中引入)的位置,我会尝试这样做

标签: .net wpf powershell version .net-framework-version


【解决方案1】:

根据 OP 的说明编辑

如果您正在寻找链接中提到的每个 WMF 版本发布的 cmdlet...

'自 .Net 2.0 起可用' '从 Powershell 3.0 开始可用'

...那么,TechNet 在这里提供:

PowerShell 1.0 Cmdlet 这些是可在 Windows PowerShell 1.0 中使用的 cmdlet 列表。 https://social.technet.microsoft.com/wiki/contents/articles/13769.powershell-1-0-cmdlets.aspx

PowerShell 2.0 Cmdlet 这些是可在 Windows PowerShell 2.0 中使用的 cmdlet 列表。 https://social.technet.microsoft.com/wiki/contents/articles/13876.powershell-2-0-cmdlets.aspx

PowerShell 3.0 Cmdlet 这些是 Windows 8 Developer Preview 中可用的 cmdlet、别名和函数。
https://social.technet.microsoft.com/wiki/contents/articles/4694.powershell-3-cmdlets.aspx

请注意,我没有找到类似上述 v4 - v6 (PowerShellCore) 的列表。

但是有这样的: 本主题列出了 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

考虑到你的追求。如果这是我。我开始的方法是使用适当的操作系统建立一组原始的 VM 客户端(或交给我们知道谁拥有它们的人),安装用于操作系统的 RTM WMF 版本并运行以下内容,保存到文件。

然后将该文件用作与代码进行比较的基础。意思是,在我的所有模块、函数和脚本上使用 Select-String 并匹配 cmdlet 名称以将比较文件中的其他属性获取到代码比较报告中。

任何 cmdlet 名称匹配,都可能是代码扫描的基线指示符,其中包含来自特定 PoSH 版本的 cmdlet。因此假设扫描的代码是在 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...}                                                                    
...

然而,我对此的最后一个想法是,为什么要这样做?

我们知道旧代码将在更高版本中运行,而更高版本的特定模块/cmdlet/开关很可能无法在旧操作系统/PS 版本中本地运行。好吧,除非您手动破解内容,将它们复制到那里或使用远程处理将它们代理到那里,执行如下操作。

 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

MS 宣布 v2 折旧,如果 .Net 4x 在主机上,v2 会出现问题:

“不支持将 PowerShell 2.0 与 .NET Framework 4.0 一起使用。这是由于 CLR 4 的运行时激活策略发生了一些变化,这会阻止针对 CLR 2 构建的应用程序自动前滚到 CLR 4。”

这里有更多详细信息:https://msdn.microsoft.com/en-us/magazine/ee819091.aspx

除非您使用 V2 及以下版本的旧版操作系统,否则我不确定比较什么会为您买单。

但是您发布的内容听起来像是一个添加到 PSScriptAnalyzer 的好主意。

【讨论】:

  • 感谢您的努力。然而,这不是我想要的。我已经用示例编辑了我的原始帖子。
猜你喜欢
  • 2015-11-10
  • 2018-11-17
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多