在导入模块期间访问 PrivateData [英] Accessing PrivateData during Import-Module
问题描述
我想在加载模块时加载 config.xml 文件的内容并将其存储在 $PrivateData
中.这是我的 PSD1 中的定义行
I want to load the contents of a config.xml file and store it in $PrivateData
when my module loads. Here is the definition line in my PSD1
# Private data to pass to the module specified in ModuleToProcess
PrivateData = @{'Variables'=@{};'Config'=$null}
这将创建一个包含两个项目的哈希表.1) Variables
是我用来存储模块私有变量的第二个哈希表.2) Config 将包含 config.xml 文件的值.示例 XML:
This creates a hashtable with two items. 1) Variables
is a second hashtable I use to store private variables for my module. 2) Config which will contain the values of a config.xml file. Example XML:
<Config>
<Foo>Bar</Foo>
</Config>
我可以使用以下行加载 xml:
I can load the xml with the following line:
$PrivateData = $MyInvocation.MyCommand.Module.PrivateData
$PrivateData.Config = ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config
我似乎无法在我的 PSM1 文件中访问它.我可以像这样将它包装在 Cmdlet 中:
It does not appear that I can access it in my PSM1 file. I CAN wrap it in a Cmdlet like so:
Function Initialize-TestModule {
$PrivateData = $MyInvocation.MyCommand.Module.PrivateData
$PrivateData.Config #= ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config
}
但随后用户将不得不调用 Import-Module
,然后再次调用 Initialize-TestModule
,这是我试图避免的.
But then the user would have to make a call to Import-Module
and then a second call to Initialize-TestModule
which is what I am trying to avoid.
如果我将代码放在 PSM1 中,它会在我调用 Import-Module
If I put the code in the PSM1 it generates this error when I call Import-Module
Property 'Config' cannot be found on this object; make sure it exists and is settable.
At C:\scripts\temp\TestModule\TestModule.psm1:7 char:2
+ $PrivateData.Config = ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
如果我尝试像这样加载 PSD1:
If I try to load in the PSD1 like this:
PrivateData = @{'Variables'=@{};'Config'=([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config}
我收到这些错误:
Import-Module : The module manifest 'C:\scripts\temp\TestModule\TestModule.psd1' could not be processed because it is
not a valid Windows PowerShell restricted language file. Please remove the elements that are not permitted by the
restricted language:
At C:\scripts\temp\TestModule\TestModule.psd1:88 char:26
+ PrivateData = @{'Config'=([xml](Get-Content $PSScriptRoot\Config.xml | Out-Strin ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Property references are not allowed in restricted language mode or a Data section.
At C:\scripts\temp\TestModule\TestModule.psd1:88 char:27
+ PrivateData = @{'Config'=([xml](Get-Content $PSScriptRoot\Config.xml | Out-Strin ...
+ ~~~~~
The type xml is not allowed in restricted language mode or a Data section.
At C:\scripts\temp\TestModule\TestModule.psd1:88 char:33
+ PrivateData = @{'Config'=([xml](Get-Content $PSScriptRoot\Config.xml | Out-Strin ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The command 'Get-Content' is not allowed in restricted language mode or a Data section.
At C:\scripts\temp\TestModule\TestModule.psd1:88 char:72
+ PrivateData = @{'Config'=([xml](Get-Content $PSScriptRoot\Config.xml | Out-Strin ...
+ ~~~~~~~~~
The command 'Out-String' is not allowed in restricted language mode or a Data section.
At line:1 char:1
+ Import-Module .\TestModule -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (C:\scripts\temp...TestModule.psd1:String) [Import-Module], Missing
MemberException
+ FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand
在我的 PSM1 中尝试使用 Invoke-Command
和 Start-Job
调用 Initialize-TestModule
,但都失败了.那么有没有人在 Import-Module
期间设法访问 $PrivateData?
In my PSM1 have tried making a call to Initialize-TestModule
using Invoke-Command
and Start-Job
both of which failed. So has anyone managed to access $PrivateData during Import-Module
?
推荐答案
您可能需要使用 $MyInvocation 变量访问私有数据.但是,我只是通过从函数中调用它来使其工作.要将其加载到 PSM1 文件中的变量,我从那里调用该函数.我从 https://social.technet.microsoft.com/Forums/windowsserver/en-US/9620af9a-0323-460c-b3e8-68a73715f99d/module-scoped-variable?forum=winserverpowershell.
You will likely need to access the private data using the $MyInvocation variable. However, I've only gotten it to work by calling it from within a function. To load it to a variable in the PSM1 file I call the function from there. I found out about this from https://social.technet.microsoft.com/Forums/windowsserver/en-US/9620af9a-0323-460c-b3e8-68a73715f99d/module-scoped-variable?forum=winserverpowershell.
function Get-PD
{
[CmdletBinding()]
Param()
Begin{}
Process
{
$MyInvocation.MyCommand.Module.PrivateData
}
End{}
}
$MyPD = Get-PD
这篇关于在导入模块期间访问 PrivateData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!