Dweeberly
Dweeberly

Reputation: 4777

PowerShell variables available in psd1 files

PowerShell PSD1 files are constrained to "non-dynamic" behavior. However, I've noticed that there are a few automatic variables that can be used. Namely, $null, $true and $false (effectively constants). Are their any others? Is there documentation as to exactly what PSD1 files can contain. I understand the content is one big hashtable of static PS types, but what are the "rules" that define what is "static" (or constant) with respect to these files. There is a long list of automatic variables here: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_automatic_variables but most do not work in the PSD1 context. For example why would something like $PSHOME or $PID be considered "dynamic"?

Consider:

PS> get-variable true|select *
Name        : true
Description : Boolean True
Value       : True
Visibility  : Public
Module      :
ModuleName  :
Options     : Constant, AllScope
Attributes  : {}    

PS> get-variable pid|select *
Name        : PID
Description : Current process ID
Value       : 5056
Visibility  : Public
Module      :
ModuleName  :
Options     : Constant, AllScope
Attributes  : {}

They look pretty much the same, both are marked as "Constant" but one I can use in a PSD1 file and the other I can't.

Upvotes: 2

Views: 2604

Answers (2)

briantist
briantist

Reputation: 47802

PowerShell Module Manifests are actually executable files that are run in a RestrictedLanguage Language Mode.

In RestrictedLanguage language mode, users may run commands (cmdlets, functions, CIM commands, and workflows) but are not permitted to use script blocks.

Only the following variables are permitted:

  • $PSCulture
  • $PSUICulture
  • $True
  • $False
  • $Null.

Only the following comparison operators are permitted:

  • -eq (equal)
  • -gt (greater-than)
  • -lt (less-than)

Assignment statements, property references, and method calls are not permitted.

So that's where $true, $false, and $null come from. What is allowed really has nothing to do with it being static or constant or read-only.

You can also use $PSScriptRoot (referring to the module's directory), and as of version 5.1 you can use $PSEdition which lets you determine whether you're on Desktop (Windows PowerShell) or Core (cross-platform). You can see in the linked example that language elements are being used (if statements).

You can see this happening in PowerShell's source code. See the LoadModuleManifestData that follows that line to see where it first checks that the manifest contents (scriptblock) conforms to restricted language mode, then where it loads the special variables and finally executes the contents.

Upvotes: 4

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

Module Manifests

A module manifest is a .psd1 file that contains a hash table. The keys and values in the hash table do the following things:

  • Describe the contents and attributes of the module.
  • Define the prerequisites.
  • Determine how the components are processed.

Manifests are not required for a module. Modules can reference script files (.ps1), script module files (.psm1), manifest files (.psd1), formatting and type files (.ps1xml), cmdlet and provider assemblies (.dll), resource files, Help files, localization files, or any other type of file or resource that is bundled as part of the module. For an internationalized script, the module folder also contains a set of message catalog files. If you add a manifest file to the module folder, you can reference the multiple files as a single unit by referencing the manifest.

The manifest itself describes the following categories of information:

  • Metadata about the module, such as the module version number, the author, and the description.
  • Prerequisites needed to import the module, such as the Windows PowerShell version, the common language runtime (CLR) version, and the required modules.
  • Processing directives, such as the scripts, formats, and types to process.
  • Restrictions on the members of the module to export, such as the aliases, functions, variables, and cmdlets to export.

For more information, see How to Write a PowerShell Module Manifest.

Source

Upvotes: 0

Related Questions