- Glen (Xapity)
Moving PowerShell Scripts Between Environments
Moving Xapity PowerShell Activity scripts between environments or instances of Microsoft Service Manager (SCSM) is fully supported. This post will outline the steps involved in exporting the scripts and then importing them to the new environment.
This process is based on the Import Connector that is built into Service Manager. Refer to this Technet article for more information: About Importing Data from Comma-Separated Files into Service Manager.
A lot of organisations use Dev, Test, Pre-prod environments to do development work and test before introducing new elements into the Production environment.
To facilitate developing scripts and templates in a lower environment and then migrating them to the production environment Xapity PowerShell Scripts were designed to be portable between environments.
We used a custom SrciptKey property as the key for any referencing in Xapity PowerShell Activity and by keeping this the same in both environments the PowerShell Scripts link up to the PowerShell Activities.
Step 1: Export the Scripts from the lower environment to CSV file
The first step is to export the PowerShell Scripts from the lower Service Manager instance. This is done by defining the class for scripts Xapity.PowershellActivity.Script and then using Get-SCSMObject to collect them into a variable.
Using Export-CSV will produce a CSV file with a header row, but the Import Connector wants a CSV file without a header, so I have used ConvertTo-CSV and then skip the first row to get a file without a header. I have included both options in the code below.
The select order is also important. As the CSV file does not have a header, the Import Connector assumes that the columns match the order in the format XML file.
# Import Smlets Module
Import-module -name Smlets
# Get the Service Requests Class
$PowerShellClass = Get-SCSMClass -Name Xapity.PowershellActivity.Script$
# Get All PowerShell Scripts
$AllScripts = Get-SCSMObject -Class $PowerShellClass
$FileLocation = "C:\PowerShellSciptsExport.csv"
#Choose one of the options and Delete the other:
#Option 1: Export PowerShell Scripts With no Header Row
$AllScripts | Select Id, DisplayName, ScriptGuid, ScriptBody, ScriptEnabled, ScriptTitle, Notes, RunAsEnabled, RunAsDescription, RunAsUserName, RunAsDomain, RunAsPassword, PSCredEnabled, PSCredDescription, PSCredUserName, PSCredDomain, PSCredPassword, SeperateProcess | ConvertTo-Csv -NoTypeInformation | select -Skip 1 | Set-Content $FileLocation
#Option 2: Export PowerShell Scripts With Header Row
$AllScripts | Select Id, DisplayName, ScriptGuid, ScriptBody, ScriptEnabled, ScriptTitle, Notes, RunAsEnabled, RunAsDescription, RunAsUserName, RunAsDomain, RunAsPassword, PSCredEnabled, PSCredDescription, PSCredUserName, PSCredDomain, PSCredPassword, SeperateProcess | Export-csv $FileLocation -NoTypeInformation
This could also target just one Script:
$AllScripts = Get-SCSMObject -Class $PowerShellClass -filter "ID -eq PS10"
Step 2: Prepare the Format XML File
The Import Connector uses two files, a data file (CSV file without a header row) and a XML format file. The format file defines the columns used in the data file and as the internal property names.
To import the PowerShell Scripts we can use the following format file.
<?xml version="1.0" encoding="utf-8"?>
<Property ID="Id" />
<Property ID="DisplayName" />
<Property ID="ScriptGuid" />
<Property ID="ScriptBody" />
<Property ID="ScriptEnabled" />
<Property ID="ScriptTitle" />
<Property ID="Notes" />
<Property ID="RunAsEnabled" />
<Property ID="RunAsDescription" />
<Property ID="RunAsUserName" />
<Property ID="RunAsDomain" />
<Property ID="RunAsPassword" />
<Property ID="PSCredEnabled" />
<Property ID="PSCredDescription" />
<Property ID="PSCredUserName" />
<Property ID="PSCredDomain" />
<Property ID="PSCredPassword" />
<Property ID="SeperateProcess" />
Step 3: Prepare the Data File
The data file cannot contain a header row and it should be removed if it exists.
When moving between environments, the Run As and PS Credentials should be changed if the lower environment is a different domain to the Production instance.
PowerShell Activities have an ID field eg PS34 which does not need to be unique (we use the Script Key for uniqueness) and thus during the import process it is possible to create two PowerShell activities with the same PowerShell ID. If this will cause a problem, edit the PS ID values in the data file before importing them so there is no clash of IDs.
Step 4: Import Scripts in Production Environment using Import CSV Connector
The data file and the format file have to have the same name, with just the extension CSV and XML being different.
The import process supports updating existing PowerShell scripts as well as creating new Scripts ie updating a script in the lower environment and re-importing it will update the script in the Production instance.
From the Service Manager admin console navigate to Administration\Connectors and choose the task Import from CSV file. Select the format file and data file and then Import.