cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Community Manager
Community Manager

PowerShell to Update Captions - What features am I missing?

I build this PowerShell script to rename my captions (I personally hate FQDN names for captions).  I've included "HostNameOnly" and "Capitalize" for features, but that's all I got and my head ran dry.

What else should be included?

 

 

<#
Bulk Rename Captions
#>

$OrionServer = "10.196.3.18"

# Filter and rename tasks
$HostNameOnly = $true
$Capitalize   = $true

if ( -not ( $SwisConnection ) )
{
    $SwisCredentials = Get-Credential -Message "Enter your Orion credentials for $OrionServer"
    $SwisConnection = Connect-Swis -Credential $SwisCredentials -Hostname $OrionServer
}

# Base Query for Nodes
# We really only need the Caption and the Uri, but I prefer to have a little extra.
$SwqlQuery = @"
SELECT [Nodes].NodeID
     , [Nodes].Uri
     , [Nodes].Caption
     , [Nodes].IPAddress
FROM Orion.Nodes AS [Nodes]
"@

#Build an empty collection for where clauses for the query
# These clauses will be put together with " OR " as the joiner, so include any "AND" statements within the logic
# For readability, I'm going to enclose any compound filters within parenthesis.
$WhereClauses = @()

if ( $HostNameOnly )
{
    # We need to filter for captions that match a domain name format: hostname.domain.local
    #   Fair warning - if you are using an IPv4 address as a caption, this will also match.
    #   We're trying to get around this by checking to see if the caption matches the IP, but no guarantees.
    
    $WhereClauses = "( [Nodes].Caption LIKE '%.%.%' AND [Nodes].Caption <> [Nodes].IPAddress )"
}

# If there are any where clauses, then let's put them together and add it to our base query
if ( $WhereClauses )
{
    # if there are clauses, then we need the "WHERE" keyword and a space for separation.
    # I'm choosing to put it on a separate line (`n) because I may want the $SwqlQuery more readable, but it doesn't matter for function
    $SwqlQuery += "`nWHERE "
    #             Join each of the WHERE clauses together with an " OR " (if we only have one, the -join does nothing)
    # Add it to the existing query string
    $SwqlQuery += $WhereClauses -join " OR "
}

$NodesToRename = Get-SwisData -SwisConnection $SwisConnection -Query $SwqlQuery
Write-Host "Found $( $NodesToRename.Count) node caption(s) to rename"

$NodesToRename | Add-Member -MemberType NoteProperty -Name NewCaption -Value "" -Force
# Copy Caption into NewCaption
ForEach ( $Node in $NodesToRename )
{
    $Node.NewCaption = $Node.Caption
}

if ( $Capitalize )
{
    ForEach ( $Node in $NodesToRename )
    {
        $Node.NewCaption = $Node.NewCaption.ToUpper()
    }
}

if ( $HostNameOnly )
{
    ForEach ( $Node in $NodesToRename )
    {
        $Node.NewCaption = $Node.NewCaption.Split(".")[0]
    }
}

# Remove any entries where Caption and NewCaption match (this shouldn't be necessary, but better safe)
# I'm using the "-cne" operator instead of the "-ne" because I want this case sensitive.
$NodesToRename = $NodesToRename | Where-Object { $_.Caption -cne $_.NewCaption }


# to actually do the rename
ForEach ( $Node in $NodesToRename )
{
    Write-Host "Updating caption on $( $Node.Caption ) to $( $Node.NewCaption )"
    # The parameter for -Properties is a hashtable of the field we want to update (Caption) and the value for it ($Node.NewCaption)
    Set-SwisObject -SwisConnection $SwisConnection -Uri $Node.Uri -Properties @{ Caption = $Node.NewCaption } -Verbose
}

 

 

If I can find a few more examples, then I might build this into a proper function.  If not, it'll likely live this way.

"Shoot for the stars to reach the moon"
Tags (3)
0 Kudos
4 Replies
MVP
MVP

What about posting this to your own public GitHub repo with a link so we can submit issues/PRs?

0 Kudos
Community Manager
Community Manager

I think I did it correctly, @mrxinu - care to see if this shows up for you?

https://github.com/kmsigma/SwqlQueries

"Shoot for the stars to reach the moon"
Community Manager
Community Manager

You assume, good sir, that I actually know how to properly use GitHub.  My skills are somewhat lacking.

"Shoot for the stars to reach the moon"
0 Kudos

Looks good! And no worries on the lagging GH stuff, you should see my backlog of stuff to learn right now. 🙄

0 Kudos