Category: Powershell

Disable Solarwinds Alerting With PowerShell

A customer is currently using solarwinds to monitor there virtual infastructure, when they do there patching they need to login to the solarwinds console, and manually step though each of the virtual machines/objects they are going to patch and put them into maintenance mode so the on-call guy doesnt get flooded with alerts.

To help matters and save a bit of time i used the below piece of powershell scripting to take away that manual task and only require a text file with a list of the servers to be modified (this is assuming you have the swisPowershell installed:

1
Install-Module -Name SwisPowerShell

swmaintme.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Check if the powershell module that is used is actually loaded and if not load it up
Import-Module SwisPowerShell  
# Hours passed when running script e.g (./swmaintme.ps1 12) will set maintance for 12 hours
$hours=$args[0]
# Where is the server file located (file is a text file with just server names not FQDN)
$serverlist = Get-Content -Path "'path to imput text file'\unmanageme.txt"
# What is the solarwinds server (can only be ran from here as port 17777 is not open remotely :( )  
$strsolarWindServer="Solarwinds Server name Here"  
# Lets connected to the server listed above nice and trusted
$swis = Connect-Swis -Hostname $strsolarWindServer -Trusted
# For each time you look at a line in the text file above do this >>>>>
foreach($server in $serverlist){  
 
    $strQuery = "SELECT uri FROM Orion.Nodes WHERE SysName LIKE '" + "$server" + "%'"  
    $uris = Get-SwisData $swis $strQuery
#   Important line where we actually set the server to unmanaged and status 9 and then set it to maintance from when script was run to the hours we said at start  
    $uris | ForEach-Object { Set-SwisObject $swis $_ @{Status=9;Unmanaged=$true;UnmanageFrom=[DateTime]::UtcNow;UnmanageUntil=[DateTime]::UtcNow.AddHours($hours)}}  
}

Once you maintenance is finished and i the following script will take the same input file and put them back into monitoring mode, unless of course you wish to wait till your maintenance period you specified in the first script ends.

swunmaintme.ps1

1
2
3
4
5
6
7
8
9
10
Import-Module SwisPowerShell  
$serverlist = Get-Content -Path "'path to imput text file'"\unmanageme.txt"
$strsolarWindServer="
Solarwinds Server name Here"  
$swis = Connect-Swis -Hostname $strsolarWindServer -Trusted

foreach($server in $serverlist){  
    $strQuery = "
SELECT uri FROM Orion.Nodes WHERE SysName LIKE '" + "$server" + "%'"  
    $uris = Get-SwisData $swis $strQuery
    $uris | ForEach-Object { Set-SwisObject $swis $_ @{Status=1;Unmanaged=$false}}  
}

Export A Running VM From vCloud

Late last year i wrote about identifying chain linked VM’s within the vCloud environment, since then ive had to export a number of virtual machines from vCloud back to native vSphere.

For some of the process we need to use the ManagedBy.PS1 powershell script provided by VMware in this artice (its attached) https://kb.vmware.com/s/article/2032366

The script that i wrote to do this is below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Export VM script for vCloud (required ManagedBy Script from VMware)

# Variables for the vApp Decommission.
$disablespec = New-Object VMware.Vim.VirtualMachineConfigSpec
$disablespec.vAppConfigRemoved = $True

# Text file with list of VM's we want to export from vCloud
$vms = Get-Content "vms.txt"

Foreach ($vm in $vms) {

$vm = "$vm"

Write-Host "Cleaning UUID UP :" $vm
#Remove vCloud UUID settings in vmx configuration
Get-AdvancedSetting -entity "$vm" -Name cloud.uuid|Remove-AdvancedSetting

Write-Host "Unlinking VM from vCloud Management :" $vm
# Unlink managed by
Invoke-Expression -Command '.\ManagedBy.ps1 -Cmd Clear -VMs $vm'

Write-Host "Removing vApp Flag :" $vm
#Remove vApp Flag
$vm = Get-VM $vm | Get-View
$vm.ReconfigVM($disablespec)

}

This successfully cleans up the VM removing the UUID in advanced settings are un-linking it from vcloud, it also changes the VM from a vApp to a standard VM. What you will be left with is the same VM with a (UUID reference number after it). Should you wish to remove this UUID referance number you can do afterwards with the script below, however check your backup technology before hand as it may see the changes as a new system and remove your historical references.

1
2
3
4
5
6
7
8
9
10
11
# Same text file that was used for the vCloud Export
$vms = Get-Content "vms.txt"

Foreach ($vm in $vms) {

    $vm = "$vm"
    Write-Host "Current VM Name :" $vm
    $newvm = $vm.Substring(0, $vm.IndexOf('('))
    Write-Host "New VM Name :" $newvm
    Set-VM -VM $vm -Name $newvm
}

And that should be it.