Showing posts with label PowerCLI Powershell VMware. Show all posts
Showing posts with label PowerCLI Powershell VMware. Show all posts

Friday, July 8, 2011

Get VMSS Files

VMSS files are suspended state files which store the state of a suspended VMware's virtual machine. Sometimes they linger on even when the VM has been resumed from suspension. They can take up a lot of space. This script will find those files and write them out to a file.

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

$vCenters = 'vCenter1', 'vCenter2'
foreach ($vCenter in $vCenters)
{

 $VIServer = Connect-VIServer $vCenter
 foreach ($VM in (Get-View -ViewType VirtualMachine ))
  {
  $x = $VM.LayoutEx.File
  Foreach ($File in $x)
   {
   if ($File.Name -like '*.vmss')
    {
    $output = $File.Name
    $output
    $output | Out-File c:\vmss.txt -Append
    }
   }
  }
}

Tuesday, July 5, 2011

Find VMs with Raw Device Mappings

#Clear screen
cls

# remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#connect to Virtual Center Server
Connect-VIServer vCenter1

#get VMs
foreach($vm in (get-view -ViewType VirtualMachine))
{
  foreach($rdm in $vm.Config.Hardware.Device)
  {
   if(($rdm.gettype()).Name -eq "VirtualDisk")
   {
    if(($rdm.Backing.CompatibilityMode -eq "physicalMode") -or ($rdm.Backing.CompatibilityMode -eq "virtualMode"))
    {
     $Details = "" | select VMName, VMDK, Mode
     $Details.VMName = $vm.Name
     $Details.VMDK = $rdm.Backing.FileName
     $Details.Mode = $rdm.Backing.CompatibilityMode
     $Details.PSTypeNames.Clear()
     $Details
    }
   }
  }
}
    
Disconnect-VIServer vCenter1 -Confirm:$false

Tuesday, June 21, 2011

VMs with duplicate MACs

We had quite a bit of trouble with vms that had duplicate MAC addresses in our environment. VMware said it should happen rarely but that hasn't been the case for us. So I wrote this handy script to find them for us.

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#where $vCenters are the names of your vCenters
$vCenters = 'vCenter1','vCenter2'
foreach ($vCenter in $vCenters)
{
#connect to vCenter
Connect-VIServer $vCenter 
$mac = @{}
foreach ($vm in Get-VM)
    {
    Get-NetworkAdapter -VM $vm | foreach
        {
         $net = $_.networkname
         if($mac.ContainsKey($_.MacAddress) -and $vm.Name -notlike $mac[$_.MacAddress])
           {
           if ($net -notlike $_.networkname)
            {
            Write-Host "Duplicate MAC address" $_.MacAddress "in" $vm.Name "and" $mac[$_.MacAddress]
   $output = "Duplicate MAC address $_.MacAddress in $vm.Name and $mac[$_.MacAddress]"
   $output | Out-File c:\dupmac.txt -Append
        }else{
            $mac[$_.MacAddress] = $vm.Name
            }
        }
    }
}
Disconnect-VIServer -Server $vCenter -Confirm:$false
}

Friday, June 17, 2011

Power Off All VMs in a cluster

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#Connect to the vCenter where 'vCenter1' is the name of your vCenter
Connect-VIServer vCenter1 

#Get all VMs in the a cluster resource Pool
$vms = Get-VM
Foreach ($vm in $vms)

      #Power Down the Test VM Guests 
       $vm | Shutdown-VMGuest -Confirm:$false

Disconnect-VIServer vCenter1 -Confirm:$false

Wednesday, June 15, 2011

Add vLAN to ESX host

#clear screen
cls
#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
#add VMware snapin
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
#vCenter is the fqdn of your vCenter
Connect-VIServer -Server vCenter 
#Cluster1 is the name of your cluster
foreach ($ESXhost in get-cluster Cluster1 | get-vmhost)
{
  #VSWITCH1 is the name of your virtual swtich
  get-virtualswitch -vmhost $ESXhost -name VSWITCH1 | new-virtualportgroup -name vLAN_201 -vlanid 201
}

Tuesday, June 14, 2011

Count VMs

#clear screen
cls
#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
#add VMware snapin
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
#allow connection to mulitple vCenters
$set = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -confirm:$false
$set Out-Null #don't write output
foreach ($vCenter in 'vCenter1','vCenter2','vCenter3')
{
$connect = Connect-VIServer -Server $vCenter #connect to each vCenter
$connect Out-Null #don't write output
$vms = Get-VM
}
write-host "The total number of VMs in the environment is" $vms.count