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

Monday, June 27, 2011

Count the Documents and the Words in the Documents in a Directory

#Clear screen
cls

$intDocs = $intWords = $null
$path = "C:\Users\owner\Documents\Harlequin\Count"
$application = New-Object -ComObject word.application
$application.visible = $false
Get-ChildItem -Path $path -include *.doc,*.docx -Recurse |
ForEach-Object {
"Processing $($_.fullname)"
$document = $application.documents.open($_.fullname)
$intWords += $document.words.count
$intDocs ++
$document.close() | out-null
}
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
"There are $intDocs documents in the $path folder. They contain a total of $intWords words."

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

Witty Banter and EMC cmdlets

I should have warned you that unlike the "Hey Scripting Guy" guys, I don't really plan on providing witty banter or even any real instruction on the scripts that I post. They are yours for the taking to muddle through as best you can.

I also wanted to mention that the EMC cmdlets are out. https://community.emc.com/docs/DOC-8561
They are very cool for helping to manage your EMC storage. Look for scripts to come.

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
}