在导入模块期间访问PrivateData

sou*_*.ws 6 powershell

我想加载config.xml文件的内容并在$PrivateData我的模块加载时存储它.这是我的PSD1中的定义行

# Private data to pass to the module specified in ModuleToProcess
PrivateData = @{'Variables'=@{};'Config'=$null}
Run Code Online (Sandbox Code Playgroud)

这将创建一个包含两个项目的哈希表.1)Variables是我用来为我的模块存储私有变量的第二个哈希表.2)Config将包含config.xml文件的值.示例XML:

<Config>
    <Foo>Bar</Foo>
</Config>
Run Code Online (Sandbox Code Playgroud)

我可以使用以下行加载xml:

$PrivateData  = $MyInvocation.MyCommand.Module.PrivateData
$PrivateData.Config = ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config
Run Code Online (Sandbox Code Playgroud)

我似乎无法在PSM1文件中访问它.我可以将它包装在Cmdlet中,如下所示:

Function Initialize-TestModule {
    $PrivateData  = $MyInvocation.MyCommand.Module.PrivateData
    $PrivateData.Config #= ([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config    
}
Run Code Online (Sandbox Code Playgroud)

但是那时用户将不得不拨打电话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
Run Code Online (Sandbox Code Playgroud)

如果我尝试像这样加载PSD1:

PrivateData = @{'Variables'=@{};'Config'=([xml](Get-Content $PSScriptRoot\Config.xml | Out-String)).Config}
Run Code Online (Sandbox Code Playgroud)

我收到这些错误:

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
Run Code Online (Sandbox Code Playgroud)

在我的PSM1尝试打电话Initialize-TestModule使用Invoke-Command,Start-Job两者都失败了.那么有没有人设法访问$ PrivateData期间Import-Module

小智 6

您可能需要使用 $MyInvocation 变量访问私有数据。但是,我只是通过从函数中调用它来使其工作。要将其加载到 PSM1 文件中的变量,我从那里调用该函数。我从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
Run Code Online (Sandbox Code Playgroud)