MID Server PowerShell files
Summarize
Summary of MID Server PowerShell files
This documentation describes the use of PowerShell script and module files (.ps1 and .psm1) by the ServiceNow MID Server to execute orchestration activities across various systems such as Active Directory, Exchange, and SCCM. The main PowerShell script,PSScript.ps1, leverages credential testing and password encryption modules to securely execute commands on remote hosts. The guidance focuses on how credentials are tested and selected, the logging utilities available for debugging, and the use of specific module files tailored to different activity packs.
Show less
PowerShell Credential Testing and Selection
The MID Server uses the credential.psm1 module to test access to target hosts by iterating through stored Windows credentials. Each PowerShell activity may specify a credType that determines which credential test function to run. If no credType is specified, the MID Server defaults to testing with the WMI method.
- Credential Types and Test Functions:
- WMI: Uses Get-WMI object to test access.
- Active Directory (AD): Uses DirectoryEntry object.
- Exchange: Uses PowerShell remoting with WinRM configured.
- SCCM: Uses PowerShell remoting with WinRM configured.
- If all credentials fail, the MID Server runs the PowerShell command under the MID Server service account.
- If the target host is the MID Server itself, credentials in the Credentials table are bypassed and the MID Server service account is used directly.
Exit Codes from PSScript.ps1
The PSScript.ps1 script returns specific exit codes that are logged by the MID Server, providing insight into the success or failure of command execution:
- 0: Command/script ran successfully.
- 1: Test succeeded but command/script failed to execute.
- 2: Incorrect syntax passed to the script.
- 3: All credentials including the MID Server service account failed.
- 4: Command executed but returned an error (e.g., user not found).
PowerShell Diagnostic Utilities
The DiagnosticsUtil.psm1 module provides utilities to enhance logging and debugging of PowerShell orchestration activities:
- SNCLog-DebugInfo: Logs debug messages.
- SNCLog-ParameterInfo: Logs function parameter values for better traceability.
- SNCObfuscate-Value: Encrypts sensitive values in logs to maintain security.
Activity Pack Module Files
Separate module files contain specific functions for each orchestration activity pack, allowing the MID Server to execute relevant PowerShell commands:
- ActiveDirectory.psm1: Functions for Active Directory activities.
- Exchange.psm1: Functions for Exchange activities.
- SCCM.psm1: Functions for SCCM activities.
These modules are invoked by the associated PowerShell scripts included in the activity packs, ensuring that credentials and commands are properly handled for each target system type.
PowerShell functions are stored in script files (*.ps1) that use a PowerShell Script module (*.psm1) file name extension.
- Active Directory
- Exchange
- SCCM
- PowerShell
PSScript.ps1
This script performs a few tasks, such as credential testing, password encryption, and the execution of scripts configured in the Orchestration Activity Designer or in MID Script Files. However, this document focuses on how PSScript.ps1 uses the credential.psm1 module for testing access to remote hosts.
PowerShell credTypes
The PowerShell credential types:
| Type | Description |
|---|---|
| WMI | testCredentialWMI |
| Exchange | testCredentilExchange |
| AD | testCredentilAD |
| SCCM | testCredentilSCCM |
If no credential type is passed to the PSScrip.ps1 script, the MID Server defaults to the WMI test function to test access to the target host. If there is a credential type used, the MID Server runs the corresponding test function for that credential type.
Exit codes
These exit codes are returned from the PSScript.ps1 script and logged in the MID Server log file.
| Type | Test function |
|---|---|
| 0 | PowerShell command/script ran successfully. |
| 1 | Test finished successfully, but the command/script failed to execute. |
| 2 | Incorrect syntax passed to script. |
| 3 | All credentials including MID Server service account failed to execute the command/script. |
| 4 | Passed test and executed the activity, but an error was returned. Example user cannot be found. |
DiagnosticsUtil.psm1
| Utility | Description |
|---|---|
| SNCLog-DebugInfo | Log a debug message for a PowerShell script or PowerShell Orchestration
activity. Examples:
|
| SNCLog-ParameterInfo | Log a function parameter value. For "function getHostName{ param(
[String] $target )", the first value to the PowerShell hashtable is a
string to indicate which function executes, and the values for each of the
function parameters. Examples:
|
| SNCObfuscate-Value | Use this utility to encrypt values for security purposes. The function
displays "$variableName":***, where *** is the obfuscated value.
Example: SNCObfuscate-Value $password
Note: The debug message
reads: "$password : ***". |
Credentials.psm1
| Function | Description |
|---|---|
| testCredentialWMI | Tests the given user and password on the target host using the Get-WMI object. |
| testCredentilAD | Tests the given user and password on the target host using the built-in DirectoryEntry object. |
| testCredentialExchange | Tests the given user and password to create a session on an Exchange host. This test uses the built-in PowerShell remoting feature on a remote host. WinRM is configured on Exchange servers by default. |
| testCredentialSCCM | Tests the given user and password to create a session on an SCCM server. This test uses the built-in PowerShell remoting feature on a remote host. This test requires WinRM to be configured. |
| testNoCredentialAccessWMI | Tests the given user and password on the target host, using the Get-WMI object. This test is used when no credType is used. |
This diagram illustrates the dependency of the credential selection behavior on the host being targeted by the PowerShell activity. If the target host is the IP address or host name of the MID Server, the MID Server bypasses all credentials in the Credentials table and uses the account of the MID Server service. If the target host is not the MID Server, then all Windows credentials are used first. If all credentials in the Credentials table are unsuccessful in running the PowerShell activity, then the MID Server uses the MID Server service account.
ActiveDirectory.psm1
This module file stores the functions used by the PowerShell scripts shipped with the Active Directory activity pack.
Exchange.psm1
This module file stores the functions used by the PowerShell scripts shipped with the Exchange activity pack.
SCCM.psm1
This module file stores the functions used by the PowerShell scripts shipped with the SCCM activity pack.