Baselines with Powershell

ConsoleWorks Baseline Configuration Management scripts for Windows devices send Powershell commands to the target device to collect configuration data. Within the script the Powershell command statements must be formatted as javascript strings that can be used by the BCM functions to run on the Console. For simple Powershell commands this is a trivial step, but for more complex functionality this can be a tedious, difficult, and error prone exercise.

One of the ways you can make sure the Powershell commands are executed successfully is by creating a Powershell script file (.ps1) on the target device and executing that file using a command from the BCM. While that solution will work, it requires you to maintain a copy of the file on all of your target devices and makes the process of updating or modifying the script much larger. This also introduces the potential issue of having different versions of the Powershell script on various devices which could affect the BCM results. In this article we will show you how to embed a .ps1 file in a BCM script using base64 encoding that will allow you to make use of complex Powershell functionality without having to convert the individual commands into javascript string format.

Encoding the File

To encode the file to base64 you can use the following commands in any powershell prompt.

Example

$Content = Get-Content -Path C:\Temp\script.ps1 -Encoding Byte
$Base64 = [System.Convert]::ToBase64String($Content)
$Base64 | Out-File c:\Temp\encoded.txt

The result should be a file called encoded.txt in c:\Temp.

Open the encoded.txt file and you will see a base64 encoded string representation of the .ps1 file.

Embedding the File

To embed the file, simply create a new variable containing the base64 string in your BCM script.
.

Example

runSetup("$Base64 = 'AcwA6AC8ALwBnAGk. . .AgACAAIAAgACAADQAKAH0ADQAK'");

NOTE: The example above has been cropped for readiblity, this should be a rather long string..

Decoding the File

With the encoded data from the .ps1 file added to the BCM script the last step is to decode the data back into a .ps1 file and put it on the target device. Then the BCM can execute the .ps1 script and collect the baseline information.

// Convert the $Base64 string back into its original form and save it in the variable $Content
runSetup("$Content = [System.Convert]::FromBase64String($Base64)");

// Name of file to be created on the target device
var fileName = "Get-IniContent.ps1"

// Save the $Content to a file (replace Get-IniContent.ps1 with the name of the original file you got the $Base64 from)  
runSetup("Set-Content -Path " + fileName + " -Value $Content -Encoding Byte");
  
// Run the newly created PowerShell script.  It contains the function Get-IniContent()
// There is a space between the two dots (required)
runSetup(". .\\" + fileName);
.
.
// Now you can use the .ps1 file in the rest of your BCM and then for housekeeping reasons remove it at the end of the script.
.
.
runSetup("del " + fileName);

This method ensures that the powershell script files needed by your BCMs will will always be available on the target device, with the right version.