How to modify a custom property for multiple nodes with Powershell

So, 

I have been working on several  powershell scripts...  Here is what i have.

#Author: John Johnson
#Last Modified: 3/8/22
#Summary: Set Node Custom Properties in SolarWinds

# Be sure to install the SwisPowershell module on Powershell 5 or lower prior to use
# Install-Module SwisPowershell

#Designate Hostname

$server = 'host'

$creds = Get-Credential

$swis = Connect-Swis -HostName $server -Credential $creds

#$nodes = Get-SwisData $swis 'SELECT NodeID, Caption, DNS, IPAddress, Uri FROM Orion.Nodes'

## Export data from SWQL and arrange it in Excel first
## CSV Import Approach
$updates = Import-Csv C:\Users\list.csv

foreach ($update in $updates)
    {
        Write-Host "Updating"$($update.IPAddress)"@"$($update.Uri)
        $properties = ''
        $properties = $update.psobject.properties | where-object {($_.Name -ne "Uri") -and ($_.Name -ne "NodeID") -and ($_.Name -ne "IPAddress")}

        $record = @{}
        foreach ($property in $properties)
            {
                if (($($property.Value) -ne $null) -and ($($property.Value) -ne ''))
                    {
                        $record += @{$($property.Name)=$($property.Value)}
                    }
            }

        Set-SwisObject $swis -Uri $($update.Uri) -properties $record
    
    }

i have a custom property 'NodesCustomProperties.ServiceNow_Owner_1'

how would i be able to use a serverlist.csv, to modify all servers in the list...and modify the custom property above?

Top Replies

Parents
  • , So below is a script that I have that can do a bulk import of External Nodes and then modifies a Custom Property during that time. It should have the requirements you need to get it to work with some modification. Some key difference between yours and mine is that it looks like you take and build out a URI, mine queries SWQL directly for the URI, which i find helps reduce errors i've seen when trying to rebuild the URI within the script. But in essence much of it is the same, at a high level you'd just need a second set of property definitions for the CP, and then a second Set-SwisObject to modify the CP's for that node.

     # Pre-Reqs:
    # 1. Ensure that the SolarWinds SDK, and powershell modules are installed on machine running the script
    # 2. CSV that contains IP in the first column, Desired Caption in the Second Column, and a CommunityString in the third column (will default to public if none identified), and desired Custom Properties in the remaining columns (IF ADDING CUSTOM PROPERTIES ENSURE THAT YOU UPDATE LINE 39 AND 82 WITH THE APPROPRIATE REFERENCES!!) No Column Headers.
    
    import-Module SwisPowerShell
    # Connect to SWIS
    
    
    $hostname = 'localhost'
    $swis = Connect-Swis -host $hostname -Certificate
    
    #Import New Nodes
    if ($names -eq $true) {
    $names = $null
    }
    #$names = import-csv \\server\file.txt
    $filename = Read-Host -Prompt "Enter CSV Location (\\server\file or C:\dir\file)"
    if (Test-Path $filename) {
    Write-Output "File exists"
    
    $ExternalNodes= import-csv $filename -Header 'IP','Caption','CommunityString','Test1'
    $count = $ExternalNodes.length
    }
    else {
    Write-Output "Bad file, exiting....."
    exit
    Write-Output $ExternalNodes
    }
    
    
    $wait = $false
    while ($wait -eq $false) {
    
    $check = Read-Host "About to add $($count) External Nodes to Orion Instance: $($hostname) Proceed? (y/n)"
    if ($check -eq 'y') {
    $wait = $true
    }
    else { if ($check -ne 'n') {
    $wait = $false
    }
    else {
    Write-host "Process Aborted.  Exiting....."
    exit 0
    }
    }
    }
    foreach($ExternalNode in $ExternalNodes){
    # Set Properties for New External Node
       $newNodeProps = @{
        IPAddress = $($ExternalNode.IP)
        EngineID = 1
        ObjectSubType = "ICMP"
        External = "true"
         Community = "$($ExternalNode.CommunityString)"
        DNS = "$($ExternalNode.Caption)"
        SysName = "$($ExternalNode.Caption)"
        Caption = "$($ExternalNode.Caption)"
    }
    Write-Output "Adding node $($ExternalNode.Caption)"
    New-SwisObject $swis -EntityType "Orion.Nodes" -Properties $newNodeProps
    sleep -m 200
    
    #Add Custom Properties
    $CustomPropsUpdate = @{ Test1="$($ExternalNode.Test1)"}
    #Build URI
    $uri = Get-swisdata $swis -query "SELECT n.CustomProperties.Uri FROM Orion.Nodes n where IP = '$($ExternalNode.IP)'"
    Write-Output "Setting Custom Properties for $($ExternalNode.Caption)"
    Write-Output "URI for update is $uri"
    set-swisobject $swis -Uri $uri -Properties $CustomPropsUpdate
    sleep -m 200 
    }
    Write-Output "Script Complete.  Exiting..."
     
    

Reply Children
No Data