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 Autologin

I would like to create a PowerShell script that auto deletes a node, once a change ticket gets approved to decommission a server. This is a script that will run automatically, so I need a service account to log on to SolarWinds, and delete the node, and log off after completion.

I got the removal part done.

Any help is appreciated

  • Hi maxgo​,

    so you already have a user specified in your script that connects to SWIS and executes your desired tasks.

    You can basically run the script with any local admin user. I personally run my Powershell Scripts from Task scheduler.

    Here's how:How to run PowerShell scripts from Task Scheduler

    Hope that helps

  • Hi, did this answer your question?

    Thanks for a reply.

  • Vielen Dank Herr Doctor, unfortunately it did not answer my question, I need it to be part of a script, if our ticketing system generates a server decommission ticket it start a script, that deletes the VM, takes it out of our CMDB and AD, I also would like the same script to remove it from

    SolarWinds.

    Thank you

  • So if you let it run from a different server than your Orion Core, do you have the SDK installed?

    Do you have the connection String in the Script set to the IP-Address/DNS Name of your Orion Core machine?

    as long as the Script is kicked off on any machine with the SDK installed it should also remotely connect to SWIS.

    would you mind sharing parts of your script? Maybe a sanitized Version without passwords/IP Addresses etc.

  • Hi, here is what I got so far, still working on SolarWinds.

    param (
    [parameter(Mandatory=$True,Position=1)]
    [string] $VMserver
    )
    ##define Log
    #$LogFile = "E:\log\VMware\$VMserver.log"
    Start-Transcript -Path "E:\log\VMware\$VMserver.log"

    #>

    #Get-Module -Name VMware* -ListAvailable | Import-Module
    Import-Module vmware.powercli

    #Creating PSCredential object
    $User = "Domain\VM-Automation"
    $File = "E:\Log\user\User.txt"
    $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

    #Connect to vCenters.  vCenters are in Enhanced Link Mode (ELM)
    #Import-Module vmware.powercli
    Connect-VIServer -Server vcdev01 -AllLinked:$false -Credential $MyCredential


    <#

    The script is for decommissioning VMs

    The tasks are as follows:
    1) Replace VM backup tag with NO-Backup tag to stop VM backups
    2) Get VM information and state
    3) Power off VM
    4) Add annotations 'Notes' to VM, when it was powered off and why
    5) Move VM to the decommission folder
    6) Unmanage VM in SolarWinds

    #>


    ####Import-Module PowerOrion
    #Get current date
    $now =[DateTime]::Now


    #Get the VM name to be decommissioned from user
    ##$VMserver = Read-Host -Prompt 'Input VM server to be decommissioned'
    #Move to folder
    $folder = "ToBeDeleted"

    #Prevent the use of wild card searches-otherwise a lot of VMs could be powered off!
    if($VMserver -match '\*'){
        Write-Host -ForegroundColor red "Cannot use '*' wild card character in the VM name"
        Break
    }
    else{
    }


    #Verify VM name in vCenter
    if(Get-VM -Name $VMserver -ErrorAction SilentlyContinue){
        Write-Host -ForegroundColor green "Found VM: $VMserver"
    }
    else{
        Write-Host -ForegroundColor red "Can not find VM with the name $vmserver"
        break
    }


    #Replace VM Veeam backup tag to prevent continuous backups
    $TagCategory = Get-TagCategory "VM-Backup"
    $StopBackup = Get-Tag Veeam-NO-Backup
    Write-Host  -ForegroundColor green "Replacing Veeam backup Tag with No-Backup tag"
    Get-VM -Name $VMserver | Get-TagAssignment -Category $TagCategory | Remove-TagAssignment -Confirm:$false
    New-TagAssignment -Tag $StopBackup -Entity $VMserver


    #Get VM information
    $vm = Get-VM -Name $VMserver
    if($vm.Guest.State -eq "Running"){
        Get-VM $vm | select name, Powerstate, @{N="IPAddress"; E={$_.Guest.IPAddress[0]}}, @{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}
        $Nodename = (Get-VM $vm).ExtensionData.Guest.Hostname
    }
    elseif ($vm.Guest.State -eq "Running")
    {
        Write-Host  -ForegroundColor green "VM is Powered Off"
    }
    elseif ($vm.Guest.State -eq "NotRunning")
    {
    write-host -ForegroundColor green $vm "was powered off"
    }   


    #Do actions: shutdown VM, force VM off if the VM is hung or does not have VMtools running
    if($vm.Guest.State -eq "Running"){
        Write-Host -ForegroundColor green "VM is shutting down now!"
        Shutdown-VMGuest -VM $vm -Confirm:$false
        Start-Sleep -Seconds 25
    }
    else{
        Stop-VM -VM $vm -Confirm:$false -ErrorAction SilentlyContinue
    }


    #Add annotations 'Notes' to VM, when it was powered off and why
    Write-Host  -ForegroundColor green "Adding Notes to VM for documentation"
    Set-VM -VM $vm -Notes "$($vm.Notes) `n $now - Powered off Date for Decommissioning:" -Confirm:$false
    Start-Sleep -Seconds 2

    #Move VM to the specified folder
    Write-Host -ForegroundColor green "Moving the VM to the $folder folder"
    get-vm $vm | Move-VM -InventoryLocation $folder
    get-vm $vm | set-vm -name $vm-Retired -Confirm:$false -ErrorAction SilentlyContinue
    Disconnect-VIServer -Server * -Force -Confirm:$false
    <#
    #Test param
    #$Nodename = 'doghouse.Domain.local'

    #Remove all characters in the hostname to pass to SolarWinds node: for example ".Domain.local"
    $Node = $Nodename.Substring(0,$Nodename.IndexOf('.'))
    $Node


    if (-not ([string]::IsNullOrEmpty($Node))){
        Write-Host -ForegroundColor green "VMs Guest OS name is: $Node"
    }
    else{
        #Get the VM Guest OS name to match with SolarWinds node name
        Write-Host -ForegroundColor red "VMware cannot pull Guest OS name, possibly because the VM was previously powered off"
        $Node = Read-Host -Prompt 'Input VMs Guest name that corrosponds to SolarWinds node name'
    }

    <#
    #Unmanage VM in SolarWinds
    #
    #Import-Module PowerOrion
    #get-command -Module PowerOrion
    #Get-Help about_modules


    # Load the SolarWinds Powershell snapin. Needed in order to execute the script. Requires the Orion SDK 1.9 installed on the machine this script is running from.
    Add-PSSnapin SwisSnapin

    #$node = 'doghouse'
    # SolarWinds user name and password section. Create an Orion local account that only has node management rights. Enter the user name and password here.
    $username = Read-Host -Prompt 'Input your SolarWinds username'
    $password = Read-Host -Prompt 'Input your SolarWinds password'


    # This section allows the password to be embedded in this script. Without it the script will not work.
    $secstr = New-Object -TypeName System.Security.SecureString
    $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$secstr


    # The actual job
    $ORIONSERVERNAME = 'solarwinds.Domain.local'
    $nodename = $env:COMPUTERNAME
    $swis = Connect-Swis -Credential $cred -host $orionservername
    $nodeid = Get-SwisData $swis "SELECT NodeID FROM Orion.Nodes WHERE SysName LIKE '$Node'"
    $now =[DateTime]::utcNow
    $later =$now.AddMinutes(2)

    Invoke-SwisVerb $swis Orion.Nodes Unmanage @("N: $nodeid ",$now,$later, "false")

    Thank you

    Max

  • So it seems you already got it.

    you can either store your password in the Script here:

    # SolarWinds user name and password section. Create an Orion local account that only has node management rights. Enter the user name and password here.

    $username = your-sdk-username

    $password = your-sdk-user-password

    and modify this Line $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$secstr

    to this

    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password

    Having this said, the password of your Orion User is then stored in plaintext in the Script, but you already have the Option in the script to have it encrypted.

    Be aware that you need to encrypt the password using a powershell run by the User who will be kicking off the scripts.