哪个 PowerShell 版本引入了给定的 cmdlet?

What PowerShell version introduced a given cmdlet?

我将 #Requires -Version 放在脚本的顶部,但需要弄清楚我需要什么版本。我希望查询哪个版本的 PowerShell 引入了我调用的每个 cmdlet。但我在其中一个 cmdlet 的 Get-Help -verbose 输出中没有看到这一点。我没有找到它的规范网页列表。

任何人都知道是否有标准方法来查找哪个版本的 PowerShell 引入了特定的 cmdlet?或者,是否有更好的方法来完成我想要做的事情?

因此,据我所知,"standard" 查找此内容的方法是阅读 MSDN。 :-) 您可以使用 Get-Help-Online 开关很容易地到达相关页面,例如:

Get-Help -Name "Get-DscConfiguration" -Online

另一种方法可能是使用 -Version 开关启动 powershell.exe 以设置特定版本,例如powershell.exe - 版本 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