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
?
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.
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.
.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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With