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抛出错误
但是,如果我尝试使用这个导入Import-Module
,这些会产生什么影响吗?该#Requires
文件只是说"剧本",但它没有说明脚本模块是否算作"脚本"或不在这里.如果不是,我该怎么办呢?
不,通过调用执行脚本#Requires
时不会处理注释.psm1
Import-Module
如果我们在这个问题的脚本保存既MyWebApp.psm1
和MyWebApp.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
会在psm1
和psd1
文件中隐式导出.这通常是通过控制Export-ModuleMember -Function
在一个psm1
文件; 模块清单中的等价物是FunctionsToExport
.我觉得它更简单,只是省略FunctionsToExport
从清单和控制什么用导出Export-ModuleMember
的psm1
脚本.
归档时间: |
|
查看次数: |
2763 次 |
最近记录: |