Wednesday, July 13, 2022

Virtual machine re-registration, disconnected virtual machine/ read-only VM virtual machine- VMWARE - PowerShell

 Import-Module VMware.VimAutomation.Core

#Run this by opening VMware PowerCLI tool and connect using connect-VIserver

$pscrd = Get-Credential

Connect-VIServer -Server 10.106.202.151 -Credential $pscrd

Write-Host "Current script directory is $ScriptDir"


$CurrDate = Get-Date -Format "yyyyMMdd_HHmmss"

$SubCurrDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"


$agree= "N"

Write-Host "Before you run the code. Makesure, to have required back-up of VMs for any recovery" -BackgroundColor DarkRed

$agree = Read-Host "You want to proceed: Y/N"

If( $agree -eq "Y" -or $agree -eq "y" )

{

#Flag to validate the ZERO Byte VMs

$flag=0

# This will get us the complete list of VMs in the inventory

#$vms = Get-VM

$vms = Get-VM #-Name TESTVM_KDC_03

$status_coll = @()

$vm_coll = @()

# Loop-in throught the Vm with various validation and checks

foreach($vm in $vms)

{


$regvm = "" | Select-Object VMName,PowerState,UsedSpaceGB,vmPathName,Remarks

$vm_state = "" | Select-Object VMName,PowerState,UsedSpaceGB,vmPathName,Remarks

  If($vm.UsedSpaceGB -eq 0 -And $vm.PowerState -eq "PoweredOn")

    {

         # If a VM is found to be a Zero byte space

         # execute the registration process

         $flag=1

         $vmname = $vm.Name

         #Write-Host "This is a Zero Byte VM and is in PowerON state: $vm.Name"

         #code to re-register the VM from the Inventory

         $resourcePool = Get-ResourcePool -VM $vm

         $vmPathName = $vm.ExtensionData.Config.Files.VmPathName

        #VM resource details

        $vm_state.VMName = $vmname

         $vm_pstate = $vm.PowerState

         $vm_state.PowerState = $vm_pstate

         $vm_usedspaceGB = $vm.UsedSpaceGB

         $vm_state.UsedSpaceGB = $vm_usedspaceGB

        $vm_state.vmPathName = $vmPathName

         $vm_state.Remarks = "DataCollection"

         get-vm $vmname | Stop-VM -confirm:$false

         $vm_stp_status = get-vm $vmname

         #Validate VM status condition block to remove from Inventory and re-registration

         If($vm_stp_status.PowerState -eq "PoweredOff") 

         {

          

             # Knock off the VMs from the Inventory without deleting the vmx file from the pool

           # Which is made as false and never make it true or remove the below Switch else Data loss will happen

            #Cautious note touch the below line only when you are aware what you are doing.

             Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false

             New-VM -VMFilePath $vmPathName -ResourcePool $resourcePool -Confirm:$false

            $vm_new_status = get-vm $vmname

             start-vm $vm_new_status -confirm:$false

                  $vm_strt_status = get-vm $vmname

                  #If the start of VM is successfull on the return

                    If($vm_strt_status.PowerState -eq "PoweredOn" -and $vm_strt_status.UsedSpaceGB -ne 0)

                      {

                       # Collect the server saying sucessfull registration

                       $regvm.VMName = $vmname

                       $vm_strt_pstate = $vm_strt_status.PowerState

                       $regvm.PowerState = $vm_strt_pstate

                       $vm_strt_usedspaceGB = $vm_strt_status.UsedSpaceGB

                       $regvm.UsedSpaceGB = $vm_strt_usedspaceGB

                       $regvm.vmPathName = $vmPathName

                       $regvm.Remarks = "Registered successfully"

                      }

                    Else

                      {

                       $regvm.VMName = $vmname

                       $vm_strt_pstate = $vm_strt_status.PowerState

                       $regvm.PowerState = $vm_strt_pstate

                       $vm_strt_usedspaceGB = $vm_strt_status.UsedSpaceGB

                       $regvm.UsedSpaceGB = $vm_strt_usedspaceGB

                       $regvm.vmPathName = $vmPathName

                       $regvm.Remarks = "Couldn't register, Please check Manually"

                      }            

           }

           Else

           {

            #Couldn't stop VM for re-registration process

            $regvm.VMName = $vmname

            $vm_stp_pstate = $vm_stp_status.PowerState

            $regvm.PowerState = $vm_stp_pstate

            $vm_strt_usedspaceGB =  $vm_stp_status.UsedSpaceGB

            $regvm.UsedSpaceGB = $vm_strt_usedspaceGB

            $regvm.vmPathName = $vmPathName

            $regvm.Remarks = "Couldn't stop VM for re-registration process"

           }

         }

         

        $status_coll = $status_coll +  $regvm

        $vm_coll = $vm_coll + $vm_state

        

}

}

Write-Host "Vmware Registration Status" -BackgroundColor DarkCyan

$status_coll

Write-Host "Vmware Info details" -BackgroundColor DarkMagenta

$vm_coll

$reg_path = $ScriptDir + "\InventoryRegistration.csv"

$status_coll | Export-Csv -Path $reg_path -NoTypeInformation

$vmstatuspath = $ScriptDir + "\Vms_status.csv"

$vm_coll | Export-Csv -Path $vmstatuspath -NoTypeInformation

If( $flag -eq 0)

{

Write-Host "No VMs with Zero Byte found with PoweredON state"

}


No comments:

Post a Comment