Use Powershell to set all subnets to override building in inventory

New Contributor III

Back again with another Powershell script to make life easier for us windows users.

If you use SCCM, then you know how great it is about letting machines flow easily between subnet boundaries and sites. To make the JSS work the same way, you need to to enable the "override building in inventory" option. This will let your machines move freely between buildings and keep your smart groups smart.

This will enumerate all the segments and update the setting. Alternatively, you could flip some logic around to also turn it off.

Here is the script:

# Created with: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.35
# Created on:   12/14/2015 2:16 PM
# Created by:   Corey Thomas
# Organization: Removed for privacy
# Version :   1.0  

### Pre-reqs:  Create a standard user account for JSS (full access, custom privileges) and grant it read/update to the JSS Objects.  Specify this account below

$JSSAPIURL = "https://yourJSSURL:8443/JSSResource"
$JSSAPIUser = "JSSApiAccount"
$JSSAPIPass = "JSSApiPassword"
$VerbosePreference = "SilentlyContinue"  #Optional for extra logging - Change to "Continue"

#First we need to setup the shell to ignore self-signed certs for non-PKI Casper installs:
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#Next we setup the creds we will be using:
$user = $JSSAPIUser
$pass = ConvertTo-SecureString -String $JSSAPIPass -AsPlainText -Force
$Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pass

#First, let's get a list of the subnets from JSS
$url = "$JSSAPIURL/networksegments"
$segments = Invoke-RestMethod -Uri $url -Credential $Creds
#The data is stored in $

#Setting up a template for XML update into the JSS API
[xml]$Template = "<?xml version='1.0' encoding='UTF-8'?><network_segment><override_buildings>true</override_buildings></network_segment>"

#Loop through the data and start pulling data:
foreach($segment in $segments.network_segments.network_segment){

    #Now we setup the URL to pull data from
    $segmentID = $segment.ID
    $segmentName = $segment.Name
    $url = "$JSSAPIURL/networksegments/id/$segmentID"
    Write-Output "Getting data for $segmentName"
    Write-Verbose "ID: $segmentID"
    Write-Verbose "API: $url"

    #Clear the data variable to ensure we don't accidentally overwrite the wrong data
    $data = $null

    #Now we call the JSS REST API to get the data
    $data = Invoke-RestMethod -Uri $url -Credential $Creds

    #Pull the current settings
    $currentSetting = $data.network_segment.override_buildings
    $building = $data.network_segment.building
    Write-Verbose "Current setting: $currentSetting"
    Write-Verbose "Current building: $building"

    if($currentSetting -match "false"){
        $newData = $template
        Write-Output "  Updating setting for $segmentName"
        $return = Invoke-RestMethod -Uri $url -Credential $Creds -Method put -Body $newData