Conditional statements in module manifests

I would like a module manifest file to reference one set of modules in my dev environment and another in my prod environment. The documentation says that if statements are permitted in psd1 files, but after a bunch of attempts I can’t make it work.

I’d like to do something like this:

if ($env:devOrProd -eq 'dev') {
    RootModule = '.\AD_Operations.psm1'
}
else {
    RootModule ='\\prodShare\sharedlibraries\AD_Operations.psm1'
}

Is this doable?

Jon,
Welcome to the forum.

Could you please share a link to the documentation you’re refering to? As far as I remember are psd1 files PowerShell data files without any active code.

You could use a script to actually create a module manifest using the cmdlet New-ModuleManifest. This way you could create 2 different manifests for your different environments.

Thanks for responding. Here’s the doc I was referencing:

  1. To address any scenarios that might not be covered by the base module manifest elements, you have the option to add additional code to your module manifest.
    For security concerns, PowerShell only runs a small subset of the available operations in a module manifest file. Generally, you can use the if statement, arithmetic and comparison operators, and the basic PowerShell data types.

Wow, cool. So I’ve learned something today - thanks for that. :wink: But unfortunately for you I’ve never used it myself yet.

So the anser to your initial question …

should actually be “yes”.

What does

actually mean? Do you get error messages? Or what does not work as expected?

I get an error message if I try to import my psd1. For example, when I write it like this:

@{
# Script module or binary module file associated with this manifest.
if ($env:devOrProd -eq 'dev') {
    RootModule = '.\AD_Operations.psm1'
}
else {
    RootModule ='\\prodShare\sharedlibraries\AD_Operations.psm1'
}
ModuleVersion = '3.0'

It tells me that assignment statements are not permitted in restricted language mode or in the data section.

If I use:

@{

# Script module or binary module file associated with this manifest.

RootModule = {
    if ($env:devOrProd -eq 'dev') {
        '.\AD_Operations.psm1'
    }
    else {
        '\\prodShare\sharedlibraries\AD_Operations.psm1'
    }
}
ModuleVersion = '3.0'

It says that script block literals aren’t permitted.

These errors make sense to me, but I’m having trouble reconciling them with the documentation about the if statement.

Would say that tying to point the root structure or psm1 path from root of the module is a bit unorthodox. Never seen a module that would reference a remote location and security most likely would not allow it. The module can be placed in a remote UNC share and modules can be imported via UNC, but you’ll see in this blog there are some security settings that need to be accounted for.

Defining a repository is a better approach:

Creating Your Private PowerShellGet Repository - Power Tips - Power Tips - IDERA Community

or in Azure:

Creating a Private PSRepository in Azure | How (pipehow.tech)

Try a subexpression instead of a scriptblock.

RootModule = $(
    If(…){
        …
    }else{
        …
    }
)
1 Like

That did it, Doug! Thank you!

1 Like

Hi Rob,

In my case the SharedLibraries share is in the Trusted Zones of all the servers that should need to use it. But I’m intrigued by the idea of a private repo and will explore that further.

Thanks for the tip!

jon