我将 #Requires -Version
放在脚本的顶部,但需要弄清楚我需要什么版本。我希望查询哪个版本的 PowerShell 引入了我调用的每个 cmdlet。但我在其中一个 cmdlet 的 Get-Help -verbose
输出中看不到这一点。我没有找到它的规范网页列表。
任何人都知道是否有一种标准方法来查找哪个版本的 PowerShell 引入了特定的 cmdlet?或者,有没有更好的方法来完成我想做的事情?
最佳答案
因此,据我所知,查找此内容的“标准”方式是阅读 MSDN。 :-) 您可以使用 Get-Help
的 -Online
开关轻松访问相关页面,例如:
Get-Help -Name "Get-DscConfiguration" -Online
另一种方法可能是使用 -Version
开关启动 powershell.exe 以设置特定版本,例如powershell.exe -Version 2 然后使用 Get-Command
cmdlet 查看您的 cmdlet 是否已列出。
我玩得很开心!这是一些似乎可以正常工作的代码,可以解析脚本,然后确定命令是否是不同 PS 版本中的有效 cmdlet。此时,Start-Job
上的 -PSVersion
开关似乎不支持“1.0”或“5.0”。
param(
$file = 'C:\scripts\PowerShell\Toolkit\Get-PSVersionCompatibility.ps1'
)
New-Variable tokens
New-Variable parseerrors
$p = [System.Management.Automation.Language.Parser]::ParseFile($file,[ref]$tokens,[ref]$parseerrors)
$Commands = $tokens | ?{$_.TokenFlags -contains "CommandName"} | Sort -Unique | Select Value
$ScriptBlock = {
param($PSVersion,$Commands)
$Output = New-Object -TypeName PSObject -Property @{PSVersion = $PSVersion}
foreach($Command in $Commands) {
if([String]::IsNullOrEmpty($Command.Value)){continue}
if(Get-Command | ?{$_.Name -eq $Command.Value}) {
$Available = $true
} else {
$Available = $false
}
$Output | Add-Member -MemberType NoteProperty -Name $($Command.Value) -Value $Available
}
return $Output
}
$Results = @()
foreach($PSVersion in 2..4) {
$job = Start-Job -PSVersion "$PSVersion.0" -ScriptBlock $ScriptBlock -ArgumentList $PSVersion,$Commands
Wait-Job $job | Out-Null
$Results += (Receive-Job $job | Select PSVersion,*-*)
Remove-Job $job
}
$Results | FT -AutoSize
Remove-Variable tokens
Remove-Variable parseerrors
关于powershell - 哪个 PowerShell 版本引入了给定的 cmdlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36190375/