From e709f1fddea0bc133d33ff7d280b7226f1b8eb8b Mon Sep 17 00:00:00 2001 From: Alan Renouf Date: Fri, 15 Jul 2016 16:47:08 -0700 Subject: [PATCH 1/2] Pester Tests for VC Connection Pester Tests for VC Connection --- Pester/Test Connection to VC.ps1 | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Pester/Test Connection to VC.ps1 diff --git a/Pester/Test Connection to VC.ps1 b/Pester/Test Connection to VC.ps1 new file mode 100644 index 0000000..cf017bc --- /dev/null +++ b/Pester/Test Connection to VC.ps1 @@ -0,0 +1,40 @@ +<# +Script name: Test Connection to VC.ps1 +Created on: 07/15/2016 +Author: Alan Renouf, @alanrenouf +Description: The purpose of this pester test is to ensure the PowerCLI modules are imported and a connection and disconnection can be made to a vCenter +Dependencies: Pester Module +#> + +$VCNAME = "MyVC@Mydomain.local" +$VCUSER = "Administrator@vsphere.local" +$VCPASS = "Admin!23" + +Describe "PowerCLI Tests" { + It "Importing PowerCLI Modules" { + Get-Module VMware* | Foreach { + Write-Host "Importing Module $($_.name) Version $($_.Version)" + $_ | Import-Module + Get-Module $_ | Should Be $true + } + } +} + +Describe "Connect-VIServer Tests" { + + $connection = Connect-VIServer $VCName -User $VCUser -password $VCPass + It "Connection is active" { + $Global:DefaultVIServer[0].isconnected | Should Be $true + } + + It "Checking connected server name is $VCName" { + $Global:DefaultVIServer[0].name | Should Be $VCName + } +} + +Describe "Disconnect-VIServer Tests" { + It "Disconnect from $VCName" { + Disconnect-VIServer $VCName -confirm:$false + $Global:DefaultVIServer | Should Be $null + } +} \ No newline at end of file From ca70d01e54cd30697faabd5fb66b47fe4c8e022e Mon Sep 17 00:00:00 2001 From: Alan Renouf Date: Tue, 26 Jul 2016 22:35:41 -0700 Subject: [PATCH 2/2] Update pester and add vrops script Updated Pester test so it can be called with parameters and included vROPS Maintenance Mode Script --- Pester/Test Connection to VC.ps1 | 10 +- .../vRealize Operations Maintenance Mode.ps1 | 116 ++++++++++++++++++ 2 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 Scripts/vRealize Operations Maintenance Mode.ps1 diff --git a/Pester/Test Connection to VC.ps1 b/Pester/Test Connection to VC.ps1 index cf017bc..006825a 100644 --- a/Pester/Test Connection to VC.ps1 +++ b/Pester/Test Connection to VC.ps1 @@ -4,11 +4,15 @@ Created on: 07/15/2016 Author: Alan Renouf, @alanrenouf Description: The purpose of this pester test is to ensure the PowerCLI modules are imported and a connection and disconnection can be made to a vCenter Dependencies: Pester Module +Example run: + +Invoke-Pester -Script @{ Path = '.\Test Connection to VC.ps1'; Parameters = @{ VCNAME="VC01.local"; VCUSER="Administrator@vsphere.local"; VCPASS="Admin!23"} } + #> -$VCNAME = "MyVC@Mydomain.local" -$VCUSER = "Administrator@vsphere.local" -$VCPASS = "Admin!23" +$VCUSER = $Parameters.Get_Item("VCUSER") +$VCPASS = $Parameters.Get_Item("VCPASS") +$VCNAME = $Parameters.Get_Item("VCNAME") Describe "PowerCLI Tests" { It "Importing PowerCLI Modules" { diff --git a/Scripts/vRealize Operations Maintenance Mode.ps1 b/Scripts/vRealize Operations Maintenance Mode.ps1 new file mode 100644 index 0000000..a6ff9e5 --- /dev/null +++ b/Scripts/vRealize Operations Maintenance Mode.ps1 @@ -0,0 +1,116 @@ +Connect-VIServer vc-l-01a.corp.local -User administrator@vsphere.local -Password VMware1! +Connect-OMServer vrops.corp1.local -User admin -Password VMware1! + +function Enable-OMMaintenance { + <# + .NOTES + Script name: vRealize Operations Maintenance Mode.ps1 + Created on: 07/26/2016 + Author: Alan Renouf, @alanrenouf + Dependencies: PowerCLI 6.0 R2 or later + .DESCRIPTION + Places a vSphere Inventory object into maintenance mode in vRealize Operations + .Example + Set All VMs with a name as backup as being in maintenance mode for 20 minutes: + + Get-VM backup* | Enable-OMMaintenance -MaintenanceTime 20 + + Name Health ResourceKind Description + ---- ------ ------------ ----------- + backup-089e13fd-7d7a-0 Grey VirtualMachine + backup-d90e0b39-2618-0 Grey VirtualMachine + backup-e48ca842-316a-0 Grey VirtualMachine + backup-77da3713-919a-0 Grey VirtualMachine + backup-c32f4da8-86c4-0 Grey VirtualMachine + backup-c3fcb95c-cfe2-0 Grey VirtualMachine + backup-4318bb1e-614a-0 Grey VirtualMachine + + #> + [CmdletBinding()] + param( + [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Resource, + + [Parameter(Position=1, Mandatory=$true)] + [ValidateNotNullOrEmpty()] + [Int] + $MaintenanceTime + ) + process { + Foreach ($Entry in $resource) { + $Item = Get-Inventory -Name $Entry | Get-OMResource + if (-not $Item) { + throw "$Entry not found" + } Else { + $Item.ExtensionData.MarkResourceAsBeingMaintained($MaintenanceTime) + Get-Inventory -Name $Entry | Get-OMResource + } + } + } +} + +function Disable-OMMaintenance { + <# + .NOTES + Script name: vRealize Operations Maintenance Mode.ps1 + Created on: 07/26/2016 + Author: Alan Renouf, @alanrenouf + Dependencies: PowerCLI 6.0 R2 or later + .DESCRIPTION + Removes a vSphere Inventory object from maintenance mode in vRealize Operations + .Example + Disable maintenance mode for all VMs with a name of backup + + Get-VM backup* | Disable-OMMaintenance + + Name Health ResourceKind Description + ---- ------ ------------ ----------- + backup-089e13fd-7d7a-0 Grey VirtualMachine + backup-d90e0b39-2618-0 Grey VirtualMachine + backup-e48ca842-316a-0 Grey VirtualMachine + backup-77da3713-919a-0 Grey VirtualMachine + backup-c32f4da8-86c4-0 Yellow VirtualMachine + backup-c3fcb95c-cfe2-0 Yellow VirtualMachine + backup-4318bb1e-614a-0 Yellow VirtualMachine + + #> + [CmdletBinding()] + param( + [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Resource + ) + process { + Foreach ($Entry in $resource) { + $Item = Get-Inventory -Name $Entry | Get-OMResource + if (-not $Item) { + throw "$Entry not found" + } Else { + $Item.ExtensionData.UnmarkResourceAsBeingMaintained() + Get-Inventory -Name $Entry | Get-OMResource + } + } + } +} + +Write-Host "Enable a single host as being in maintenance mode for 1 minute" +Enable-OMMaintenance -Resource ESX-01a* -MaintenanceTime 1 + +Write-Host "List All Host Resources and their state" +Get-OMResource ESX-* | Select Name, State | FT + +Write-Host "Set All VMs with a name as backup as being in maintenance mode for 20 minutes" +Get-VM backup* | Enable-OMMaintenance -MaintenanceTime 20 + +Write-Host "List All Backup VM Resources and their state" +Get-VM backup* | Get-OMResource | Select Name, State | FT + +Write-Host "Disable maintenance mode for all VMs with a name as backup as we have completed our scheduled work" +Get-VM backup* | Disable-OMMaintenance + +Write-Host "List All VM Resources and their state" +Get-VM backup* | Get-OMResource | Select Name, State | FT +