My Blog List

Thursday, May 23, 2024

Powershell(VMWare)


 Import-Module $env:SMS_ADMIN_UI_PATH.Replace("bin\i386","bin\ConfigurationManager.psd1") -Global

import-module AHToolKit

Import-Module VMware.VimAutomation.Core


Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false


# use upn for credential 


$MyCredential = Get-Credential


$Vcenters = "axxxx01","acxxx01"

$vcenters = "172.2x.xxx.xxx","172.2x.xxx.xxx"


$vcenters | % {

    Start-Sleep -Seconds 1

    Connect-VIServer -Server $_ -User $MyCredential.UserName -Password $MyCredential.GetNetworkCredential().Password

}

Start-Sleep -Seconds 2


$LegacyVcenters | % {

    Start-Sleep -Seconds 1

    Connect-VIServer -Server $_ -User $MyCredential.UserName -Password $MyCredential.GetNetworkCredential().Password

}



# Axxxxxxxxxx01 ESXi server

Connect-VIServer -Server 172.xxx.xx.xxx -User root -Password "xyxyxyxyxyxy"

############ Remove Snapshot #################


# Get-Vm $List | Get-Snapshot | ? { $_.Description -eq "Security Update OS" } | Remove-Snapshot -RunAsync -Confirm:$false -ErrorAction SilentlyContinue

# Get-Vm $List | Get-Snapshot | ? { $_.Description -like "Security Update OS*" } | Select VM,Name,Description 

# Get-Vm $List | Get-Snapshot | Select VM,Name,Description

# Get-Vm $List | Get-Snapshot | ? { $_.Name -like "Patch Tuesday*" } | Remove-Snapshot -RunAsync -Confirm:$false -ErrorAction SilentlyContinue



# Patch Tuesday - Remove all snapshot

# Get-Vm | Get-Snapshot | ? { $_.Description -eq "Security Update OS" } | Remove-Snapshot -RunAsync -Confirm:$false -ErrorAction SilentlyContinue

# Get-Vm | Get-Snapshot | ? { $_.Description -eq "Security Update OS" } | Select VM,Name,Description 

#############  New snapshot ###################


# $month = "August"

# Get-VM $List | ? {$_.PowerState -eq "PoweredOn"} | New-Snapshot -Name "Patch Tuesday August" -Description "Security Update OS" -RunAsync -Quiesce

########### Patching Snapshot function #################

########### Warning: This functon uses asynsc snapshooting. Do not take snapshots if there is existing snapshot been removed, otherwise multiple snapshots for same machine will be created ########



$PatchMonth = "December"

$SnapshotStartTime = Get-Date

$Count = 0


Function Run-SnapShot ($List) {


    $LapseTime = New-TimeSpan -Start  $SnapshotStartTime -End (Get-Date)

    if ($LapseTime.TotalSeconds -gt 3600){

        Return

    }

    

    $FailedSnapshot = @()

    

    $List | % {  

        # Get-Vm $_ | Get-Snapshot | ? { $_.Description -like "Security Update *" }

        $Test4SS = Get-Vm $_ | Get-Snapshot | ? { $_.Name -like "Patch Tuesday $PatchMonth" }

        if (-not $Test4SS){

            

            $FailedSnapshot += $_

        }

        else { 

            

            $Test4SS | Select VM, Created

            

        }

    }

    

    if ($FailedSnapshot.Count -gt 0) { 

         $FailedSnapshot

         if ($Count -gt 3) {

            Get-VM $FailedSnapshot | ? {$_.PowerState -eq "PoweredOn"} | New-Snapshot -Name "Patch Tuesday $PatchMonth" -Description "Security Update OS - Non Quiesce" -RunAsync

         }

         else {

            Get-VM $FailedSnapshot | ? {$_.PowerState -eq "PoweredOn"} | New-Snapshot -Name "Patch Tuesday $PatchMonth" -Description "Security Update OS" -RunAsync -Quiesce

         }


         Start-Sleep -Seconds 600         

         $Count++

         Run-Snapshot $List

    }

    else {

        Return

    }

}

Run-Snapshot $List

No comments:

Post a Comment

Steps to extract ntfs share details from file server shares

As part of a file server consolidation effort, I was tasked to come up with a method to move multiple shares on multiple shares to a WSFC. I...