# Variables Variables allow for the global definition of variables that are available within scripts. You can also import secrets that are also available within scripts or as run as credentials. {% hint style="info" %} Variables are stored in the `variables.ps1` configuration file. {% endhint %} ## Creating a Variable To create a variable, navigate to the Platform \ Variables page. Click Create Variable to define a new variable.

Create Variable dialog

Variables will be added to your scripts before they are run. ## Creating a Secret Variable Secret variables are stored within the selected vault. The value of those variables is never stored within Universal. To define a new secret variable, click Create Secret Variable on the variables page and select the Secret tab. From this dialog, you'll be able to define string and PSCredentials in the specified vault.

Secret Variable Dialog

### Credential Format In some environments, it may be necessary to specify the domain name in the user name field. You can specify in either the `domain\user` or `user@domain` format. If you do not do so, you will receive errors when attempting to start processes, like scripts or dashboards, as that user account. ### Group Managed Service Accounts When using Group Managed Service Accounts (GMSA), you will need to ensure that the machine and account has access to the GSMA account. You can follow [Microsoft's guide here](https://techcommunity.microsoft.com/blog/itopstalkblog/step-by-step-how-to-work-with-group-managed-service-accounts-gmsa/329864). Once configuration is complete, create a new credential in PowerShell Universal secret management and select the Password Not Required option. Enter the GSMA account (e.g. domain\user$). As long as the hosting account has the proper privileges, you will be able to run jobs are the GMSA account. ### Credential Validation On Windows, you can validate credentials before saving their value. Click the Validate button on the password field to attempt to login the user locally. This button will not appear on non-Windows systems. ### Roles {% hint style="warning" %} Applying roles to a secret will prevent the secret from being accessible in scheduled jobs because no roles are applied in the scheduler. {% endhint %} Secret variables can include role-based access. Roles limit who can use the secret in their scripts and as run as credentials. When a secret has a role defined, it will not be accessible in resources that cannot provide that role. This can include the following: * Schedule Jobs * Unauthenticated APIs or Apps * Terminals ## Vaults ### Database The database vault stores secrets within the PowerShell Universal database. These secrets are encrypted using AES encryption using a customizable key. You can customize the key by specifying the Secrets \ Database \ EncryptionKey. #### appsettings.json You can configure this setting in appsettings.json. ```json "Secrets": { "Database": { "EncryptionKey": "=b0ywQA@VOSdr&R7an5g&XK6NVO%s4Tf" } } ``` #### Environment Variable You can configure this setting with an environment variable. ```powershell $Env:Secrets__Database__EncryptionKey = "=b0ywQA@VOSdr&R7an5g&XK6NVO%s4Tf" ``` ### BuiltInLocalVault {% hint style="warning" %} If PowerShell Universal is run as a group managed service account, it cannot use the `BuiltInLocalVault`. {% endhint %} Values for secrets with the `BuiltInLocalVault` are stored within the Windows Credential Manager instance of the security principal that is running PSU. For example, the service account of the user running the Universal service. If you change users (such as running as a service account), the account will not have access to the previous user's secrets and you will need to add those secrets again. ### PSUSecretStore {% hint style="warning" %} If PowerShell Universal is run as a group managed service account, it cannot use the `PSUSecretStore`. {% endhint %} The `PSUSecretStore` vault is integrated with the Microsoft `SecretStore` module to store secrets in a cross-platform file. Ths file is tied to the current user account running PowerShell Universal. The password for the vault is stored in `appsettings.json`. ### Az.KeyVault By default, we do not include the Azure Key Vault extension directly in PowerShell Universal As of Powershell Universal v4.0.11, a container including the required Az.Accounts and Az.KeyVault modules is avilable to allow you use KeyVault out of the box. This can be found on [Docker Hub](https://hub.docker.com/r/ironmansoftware/universal/tags?page=1\&name=modules). Below you will find how to configure it. This example uses an Azure hosted web-app version of PowerShell Universal. In Azure, we'll need to configure a managed identity for our web app. This step isn't necessarily required if you are running outside of Azure. You can enable the managed identity under the Identity page for your web app. ![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-7f8eb0eb0f0e81f48693524bd14da274f9ddbaaa%2Fimage%20\(228\).png?alt=media) Next, you'll need to allow your managed identity to access your key vault and read your subscription. You can add the managed identity to the builtin Reader group to allow access to the subscription. ![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-19e52376561e646b0db53784c2b6f114cedd7521%2Fimage%20\(430\).png?alt=media) Then, I provided all privileges to secret and key management for my managed identity in my Key Vault resource. ![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-c5f1d8747a0f8925d0c7614554f176edc5095cb9%2Fimage%20\(457\).png?alt=media) Finally, we will need to register the key vault and connect to Azure when the web app starts. This can be accomplished by using the Az.Account and Az.KeyVault modules. After deploying your web-app, you will need to first install the Az.Account and Az.KeyVault modules. You can do so on the modules page. They will be installed to the local repository within the web app. ![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-fc8462ef6efec860ca72ee9ff48b635f248bd3ae%2Fimage%20\(21\).png?alt=media) Next, create a script within PowerShell Universal to connect to Azure and register the vault. Run the script to verify it works properly. ```powershell $sub = 'affdf0d4-eed5-48a6-889c-599d482xxxxx' Connect-AzAccount -Id -Scope CurrentUser -SubscriptionId $sub Register-SecretVault -ModuleName Az.KeyVault -Name AzureKeyVault -VaultParameters @{ AZKVaultName = 'psu-demo' SubscriptionId = $sub } -AllowClobber ``` Now, when you are creating secrets, you will see the AzureKeyVault available.

