我想加载 config.xml 文件的内容并将其存储在$PrivateData
当我的模块加载时。这是我的 PSD1 中的定义行
# Private data to pass to the module specified in ModuleToProcess
PrivateData = @{'Variables'=@{};'Config'=$null}
这将创建一个包含两个项目的哈希表。 1)Variables
是我用来存储模块私有变量的第二个哈希表。 2) Config 将包含 config.xml 文件的值。 XML 示例:
<Config>
<Foo>Bar</Foo>
</Config>
我可以使用以下行加载 xml:
$PrivateData = $MyInvocation.MyCommand.Module.PrivateData
$PrivateData.Config = ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config
我似乎无法在 PSM1 文件中访问它。我可以将它包装在 Cmdlet 中,如下所示:
Function Initialize-TestModule {
$PrivateData = $MyInvocation.MyCommand.Module.PrivateData
$PrivateData.Config #= ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config
}
但随后用户必须拨打电话Import-Module
然后第二次致电Initialize-TestModule
这是我试图避免的。
如果我将代码放入 PSM1 中,当我调用时它会生成此错误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:
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 中尝试拨打电话Initialize-TestModule
using Invoke-Command
and Start-Job
两者都失败了。那么有没有人在期间设法访问 $PrivateDataImport-Module
?