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

标签 powershell powershell-cmdlet

我将 #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/

相关文章:

c# - 检测 PowerShell 开关

date - Powershell 中的 get-counter/export-counter 返回的时间格式错误

c# - 如何在运行时从模块代码内部获取 PowerShell 模块版本

java - Powershell 打开窗口(来自 Java.Runtime.exec)

windows - 如何使用类似 forth 的交换操作来操纵 PowerShell 的位置堆栈?

c# - 在执行 C# 自定义 Cmdlet 期间收集用户输入(不通过参数)

c# - 我可以使用 .NET Core 编写 PowerShell 二进制 cmdlet 吗?

powershell - 在本例中,如何通过powershell >> if(Test-Path%Program Files(x86)%\Google)正确使用环境变量?

json - 如何获取 PSCustomobject 的长度?

powershell - New-SelfSignedCertificate -CertStoreLocation 找不到路径