Secret Variables Vault Selector

To ensure the application is connected to Azure and the key vault is registered, run the script within `vaults.ps1`. We recommend running the script in an external PowerShell process to avoid assembly conflicts with PowerShell Universal DLLs. ```powershell $pwsh = (Get-Command pwsh).Path & $pwsh -File "$PSScriptRoot\registerVault.ps1" ``` This script runs before variables are registered and vaults are located. You can create this file directly in the Repository\\.universal directory. It will be run during system startup before other configuration scripts are run. ### vaults.ps1 The `vaults.ps1` file contains vault registration commands. While the above example defines an Azure KeyVault secret vault registration, you can actually include any vault registration you need in this file. You can edit vaults by clicking Platform \ Variables \ Vaults and then navigate to the vaults.ps1 tab. ## Importing Secret Variables You can also import pre-existing secrets as variables into Universal. The variable values are not imported but will be looked up during execution. Click the Import Secret button to import secrets. ## Using Variables Variables can be used in APIs, Scripts and Dashboards. When using the default environments, all variables are automatically imported. This is accomplished by specifying the `-Variable` parameter of `New-PSUEnvironment` with a wild card (\*). If you are using your own environments, you will have to configure [which variables you would like included](https://docs.powershelluniversal.com/config/environments#variables). You can reference a variable like you would any other PowerShell variable. The variable will contain the value you set. If you use a secret, it will contain the secret's value. ``` $MyVariable ``` You can customize which variables are allowed in an environment by customizing the environments `-Variable` parameter. See [Environments](https://docs.powershelluniversal.com/config/environments#variables) for more information. ## Secret Scope To access secrets that you have added to PowerShell Universal, you can use the `$Secret` scope. For example, if you defined a secret named `Credential`, you could access that secret anywhere with the secret scope. You cannot set secrets using the secret scope. ```powershell Invoke-Command -Credential $Secret:Credential { Write-Host "Hello" } ``` ### Access Variables by Name You can access variables by name using the `$Secret:` prefix. ```powershell # PSCredential $Secret:MyNewSecret.UserName $Secret:MyNewSecret.Password # String $Secret:DashboardSecret ``` ### Access Values Dynamically You can access the secret scope dynamically by using `Get-Item`. The secret scope is implemented as a provider. This is helpful if you don't have static variable names. ```powershell # PSCredential (Get-ChildItem "Secret:MyNewSecret").UserName (Get-ChildItem "Secret:MyNewSecret").Password # String (Get-ChildItem "Secret:DashboardSecret") ``` ### ForEach-Object -Parallel In order to use secrets when using `ForEach-Object` with the `-Parallel` parameter, you will need to take advantage of the `$using` keyword. ```powershell 1..5 | ForEach-Object -Parallel { $using:Secret:MySecret } ``` ## Configuring the `PSUSecretStore` Password By default, the `PSUSecretStore` vault password is stored within `appsettings.json` in Secrets \ SecretStore \ Password. ## Built-In Variables The following variables are available throughout all environments within PowerShell Universal. | Name | Type | Description | | --------------- | ------ | -------------------------------------------------------------------------- | | $PSUEnvironment | string | The name of the environment the script is running within (e.g. Integrated) | | $Repository | string | The absolute path to the repository folder. | ### API There are a set of predefined variables that are available in API endpoints. You'll be able to use these variables in your scripts. | Variable | Description | Type | | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | | $Url | URL the client used to call the endpoint | String | | $Method | The HTTP method used to call the endpoint | String | | $Headers | Headers provided by the client to call the endpoint | Hashtable | | $Body | The UTF8 encoded string of the content of the request | String | | $Data | Binary byte array for the content of the request | Byte\[] | | $RemoteIpAddress | The remote IP address used to make the request. | String | | $LocalIpAddress | The local IP address used to service the request. | String | | $RemotePort | The remote port that was called to make the request. | Integer | | $LocalPort | The local port that was used to service the request. | Integer | | $Identity | The identity name of the principal accessing the API. | String | | $UrlDefinition | The definition for the URL. | String | | $ConnectionId | The [HTTP Context connection ID](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.connectioninfo.id?view=aspnetcore-6.0#Microsoft_AspNetCore_Http_ConnectionInfo_Id). | String | | $SessionId | The [HTTP Context session ID.](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.isession.id?view=aspnetcore-6.0#Microsoft_AspNetCore_Http_ISession_Id) | String | | $RequestId | The [HTTP Context request ID.](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpcontext.traceidentifier?view=aspnetcore-6.0#Microsoft_AspNetCore_Http_HttpContext_TraceIdentifier) | String | | $ClaimsPrincipal | The claims principal of the current user. This is the same object that is provided to role-based access policies. | [ClaimPrincipal](https://docs.powershelluniversal.com/apps/role-based-access) | ### Apps Below are variables that are available in apps in addition to the global variables. | Name | Description | Type | | ---------------- | ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | | $User | The user name of the logged in user. $Null if authentication is disabled. | String | | $Roles | The roles that the user has been granted. $Null if authentication is disabled. | String\[] | | $RemoteIpAddress | The remote IP address of the connected user. | String | | $RemotePort | The remote port of the connected user. | Int | | $ClaimsPrincipal | The claims principal of the current user. This is the same object that is provided to role-based access policies. | [ClaimPrincipal](https://docs.powershelluniversal.com/apps/role-based-access) | | $Headers | The headers provided by the browser. | hashtable | | $Cookies | The request cookies provided by the browser. | hashtable | | $PSUAppToken | The app token of the current user. Only available when -GrantAppToken is enabled. | string | | $PSUComputerName | The URL of the PSU server. Only available when -GrantAppToken is enabled. | string | | $DashboardName | The name of the current app | string | | $Query | The query string parameters from the URL of the app | Hashtable | | $RefreshToken | The refresh token when using OpenID Connect | string | | $AccessToken | The app token when using OpenID Connect | string | | $UDPage | The name of the current page. | string | ### Scripts There are several built-in variables that are defined when a job is run. You can use these variables in your scripts to retrieve information about the current job. | Name | Description | | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | $UAJob | The current job that is running. This will include properties such as the script, the user that started the job and when the job was started. | | $UAJobId | The ID of the running job. | | $UAScript | The script that is running. This will include properties such as the name of the script and path to the script. | | $UAScriptId | The ID of the running script. | | $UASchedule | The schedule that was used to start the script. | | $UAScheduleId | The ID of the schedule that started the script. | | $AccessToken | When using OIDC authentication, you can retrieve the current user's access token for access resources on their behalf. | | $Roles | The roles of the caller of the script. | #### $UAJob Object Structure Note that properties may be null. `DateTime` objects are return in UTC time zone. ```csharp class Job { public long Id { get; set; } public DateTime CreatedTime { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public JobStatus Status { get; set; } public string Output { get; set; } public string ScriptFullPath { get; set; } public Identity Identity { get; set; } public Job ParentJob { get; set; } public int ParentLineNumber { get; set; } public string ComputerName { get; set; } public int ProcessId { get; set; } public long MemoryBytes { get; set; } public int RunspaceId { get; set; } public string Activity { get; set; } public string CurrentOperation { get; set; } public int PercentComplete { get; set; } public int SecondsRemaining { get; set; } public string StatusDescription { get; set; } public string Environment { get; set; } public Computer Computer { get; set; } public ActionPreference ErrorAction { get; set; } public IEnumerable Children { get; set; } public IEnumerable Parameters { get; set; } public string Credential { get; set; } public long ScheduleId { get; set; } public bool Triggered { get; set; } public string Trigger { get; set; } public int RetryCount { get; set; } public string Tags { get; set; } public string Schedule { get; set; } public bool Archived { get; set; } public bool Batch { get; set; } public Guid? RunId { get; set; } public string Roles { get; set; } } public enum JobStatus { Queued, Running, Completed, Failed, WaitingOnFeedback, Canceled, Canceling, Historical, Active, TimedOut, Warning, Error } ``` #### Retrieving the user that started a script You can retrieve the name of the user that started the script by using the `UAJob` variable ``` $UAJob.Identity.Name ``` #### Checking if a job was run manually You can check if a job was run manually by using the Schedule and Trigger properties. ```powershell $Manual = $UAJob.Schedule -eq $null -and $UAJob.Trigger -eq $null ``` ## Preference Variables Preference variables can be configured on a script, during the execution of a job and on a global level. These include: * DebugPreference * ErrorActionPreference * InformationPreference * ProgressPreference * VerbosePreference * WarningPreference The preference variables have precedence. The following list is ordered in precedence. For example, if a script manually sets the `$DebugPreference` variable, it will override the value set in the script's properties dialog. * Script * Run Dialog or Invoke-PSUScript * Script Properties * Global Settings ## API * [New-PSUVariable](https://github.com/ironmansoftware/universal-docs/blob/v5/cmdlets/New-PSUVariable.txt) * [Get-PSUVariable](https://github.com/ironmansoftware/universal-docs/blob/v5/cmdlets/Get-PSUVariable.txt) * [Remove-PSUVariable](https://github.com/ironmansoftware/universal-docs/blob/v5/cmdlets/Remove-PSUVariable.txt) * [Set-PSUVariable](https://github.com/ironmansoftware/universal-docs/blob/v5/cmdlets/Set-PSUVariable.txt)