我在电脑上同时安装了Powershell 5.1和Powershell 7.0,现在我想要运行一个必须在Powershell 7.0中运行的脚本。我该如何让脚本在特定版本中运行?
我在电脑上同时安装了Powershell 5.1和Powershell 7.0,现在我想要运行一个必须在Powershell 7.0中运行的脚本。我该如何让脚本在特定版本中运行?
#Requires -Version 7.0
这是来自Microsoft支持的参考资料。
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires
该链接是关于在PowerShell中使用“requires”语句的说明。如果脚本是在PS shell中被调用的,如果不是正确版本,你可以重新启动它。
# At beginning of .ps1
if ($PSVersionTable.PSVersion -ne [Version]"5.1") {
# Re-launch as version 5 if we're not already
powershell -Version 5.1 -File $MyInvocation.MyCommand.Definition
exit
}
# Your script code
Requires
指令 可以防止在较低版本的 PowerShell 中运行脚本。 - AKd