Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing PrivateData during Import-Module

Tags:

powershell

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}

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>

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

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    
}

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.

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

If I try to load in the PSD1 like this:

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

I get these errors:

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

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?

like image 236
sourcecode.ws Avatar asked Mar 08 '14 12:03

sourcecode.ws


People also ask

How do I Unimport a PowerShell module?

The Remove-Module cmdlet removes the members of a module, such as cmdlets and functions, from the current session. If the module includes an assembly ( . dll ), all members that are implemented by the assembly are removed, but the assembly is not unloaded.

How do I view PowerShell modules?

The Get-InstalledModule cmdlet gets PowerShell modules that are installed on a computer using PowerShellGet. To see all modules installed on the system, use the Get-Module -ListAvailable command.

What is psd1 and psm1?

.PS1 – Windows PowerShell shell script. .PSD1 – Windows PowerShell data file (for Version 2) .PSM1 – Windows PowerShell module file (for Version 2) .PS1XML – Windows PowerShell format and type definitions. .CLIXML - Windows PowerShell serialized data.


1 Answers

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
like image 110
Juan Perez Avatar answered Nov 04 '22 09:11

Juan Perez