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.

Need to change the caption of multiple nodes using powershell ?

Can anyone help me in this using powershell ?

$swis1 = Connect-Swis -Hostname $OrionServer -Username $Username -Password $Password

get-content = Get-SwisData $swis1 -Query "Select NodeID, Caption from Orion.Nodes where Caption like '%01'"

NodeID Caption     

------ -------     

     1 xxx01 

     4 yyy01 

     5 ZZZ0

I NEED TO CHanGE LIKE BELOW TABLE USING POWERSHELL

NodeID Caption     

------ -------     

     1 xxx0

     4 yyy0

     5 ZZZ0

Sample for one object:

##Set-SwisObject -SwisConnection $swis1 -Uri [swis://#####/Orion/Orion.Nodes/NodeID=$($nodeId)] -properties [json formatted caption]

  • The Set-SwisObject can be used like so:

    # Variables
    $SwisHost = "<your Orion server>"
    $user = "<user name>"
    $nodeid = <NodeId>
    $newname = "<new name>"
    
    #Get password and create credentials
    $passwd = Get-Content 'C:\Scripts\OrionAdmin.txt' | ConvertTo-SecureString -AsPlainText -Force
    $SwisCreds = New-Object -typename System.Management.Automation.PSCredential -argumentlist $user, $passwd
    
    #SWIS connection
    $Swis = Connect-Swis -Hostname $SwisHost -Credential $SwisCreds 
    
    Set-SwisObject $swis -Uri "swis://$SWIShost/Orion/Orion.Nodes/NodeID=$nodeid" -Properties @{ Caption = '$newname' }

    Wrap that in a ForEach loop and Bob's your uncle. :smileywink:

  • Guessing something like that could be accomplished in a simple SQL query in the database manager.  Something along the lines of 

    UPDATE Nodes N

    SET (Caption = SUBSTRING(N.Caption, 1, LEN(N.Caption) - 1))

    WHERE N.Caption LIKE '%01'

  • I see that the first two changed, but not the third. Did you mean for ZZZ0 to be ZZZ01 in the first example?

  • I gave the example similar like what am getting in output first two caption contain 01 that need to remove but third one no need to change.
  • Need to do this only using powershell
  • Hi Rajasekar,

    Here's a starter for you, but before I give you a fully working script you'll need to check that it works first! 

    The below script will extract all Captions ending in 01, using Get-SwisQuery and the SWQL  query: 

    Select NodeID, Caption FROM Orion.Nodes WHERE Caption like '%01'

    It then removes the last character (which will always be '1') and populates this value into $newname.

    #  Variables
    $SwisHost = "<your Orion server>"
    $user = "admin"
    $creds= Get-Credential -UserName $user -Message "Enter password"
    $Swis = Connect-Swis -Hostname $SwisHost -Credential $creds 
    
    #Find hostnames ending in the number '01'
    #     and then create newname by removing the last character
    $nodes = Get-SwisData $swis -Query "Select NodeID, Caption FROM Orion.Nodes WHERE Caption like '%01'"
    
    ForEach ($node in $nodes)
           {
             $id = $node.NodeID
             $name = $node.Caption
             $newname = $name.Substring(0,$name.Length-1)
             Write-Host "$name will become $newname"
             
    
             #Update caption to $newname
             #Set-SwisObject $swis -Uri "swis://$SWishost/Orion/Orion.Nodes/NodeID=$id" -Properties @{ Caption = $newname }
             
             #Check the results
             #Get-SwisObject $swis -Uri "swis://$SWishost/Orion/Orion.Nodes/NodeIs=$id" 
           }

    From your example:

    NodeID Caption     

    ------ -------     

         1 xxx01 

         4 yyy01 

         5 ZZZ0

    So  xxx01 would become xxx0, yyy01 would become yyy0 and ZZZ0 would not change, as it is not be returned by the SWQL query.

    The actual Set-SwisObject call has been commented out, until you are happy that you have selected the correct nodes (edit the SWQL query if not) and that it creates the correct 'new name'.

    Once you are happy with that the output to the console is TOTALLY CORRECT, then uncomment the Set-SwisObject (and the following Get-SwisObject to check the results), and run it.

    I will warn you, this could change hundreds of node captions, so ensure that the SWQL only returns the node captions you want to change.

    Probably also worth backing up the Nodes table (or the whole DB), just in case.

    Then if you do muck it up, it will relatively simple to fix.

    A really good place to start learning this is from: https://github.com/solarwinds/OrionSDK/tree/master/Samples/PowerShell 

    Hope it helps,

    Rich (aka yaquaholic) 

  •   - How did you get on, did it work?