[英]Can we check for needed Powershell/WPF/.Net Version for Powershell Scripts?
我希望能夠看到我的腳本與較早版本的軟件兼容的程度,因此,我想了解腳本需要運行的最低Powershell,.Net和WPF版本。 如果我知道如何檢索有關每個.Net類和PowerShell cmdlet的信息以及它們引入的版本,我想我將能夠完成此任務。
編輯:
我可能還不太清楚,所以這里有一些在線手動找到的例子。 但是,我希望能夠通過PowerShell獲得此信息。 但是正如Ansgar Wiechers所說,此信息可能不太容易獲得。 我只是希望有人可能已經做到了,或者知道如何獲取這些信息。
例如。
[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-自Powershell 3.0起可用(在第一段中找到https://blogs.msdn.microsoft.com/powershell/2012/08/24/introduction-to-cim-cmdlets/ )
根據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/zh-cn/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 /開關很可能在舊版OS / 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來說是個好主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.