{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":48380,"path":"servicenow-platform/orchestration/mid-server-powershell-files.md","title":"MID Server PowerShell files","bundle":null,"doc_type":null,"release":"australia","body":"\n# MID Server PowerShell files\n\nPowerShell functions are stored in script files \\(\\*.ps1\\) that use a PowerShell Script module \\(\\*.psm1\\) file name extension.\n\nThe PowerShell functions are used by the PowerShell MID Server script files included in these activity packs:\n\n-   Active Directory\n-   Exchange\n-   SCCM\n-   PowerShell\n\n## PSScript.ps1\n\nThis 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.\n\nThe PowerShell variables are generally used directly in the [PowerShell execution command](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/orchestration/CreateInputVariables.md) or as arguments in the MID Server script file you specify. There are special variables that are passed to PSScript.ps1, such as **credType**.\n\n\\[Omitted image \"credTypeExample.png\"\\] Alt text: Using credType in execution parameters\n\n## PowerShell credTypes\n\nThe PowerShell credential types:\n\n|Type|Description|\n|----|-----------|\n|WMI|testCredentialWMI|\n|Exchange|testCredentilExchange|\n|AD|testCredentilAD|\n|SCCM|testCredentilSCCM|\n\nIf 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.\n\n## Exit codes\n\nThese exit codes are returned from the PSScript.ps1 script and logged in the MID Server log file.\n\n|Type|Test function|\n|----|-------------|\n|0|PowerShell command/script ran successfully.|\n|1|Test finished successfully, but the command/script failed to execute.|\n|2|Incorrect syntax passed to script.|\n|3|All credentials including MID Server service account failed to execute the command/script.|\n|4|Passed test and executed the activity, but an error was returned. Example user cannot be found.|\n\n## DiagnosticsUtil.psm1\n\nThe MID Server uses this module file to perform PowerShell logging that assists debugging any Orchestration activity using PowerShell scripting. You can also add debugging statements directly to custom scripts.\n\n<table id=\"table_sd2_vkl_4bb\"><thead><tr><th>\n\nUtility\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nSNCLog-DebugInfo\n\n</td><td>\n\nLog a debug message for a PowerShell script or PowerShell Orchestration activity. Examples: -   `SNCLog-DebugInfo -message \"My debug message...\"`\n-   `SNCLog-DebugInfo \"My debug message\"`\n\n</td></tr><tr><td>\n\nSNCLog-ParameterInfo\n\n</td><td>\n\nLog 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: -   Function with single parameter: `SNCLog-ParameterInfo @(\"Running getHostName\", $target)`\n\n**Note:** The debug message shows, '`Running getHostName $target:[actual value of $target]`'.\n\n-   Function with multiple parameters: `SNCLog-ParameterInfo @(\"Running functionName\", $param1, $param2, $param3)`\n\n</td></tr><tr><td>\n\nSNCObfuscate-Value\n\n</td><td>\n\nUse this utility to encrypt values for security purposes. The function displays `\"$variableName\":***`, where \\*\\*\\* is the obfuscated value. Example: `SNCObfuscate-Value $password`\n\n**Note:** The debug message reads: `\"$password : ***\"`.\n\n</td></tr></tbody>\n</table>## Credentials.psm1\n\nThe MID Server uses this module file to test access to a target host. The MID Server loops through all Windows credentials stored in its credentials table using the following access type functions, unless the PowerShell activity has a credential type \\(**credType**\\) defined. All ServiceNow® authored PowerShell activities are hard-coded to use a specific credential type. As a result, the MID Server only tests credential access against the designated function.\n\n|Function|Description|\n|--------|-----------|\n|testCredentialWMI|Tests the given user and password on the target host using the **Get-WMI** object.|\n|testCredentilAD|Tests the given user and password on the target host using the built-in **DirectoryEntry** object.|\n|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.|\n|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.|\n|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.|\n\n**Note:** If the test passes using one of these functions, that credential is used to run the PowerShell script/command. If the tests fail to access the target host using these functions, the MID Server runs the PowerShell script/command under the account of the MID Server service.\n\nThis 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.\n\n\\[Omitted image \"PowerShellCredentialPickingDiagram.png\"\\] Alt text: PowerShell credential selection criteria\n\n## ActiveDirectory.psm1\n\nThis module file stores the functions used by the PowerShell scripts shipped with the [Active Directory](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/orchestration/c_OrchActiveDirectoryActivities.md) activity pack.\n\n## Exchange.psm1\n\nThis module file stores the functions used by the PowerShell scripts shipped with the [Exchange](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/orchestration/c_OrchestrationExchangeActivities.md) activity pack.\n\n## SCCM.psm1\n\nThis module file stores the functions used by the PowerShell scripts shipped with the [SCCM](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/orchestration/c_SCCMActivityPack.md) activity pack.\n\n**Parent Topic:**[PowerShell protocols and troubleshooting](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/orchestration/powershell-protocols.md)\n\n"}}