简短说明
防止脚本在缺少必要元素的情况下运行。
详细说明
除非满足 PowerShell 版本、模块(和版本)或管理单元(和版本)和版本先决条件,否则该 #Requires 语句会阻止脚本运行。 如果未满足先决条件,PowerShell 不会运行脚本或提供其他运行时功能,例如选项卡完成。
语法
#Requires -Version <N>[.<n>]
#Requires -Modules { <Module-Name> | <Hashtable> }
#Requires -PSEdition <PSEdition-Name>
#Requires -RunAsAdministrator
有关语法的详细信息,请参阅 ScriptRequirements。
使用规则
脚本可以包含多个 #Requires 语句。
#Requires 语句可以出现在脚本中的任何行上。
将 #Requires 语句置于函数内不会限制其范围。 所有 #Requires 语句始终全局应用,并且必须满足这些语句才能执行脚本。
警告
即使 #Requires 语句可以出现在脚本中的任何一行上,但其在脚本中的位置不会影响其应用程序的序列。 在脚本执行之前,必须满足 #Requires 语句呈现的全局状态。
示例:
Get-Module AzureRM.Netcore | Remove-Module
#Requires -Modules AzureRM.Netcore
你可能会认为,上述代码不应运行,因为在 #Requires 语句之前删除了所需的模块。 但是,在脚本甚至可以执行之前,必须满足 #Requires 状态。 然后,脚本的第一行将所需状态失效。
参数
-Assembly <程序集路径> |<.NET 程序集规范>
重要
-Assembly 语法已弃用。 它不提供任何函数。 语法已添加到 PowerShell 5.1 中,但从未实现支持代码。 仍接受语法以实现向后兼容性。
指定程序集 DLL 文件或 .NET 程序集名称的路径。 PowerShell 5.0 中引入了 程序集 参数。 有关 .NET 程序集的详细信息,请参阅 程序集名称。
例如:
#Requires -Assembly path\to\foo.dll
#Requires -Assembly "System.Management.Automation, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
-版本 <N>[.<n>]
指定脚本所需的最低 PowerShell 版本。 输入主版本号和可选的次要版本号。
例如:
#Requires -Version 6.0
-Modules <Module-Name> |<哈希表>
指定脚本所需的 PowerShell 模块。 输入模块名称和可选版本号。
如果所需的模块不在当前会话中,PowerShell 会导入它们。 如果无法导入模块,PowerShell 将引发终止错误。
#Requires 语句不会加载模块中的类和枚举定义。 使用脚本开头的 using module 语句导入模块,包括类和枚举定义。 有关详细信息,请参阅 about_Using。
对于每个模块,请键入模块名称(<字符串>)或哈希表。 该值可以是字符串和哈希表的组合。 哈希表具有以下键。
-
ModuleName- 必需 指定模块名称。 -
GUID- 可选 指定模块的 GUID。 - 它还 必需 指定以下三个键中的至少一个。
-
ModuleVersion- 指定模块的最低可接受版本。 -
MaximumVersion- 指定模块的最大可接受版本。 -
RequiredVersion- 指定模块的确切所需版本。 这不能与其他版本密钥一起使用。
-
注释
windows PowerShell 5.0 中添加了 RequiredVersion。
Windows PowerShell 5.1 中添加了 MaximumVersion。
例如:
要求安装 AzureRM.Netcore(版本 0.12.0 或更高版本)。
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; ModuleVersion="0.12.0" }
要求安装 AzureRM.Netcore(仅。
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; RequiredVersion="0.12.0" }
要求安装 AzureRM.Netcore(版本 0.12.0 或更低版本)。
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; MaximumVersion="0.12.0" }
要求安装任何版本的 AzureRM.Netcore 和 PowerShellGet。
#Requires -Modules AzureRM.Netcore, PowerShellGet
使用 RequiredVersion 键时,请确保版本字符串与所需的版本字符串完全匹配。
Get-Module AzureRM.Netcore -ListAvailable
Directory: /home/azureuser/.local/share/powershell/Modules
ModuleType Version Name PSEdition ExportedCommands
---------- ------- ---- --------- ----------------
Script 0.12.0 AzureRM.Netcore Core
下面的示例失败,因为 0.12 与 0.12.0不匹配。
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; RequiredVersion="0.12" }
-PSEdition <PSEdition-名称>
指定脚本所需的 PowerShell 版本。 有效值为 PowerShell Core,以及适用于 Windows PowerShell 的桌面。
例如:
#Requires -PSEdition Core
-RunAs管理员
将此 switch 参数添加到 #Requires 语句时,它指定运行脚本的 PowerShell 会话必须以提升的用户权限启动。 非 Windows作系统上忽略 RunAsAdministrator 参数。 PowerShell 4.0 中引入了 RunAsAdministrator 参数。
例如:
#Requires -RunAsAdministrator
例子
以下脚本有两个 #Requires 语句。 如果未满足这两个语句中指定的要求,则脚本不会运行。 每个 #Requires 语句必须是行上的第一项:
#Requires -Modules AzureRM.Netcore
#Requires -Version 6.0
Param
(
[parameter(Mandatory=$true)]
[String[]]
$Path
)
...