Update vRealize Operations Maintenance Mode.ps1

Since Get-Inventory does not return Datastore Objects, added switch "IsDatastore" to specify Datastore Objects being passed to the function.
This commit is contained in:
ashishsharma-git
2021-01-25 21:37:23 +05:30
committed by GitHub
parent 29c2cc0221
commit 066dbb25f7

View File

@@ -10,6 +10,10 @@ function Enable-OMMaintenance {
.Example .Example
Set All VMs with a name as backup as being in maintenance mode for 20 minutes: Set All VMs with a name as backup as being in maintenance mode for 20 minutes:
Since Get-Inventory does not return Datastore Objects, we need to specify we're passing Datastore objects using switch "IsDatastore"
Get-Datastore DS1 | Enable-OMMaintenance -MaintenanceTime 20 -IsDatastore True
Get-VM backup* | Enable-OMMaintenance -MaintenanceTime 20 Get-VM backup* | Enable-OMMaintenance -MaintenanceTime 20
Name Health ResourceKind Description Name Health ResourceKind Description
@@ -33,10 +37,28 @@ function Enable-OMMaintenance {
[Parameter(Position=1, Mandatory=$true)] [Parameter(Position=1, Mandatory=$true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[Int] [Int]
$MaintenanceTime $MaintenanceTime,
[Parameter(Position=2, Mandatory=$false)]
[ValidateSet(True, False)]
[System.String]
$IsDatastore= $false
) )
process { process {
Foreach ($Entry in $resource) { Foreach ($Entry in $resource) {
if ($IsDatastore -eq $true)
{
$Item = Get-Datastore -Name $Entry | Get-OMResource
if (-not $Item) {
throw "$Entry not found"
} Else {
$Item.ExtensionData.MarkResourceAsBeingMaintained($MaintenanceTime)
Get-Datastore -Name $Entry | Get-OMResource
}
}
Else
{
$Item = Get-Inventory -Name $Entry | Get-OMResource $Item = Get-Inventory -Name $Entry | Get-OMResource
if (-not $Item) { if (-not $Item) {
throw "$Entry not found" throw "$Entry not found"
@@ -47,6 +69,7 @@ function Enable-OMMaintenance {
} }
} }
} }
}
function Disable-OMMaintenance { function Disable-OMMaintenance {
<# <#
@@ -60,6 +83,10 @@ function Disable-OMMaintenance {
.Example .Example
Disable maintenance mode for all VMs with a name of backup Disable maintenance mode for all VMs with a name of backup
Since Get-Inventory does not return Datastore Objects, we need to specify we're passing Datastore objects using switch "IsDatastore"
Get-Datastore DS1 | Disable-OMMaintenance -IsDatastore True
Get-VM backup* | Disable-OMMaintenance Get-VM backup* | Disable-OMMaintenance
Name Health ResourceKind Description Name Health ResourceKind Description
@@ -78,10 +105,27 @@ function Disable-OMMaintenance {
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[System.String] [System.String]
$Resource $Resource,
[Parameter(Position=1, Mandatory=$false)]
[ValidateSet(True, False)]
[System.String]
$IsDatastore= $false
) )
process { process {
Foreach ($Entry in $resource) { Foreach ($Entry in $resource) {
If ($IsDatastore -eq $true)
{
$Item = Get-Datastore -Name $Entry | Get-OMResource
if (-not $Item) {
throw "$Entry not found"
} Else {
$Item.ExtensionData.UnmarkResourceAsBeingMaintained()
Get-Datastore -Name $Entry | Get-OMResource
}
}
Else
{
$Item = Get-Inventory -Name $Entry | Get-OMResource $Item = Get-Inventory -Name $Entry | Get-OMResource
if (-not $Item) { if (-not $Item) {
throw "$Entry not found" throw "$Entry not found"
@@ -92,6 +136,7 @@ function Disable-OMMaintenance {
} }
} }
} }
}
#Write-Host "Enable a single host as being in maintenance mode for 1 minute" #Write-Host "Enable a single host as being in maintenance mode for 1 minute"
#Enable-OMMaintenance -Resource ESX-01a* -MaintenanceTime 1 #Enable-OMMaintenance -Resource ESX-01a* -MaintenanceTime 1