This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

powershell script to search node and delete

Please note that this script was based on this thwack message.

All I did was to change the swsql query used to get the URI and to search and delete a node via PowerShell

# This script will search for  node and delete it if exists

#

################################## Attention ###############################################

# You must have Orion SDK installed and set-execution policy must be set to unrestricted!!#

#Before running you must setup your hashed encrypted password using the line below.       #

###########################################################################################

#### Prerequisites ###

#  run the line below save credentials to be used by this script on a encrypted file

#GET-CREDENTIAL –Credential "EnterTheOrionUserNamehere” | EXPORT-CLIXML C:\Scripts\SecureCredentialsSAM.xml

## Load the SolarWinds Powershell snapin.

##Needed in order to execute the script.

##Requires the Orion SDK 2.0 installed on the machine this script is running from.

Add-PSSnapin SwisSnapin

# SolarWinds username and password section.

# It is recommended to use a local account create within SAM with only. I strongly recommend that you don't use a MS local account or domain account.

# This section allows the password to be embedded in this script. Without it, the script will not work.

#This is using the hashed encrypted password

$MyCredentials=IMPORT-CLIXML C:\Scripts\SecureCredentialsSam.xml

$ORIONSERVERNAME = 'yourserver.domain.com'

#This is connecting to SAM to pull the info we need need with the info given

$swis = Connect-Swis -Credential $MyCredentials -host $orionservername

$nodename = read-host "Enter node Name"

$nodeuri = Get-SwisData $swis "SELECT Uri FROM Orion.Nodes WHERe NodeName LIKE '%$nodename'"

if (!$nodeuri) {Write-Host "node does not exist on SAM"}

if ($nodeuri) {

Write-Host "node exist, uri: $nodeuri"

Remove-SwisObject $swis $nodeUri

Write-Host "node has been removed"

}

  • Do you know if the Orion.Node table is the only place that the Node needs to be deleted from?

  • Once it is deleted from there, nightly database maintenance will clean up data connected to deleted nodes in related tables later.

  • Nicely done egberto.zanon@surrey.ca​ 

    Kudos on both the secure handling of credentials, and the great use of comments in your code! +1 internets to you, sir!

  • # This Script will Create a list of SolarWinds Windows machines matching name and IP criteria

    # Must have Orion SDK and Active Directory Tools Installed

    Add-PSSnapin SwisSnapin

    Import-Module ActiveDirectory

    # The line below will add credentials to an encrypted file

    # GET-CREDENTIAL –Credential "SolarWinds” | EXPORT-CLIXML C:\Scripts\SecureCredentials.xml

    $MyCredentials=IMPORT-CLIXML C:\Scripts\SecureCredentials.xml

    $OrionServer = 'OrionServerName'

    # Connect to Solarwinds

    $swis = Connect-Swis -Credential $MyCredentials -host $OrionServer

    # Query a List of Computer Names

    $NPM_Nodes = Get-SwisData $swis "SELECT SysName FROM Orion.Nodes WHERE [Vendor] = 'WINDOWS' AND (IP_Address LIKE '172.16.%' OR IP_Address LIKE '192.168.%' OR IP_Address LIKE '10.73.%') AND SysName <> 'DLS' AND SysName NOT LIKE '%NVR%'"

    # Query a List of Computer Names From Active Directory

    $AD_Nodes = Get-ADComputer -Filter {Enabled -eq $True -and Name -notlike '*DED*' -and Name -notlike '*SHR*' } -SearchBase "OU=Computers,DC=xyz,DC=company,DC=org" | SELECT Name -ExpandProperty Name

    # List Computers in SolarWinds that are not in Active Directory (No longer Joined to the domain)

    $NodeDelete = $NPM_Nodes | ?{$AD_Nodes -notcontains $_}

    # Get the URI Property for each node to be deleted from SolarWinds

    $NodeDeleteURI = Get-SwisData $swis "SELECT Uri FROM Orion.Nodes WHERE SysName IN @NodeDelete" @{ NodeDelete = $NodeDelete }

    # Delete the Node From Solarwinds

    $NodeDeleteURI | Remove-SwisObject $swis

  • wickedoz

    Is there a python(Unix) equivalent to this.

    # GET-CREDENTIAL –Credential "SolarWinds” | EXPORT-CLIXML C:\Scripts\SecureCredentials.xml

    $MyCredentials=IMPORT-CLIXML C:\Scripts\SecureCredentials.xml

    $OrionServer = 'OrionServerName'

    # Connect to Solarwinds

    $swis = Connect-Swis -Credential $MyCredentials -host $OrionServer

  • Can you run Orion on Unix? I thought it required Windows Server. I am not familiar with Unix.

  • If you mean the Orion primary server, Polling engines. Yes they are on windows. But the Orion SDK(api) can run in Unix through python.

    I got the python instruction here https://github.com/solarwinds/orionsdk-python

    And the code are working fine in Linux(after installing Orion SDK through pip)

  • python I don't know but maybe via bash according to : https://blog.ls-al.com/hiding-passwords-in-scripts, but I personally haven't  used this method yet. Let me know if you got this working or found another method!

    # Test
    $ echo 'mySecretPassword' | openssl enc -base64 -e -aes-256-cbc -nosalt  -pass
    pass:garbageKey
    yQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=
    $ echo 'yQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=' | openssl enc -base64 -d
    -aes-256-cbc -nosalt -pass pass:garbageKey
    mySecretPassword

    # Use a hidden file
    $ echo 'mySecretPassword' | openssl enc -base64 -e -aes-256-cbc -nosalt  -pass pass:garbageKey > .hidden.lck
    $ cat .hidden.lck
    yQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=
    # In a script
    $ MYENCPASS=`cat .hidden.lck | openssl enc -base64 -d -aes-256-cbc -nosalt
    -pass pass:garbageKey`
    $ echo $MYENCPASS
    mySecretPassword