Creating On-Prem PowerShell Scripts

When adding an on-prem PowerShell data source, SquaredUp will look for your scripts in the Scripts directory you define in the data source configuration.

Additionally, if you want to import new objects into SquaredUp, you can also choose to provide an Import script.

The following information describes and provides examples of how to format these scripts.

Creating import scripts

If you want to import new objects into SquaredUp, you must create an import script and supply it when configuring the data source. This script contains the definitions for objects and their links in the Knowledge Graph.

There can only be one import script per PowerShell On-Prem data source. However, if you want to use different import scripts, you can add more PowerShell on-prem data sources to SquaredUp which share the same folder.

Format

Import scripts must write objects to SquaredUp in the following format:

@{
vertices = @()
edges = @()
}

Vertices

Vertices are responsible for creating the objects you import into SquaredUp. The following information describes the format of a vertex.

name
string – the name of the vertex
sourceId
string – a unique ID identifying the vertex
sourceType
string – a type for the vertex pertinent to your specific use-case
other properties
You can use any other properties of use in your specific use-case

Edges

Edges are used to create links between your SquaredUp objects. The following information describes how to define an edge.

label
string – the type of edge
inV
string – the sourceId value of the vertex the edge enters
outV
string – the sourceId value of the vertex the edge leaves

Example import script

get-objects.ps1:
@{
    vertices = @(Get-CimInstance -Class Win32_LogicalDisk |
        ? { $_.DriveType -eq 3 } |
    %{
        [PSCustomObject]@{
            name = ('{0} {1}' -f $_.SystemName,$_.Name)
            sourceId = ('\\{0}\root\cimv2:Win32_LogicalDisk.DeviceID="{1}" ({2})' -f $_.SystemName,$_.Name,$_.VolumeSerialNumber)
            sourceType="volume"
            FileSystem = $_.FileSystem
            Compressed = $_.Compressed
            Size = $_.Size
        }
    })
    edges = @()
}

Creating data stream scripts

All the other scripts SquaredUp finds in this folder are treated as data stream scripts. These scripts define the different data streams you want to use when you are creating tiles for a dashboard and are supplied when using the Run script on-premise data stream.

Format

Data stream scripts must return data as a simple array of objects.

@()

The format of the result objects of your script needs to match the columns you've defined in the custom Data Stream.

Example data stream script

get-diskFreeSpace.ps1:
Param(
    [Parameter(Mandatory=$true)]
    $scope
)

$targetObjectsBySourceId = @{}
foreach ($targetObject in $scope) {
    $targetObjectsBySourceId[$targetObject.sourceId[0]] = $targetObject
}

$results = (Get-CimInstance -Class Win32_LogicalDisk |
    ? { $_.DriveType -eq 3 } |
%{
    $sourceId = '\\{0}\root\cimv2:Win32_LogicalDisk.DeviceID="{1}" ({2})' -f
        $_.SystemName,$_.Name,$_.VolumeSerialNumber
    if ($targetObjectsBySourceId.ContainsKey($sourceId)) {
        [PSCustomObject]@{
            name = ('{0} {1}' -f $_.SystemName,$_.Name)
            Size = $_.Size
            FreeSpace = $_.FreeSpace
        }
    }
})

return $results

Was this article helpful?


Have more questions or facing an issue?