I put together a few PowerShell scripts to simplify the exporting and importing Configuration Manager Scripts during site migrations or lab rebuilds. The first script exports all Scripts from Configuration Manager and converts each script into a JSON string, which is then saved to a file. The second script imports these files converts the JSON strings back into Configuration Manager scripts, and approves each script.
By automating this process, these scripts save time and effort for administrators who would otherwise need to manually export and import each script one by one. Additionally, by converting the scripts to JSON, the data is easily transferable between different environments, making it a convenient solution for site migrations or lab rebuilds. Whether you are an experienced administrator or just starting out, these scripts provide a simple and efficient way to manage your Configuration Manager scripts.
Export-CMScriptsToJSON
<#
=================================================================================
DISCLAIMER:
This script is provided "as-is" with no warranties. Usage of this script is at
your own risk. The author is not liable for any damages or losses arising from
using this script. Please review the full legal disclaimer at:
https://kaidojarvemets.com/legal-disclaimer/
=================================================================================
#>
#Get all Scripts from Configuration Manager
$Scripts = Get-CMScript | Select-Object -Property ScriptName,Script
$ScriptCounter = 0
foreach($Script in $Scripts){
Write-Output -InputObject "Exporting $($Script.ScriptName)"
#Convert the Script Name and Content to JSON and Save it
$Script | ConvertTo-Json | Out-File "E:\TEMP\Script-$ScriptCounter.JSON" -Force
$ScriptCounter++
}
Import-CMScriptsFromJSON
<#
=================================================================================
DISCLAIMER:
This script is provided "as-is" with no warranties. Usage of this script is at
your own risk. The author is not liable for any damages or losses arising from
using this script. Please review the full legal disclaimer at:
https://kaidojarvemets.com/legal-disclaimer/
=================================================================================
#>
$Scripts = Get-ChildItem -Path E:\TEMP\Scripts |
ForEach-Object {Get-Content -Path $PSITEM.FullName -Raw} | ConvertFrom-Json #| Select-Object -First 1
foreach($Script in $Scripts){
Write-Output -InputObject "Importing $($Script.ScriptName)"
$ScriptContent = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Script.Script))
New-CMScript -ScriptName $Script.ScriptName -ScriptText $ScriptContent | Out-Null
}
Approve-CMScripts
<#
=================================================================================
DISCLAIMER:
This script is provided "as-is" with no warranties. Usage of this script is at
your own risk. The author is not liable for any damages or losses arising from
using this script. Please review the full legal disclaimer at:
https://kaidojarvemets.com/legal-disclaimer/
=================================================================================
#>
#Get all the Scripts and Approve all of them
Get-CMScript | ForEach-Object {Approve-CMScript -InputObject $PSITEM}