#Requires在模块脚本中有效吗?

jpm*_*c26 6 powershell powershell-module

考虑以下模块脚本:

MyWebApp.psm1:

#Requires -Version 4
#Requires -Modules WebAdministration

function Test-MyWebApp() {
    return ((Get-WebApplication 'myapp') -ne $null)
}
Run Code Online (Sandbox Code Playgroud)

(Export-ModuleMember为简单起见省略.脚本仍然可以作为没有它的模块.)

如果这是一个ps1脚本,#Requires顶部的注释会强制PowerShell抛出错误

  • 版本低于4
  • 无法加载(导入)WebAdministration模块

但是,如果我尝试使用这个导入Import-Module,这些会产生什么影响吗?#Requires文件只是说"剧本",但它没有说明脚本模块是否算作"脚本"或不在这里.如果不是,我该怎么办呢?

jpm*_*c26 9

不,它被视为正常评论

不,通过调用执行脚本#Requires时不会处理注释.psm1Import-Module

如果我们在这个问题的脚本保存既MyWebApp.psm1MyWebApp.ps1缺乏的一台机器上的WebAdministration模块,我们可以得到以下结果:

PS> .\MyWebApp.ps1
.\MyWebApp.ps1 : The script 'MyWebApp.ps1' cannot be run because the following modules that are specified by the
"#requires" statements of the script are missing: WebAdministration.
At line:1 char:1
+ .\MyWebApp.ps1
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (MyWebApp.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresMissingModules

PS> Import-Module .\MyWebApp.psm1
PS>
Run Code Online (Sandbox Code Playgroud)

导入模块成功,该功能现在存在于当前范围内.但该功能将失败:

PS> Test-MyWebApp
Get-WebApplication : The term 'Get-WebApplication' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At .\MyWebApp.psm1:5 char:14
+     return ((Get-WebApplication 'myapp') -Eq $null)
+              ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-WebApplication:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

甚至-Version支票也被忽略了.如果我们在只有PowerShell 4的机器上将其提升到5:

PS> .\MyWebApp.ps1
.\MyWebApp.ps1 : The script 'MyWebApp.ps1' cannot be run because it contained a "#requires" statement for Windows
PowerShell 5.0. The version of Windows PowerShell that is required by the script does not match the currently running
version of Windows PowerShell 4.0.
At line:1 char:1
+ .\MyWebApp.ps1
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (MyWebApp.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion

PS> Import-Module .\MyWebApp.psm1
PS>
Run Code Online (Sandbox Code Playgroud)

使用模块清单

正确验证要求的唯一方法是使用模块清单.不幸的是,这必须是文件旁边的单独psm1文件.以下清单将实现#Requires评论的目的:

MyWebApp.psd1:

#
# Module manifest for module 'MyWebApp'
#

@{
ModuleVersion = '1.0'
PowerShellVersion = '4.0'
RequiredModules = @('WebAdministration')
RootModule = @('.\MyWebApp.psm1')
}
Run Code Online (Sandbox Code Playgroud)

导入此文件会出现我们想要的错误:

PS> Import-Module .\MyWebApp.psd1
Import-Module : The required module 'WebAdministration' is not loaded. Load the module or remove the module from 'RequiredModules' in the file 
'.\MyWebApp.psd1'.
At line:1 char:1
+ Import-Module .\MyWebApp.psd1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (.\MyWebApp.psd1:String) [Import-Module], MissingMemberException
    + FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand
Run Code Online (Sandbox Code Playgroud)

不幸的是,您无法在同一文件中声明该函数.您必须使用单独的 psd1文件,然后将原始psm1脚本显式声明为"根模块".根模块表示为相对于文件的路径psd1.

其他属性:

  • ModuleVersion是必需的.它必须存在.
  • PowerShellVersion完成了什么#Requires -Version 4意图.
  • RequiredModules完成了什么#Requires -Modules WebAdministration意图.

请注意,它Test-MyWebApp会在psm1psd1文件中隐式导出.这通常是通过控制Export-ModuleMember -Function在一个psm1文件; 模块清单中的等价物是FunctionsToExport.我觉得它更简单,只是省略FunctionsToExport从清单和控制什么用导出Export-ModuleMemberpsm1脚本.