Export and Import ConfigMgr Run Scripts

This is one of those articles that have been laying around in the draft folder for a long time and I just wanted to put this out there for anyone who might find it of value.

In short, In ConfigMgr we have the capabilities to “Run Scripts” which is a feature to pre-define a script that we later can run on one or more machines and get real time results back. I first wrote about this feature 7 years ago back in 2017 when it first was introduced in the Technical preview.

Since the introduction of Run Script its been a really good tool in the toolbox for ConfigMgr Admins. This time however I wanted to focus on a way of importing and exporting the scripts we have created in ConfigMgr. This could be relevant in a backup scenario or when you might want to move to a new envirorment but keep the Run Scripts.

I had to use the export and Import a few months ago when I wanted to move from one ConfigMgr setup to another. To be able to do this we need to run a powershell script on the Primary Site Server and I’ll show you how to do that below.

Export script

#.NOTES
#===========================================================================
#Created on:    3/21/2023
#Modified on:   3/21/2023
#Created by:    Timmy Andersson
#Twitter:       @TimmyITdotcom
#Blog:          www.timmyit.com
#===========================================================================
#.DESCRIPTION
#Export Run scripts. 
#Specify Destination path with the parameter $DestinationPath
 
[CmdletBinding(DefaultParameterSetName = 'DestinationPath')]
param
(
[Parameter(Mandatory = $true,
Position = 1)]
$DestinationPath
)
    Begin{
    $SiteCodeObjs = Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $env:COMPUTERNAME -ErrorAction Stop
        foreach ($SiteCodeObj in $SiteCodeObjs)
        {
            if ($SiteCodeObj.ProviderForLocalSite -eq $true)
                {
                $SiteCode = $SiteCodeObj.SiteCode
                }
        }
    $SitePath = $SiteCode + ":"
    Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0, $Env:SMS_ADMIN_UI_PATH.Length - 5) + '\ConfigurationManager.psd1')
}
PROCESS
{
 
Set-Location $SitePath
$Scripts = Get-CMScript 

    if (-not (Test-Path $DestinationPath))
    {
        new-item -Path $DestinationPath -ItemType Directory -Force
    }

foreach ($Script in $Scripts) {

  $PS = [System.Text.Encoding]::unicode.GetString([System.Convert]::FromBase64String($($Script).Script))
   Out-File -FilePath "$DestinationPath\$($Script.ScriptName).ps1" -InputObject $PS
 }

 }

Export script is also available on Github
https://github.com/timmyit/Configuration-Manager/blob/main/Export-RunScripts.ps1

To export the scripts, open up a Powershell prompt and execute the Export-RunScript.ps1 script with the DestinationPath parameter to point to where you want to save all the scripts that you are about to export.

Once the script has finished you will find all the scripts in the destination path you specified.

Copy the exported scripts to the new server or environment where you want to import them to.

Import script

#.NOTES
#    ===========================================================================
#     Created on:    3/21/2023 
#     Modified on:   3/21/2024 
#     Created by:    Timmy Andersson
#     Twitter:       @TimmyITdotcom
#     Blog:          www.timmyit.com
#    ===========================================================================
#    .DESCRIPTION
#        Import Run Scripts. 
#        Specify source path with the parameter -SourcePath

[CmdletBinding(DefaultParameterSetName = 'SourcePath')]
param
(
[Parameter(Mandatory = $true,
Position = 1)]
$SourcePath
)
  
Begin{
$SiteCodeObjs = Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $env:COMPUTERNAME -ErrorAction Stop
    foreach ($SiteCodeObj in $SiteCodeObjs)
    {
        if ($SiteCodeObj.ProviderForLocalSite -eq $true)
            {
            $SiteCode = $SiteCodeObj.SiteCode
            }
    }
$SitePath = $SiteCode + ":"
Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0, $Env:SMS_ADMIN_UI_PATH.Length - 5) + '\ConfigurationManager.psd1')
}
Process
{
    $Scripts = (Get-ChildItem -Path $SourcePath) 
    
  
  
Set-Location $SitePath

                Foreach ($Script in $Scripts)
                    {

                     $ScriptFile = $SourcePath + "\" + $Script.name
                     New-CMScript -ScriptName "$($Script)" -ScriptFile $ScriptFile  -Fast
                    }

  
}

Import script is also available on Github
https://github.com/timmyit/Configuration-Manager/blob/main/Import-RunScripts.ps1

To Import the scripts open up a powershell prompt and execute the Import-RunScript.ps1 with the SourcePath parameter where you point towards the folder containing all the scripts you want to import.

Once the script has finished you will see all the scripts in the ConfigMgr console, however they need to be approved before they can be used.

You can approve them manually one by one or here’s a way of approving all of the scripts in one go from powershell

$Scripts = Get-CMScript

foreach ($Script in $Scripts) {

 Approve-CMScript -ScriptGuid $Script.ScriptGuid

 }

That’s it for this time, Don’t forget to follow me on X (twitter) @timmyitdotcom or connect with me on LinkedIn

2 comments

  1. I thought this import was successful but when we deployed scripts we no longer received any write-host output like before. I’m not sure if the imported scripts may have had hidden registration or metadata issues but the way I fixed it was making a copy of each script and deleting the original and then the output was received like normal when deployed. Curious if anyone else had this issue?

Leave a Reply