Export System Center Orchestrator PowerShell Scripts

This PowerShell script connects to a SQL Server database, retrieves PowerShell scripts, and saves them to the local filesystem.

Parameters

  • ServerInstance: The SQL Server instance to connect to. Default is “SERVER\INSTANCE”.
  • Database: The name of the database to query. Default is “orchestrator”.
  • BasePath: The base directory where the script files will be saved. Default is “c:\SCO_PS_STRUCTURE\Runbooks”.

Function: Execute-SqlSelectQuery

This function executes a SQL query and returns the result.

Parameters

  • ServerInstance: The SQL Server instance.
  • Database: The name of the database.
  • Query: The SQL query to execute.

Expected Result

After running the PowerShell script, you can expect to receive a collection of objects representing the results of the SQL query. Each object includes two properties:

  • FolderPath: A string representing the file path where the script file will be saved.
  • ScriptBody: A string containing the content of the PowerShell script.

Here is an example representation of a single object in the result:

{
  "FolderPath": "c:\SCO_PS_STRUCTURE\Runbooks\SomeFolder\SomeScript.ps1",
  "ScriptBody": "Write-Host 'Hello, World!'"
}

From this results we are generating the Powershell Files located in the System Center Orchestrator

Example Usage

# Define the SQL Server connection parameters
$serverInstance = "SERVER\INSTANCE"
$database = "orchestrator"

# Specify the base path where the PowerShell script files will be saved
$basePath = "c:\SCO_PS_STRUCTURE\Runbooks\"

# Execute the SQL query to retrieve script data from the database

$query = @"
DECLARE @BasePath NVARCHAR(255) = '$($basepath)';

SELECT
    @BasePath + ISNULL(F2.Name, '') + '\' + F1.Name + '\' + P.Name + '\' + O.Name + '.ps1' AS FolderPath,
    R.ScriptBody
FROM FOLDERS AS F1
JOIN Policies AS P ON F1.UniqueID = P.ParentID
JOIN Objects AS O ON P.UniqueID = O.ParentID
JOIN RUNDOTNETSCRIPT AS R ON O.UniqueID = R.UniqueID
LEFT JOIN FOLDERS AS F2 ON F1.ParentID = F2.UniqueID
WHERE R.ScriptType = 'PowerShell';
"@

# Execute the SQL query and save the script files
$result = Execute-SqlSelectQuery -ServerInstance $serverInstance -Database $database -Query $query

# Process the query results and save script files
foreach ($object in $result) {
    $filePath = $object.FolderPath
    $fileContent = $object.ScriptBody

    if (Test-Path -Path $filePath -PathType Leaf) {
        # File already exists, so overwrite it
        Set-Content -Path $filePath -Value $fileContent
        Write-Host "File '$filePath' already exists and has been overwritten."
    } else {
        # File doesn't exist, create it
        New-Item -Path $filePath -ItemType File -Force
        Set-Content -Path $filePath -Value $fileContent
        Write-Host "File '$filePath' has been created."
    }
}

A big Benefit is

  • Version Control: With source control, you can track changes made to the scripts over time. This includes who made the changes, when they were made, and what exactly was changed. This makes it easier to roll back to previous versions if needed and to collaborate with others on development.


You can find the actual Script under my GitHub Repository :
https://github.com/uzejnovicahmed/System_Center_Orchestrator/tree/e6f06450c651973eb732104d38b2157730395445/ExtractScoPsScripts: Export System Center Orchestrator PowerShell Scripts

Happy Automating !

Leave a comment