我把#Requires -Version
我的脚本放在顶部,但需要弄清楚我需要什么版本.我希望查询PowerShell的哪个版本引入了我调用的每个cmdlet.但我没有在Get-Help -verbose
其中一个cmdlet 的输出中看到它.我没有找到适合它的规范网页列表.
任何人都知道是否有标准的方法来查找哪个版本的PowerShell引入了特定的cmdlet?或者,有没有更好的方法来完成我想要做的事情?
所以,据我所知,查看它的"标准"方法是阅读MSDN.:-)您可以使用-Online
开关轻松访问相关页面Get-Help
,例如:
Get-Help -Name "Get-DscConfiguration" -Online
Run Code Online (Sandbox Code Playgroud)
另一种方法可能是使用开关设置特定版本来启动powershell.exe-Version
,例如powershell.exe -Version 2然后使用Get-Command
cmdlet查看您的cmdlet是否已列出.
我很开心!下面是一些似乎可以正常工作的代码,可以解析脚本,然后确定命令是不同PS版本中的有效cmdlet.此时,-PSVersion
开关似乎不支持"1.0"或"5.0" Start-Job
.
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1180 次 |
最近记录: |