Friday, November 4, 2011

VMware Tools Advance Config

Here is a good script for changing the VMware Tools Advanced Config options (Tools Update/Time Sync).

######################################
#
# VMware Tools Advanced Config
#
######################################

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

##############################
# Connect to specified vCenter
##############################

Connect-VIServer -server $vCenterName -Protocol https -User USERNAME -Password PASSWORD
##############################
# Get collection of VMs, create log file
##############################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\VMware-Tools-AdvConfig_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"

##############################
# Get variables for each VM
##############################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vmID = get-vm $VM | foreach {get-view $_.Id}
    $vmToolsStatus = $vmID.Guest.ToolsStatus

##############################
# Change to Manual or forced update/install
##############################

#$vm = Get-VM -Name "virtualmachine"
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.changeVersion = $VM.ExtensionData.Config.ChangeVersion
$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
$spec.tools.toolsUpgradePolicy = "manual"
#$spec.tools.toolsUpgradePolicy = "upgradeAtPowerCycle"

$_this = Get-View -Id $VM.Id
$_this.ReconfigVM_Task($spec)

    Write-Host $VMName `t $spec -ForegroundColor Green
    Add-Content -Path $logFile -Value ($VMName + $spec)
   
##############################
# Change Time Sync
##############################

#$vm = Get-VM -Name "virtualmachine"
#$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
#$spec.changeVersion = $VM.ExtensionData.Config.ChangeVersion
#$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
#$spec.tools.syncTimeWithHost = $true
#$spec.tools.syncTimeWithHost = $false

#$_this = Get-View -Id $VM.Id
#$_this.ReconfigVM_Task($spec)

#    Write-Host $VMName `t $spec -ForegroundColor Green
#    Add-Content -Path $logFile -Value ($VMName + $spec)

    }

##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################


Thursday, October 13, 2011

List VMware Tools versions

######################################
#
# VMware Tools version
#
######################################

######################################
# Custom Cmdlet
######################################

New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine `
    -ValueFromExtensionProperty 'config.tools.ToolsVersion' `
    -Force
   
######################################

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
#$clusterName = Read-Host "Which Cluster"

##############################
# Connect to specified vCenter
##############################

Connect-VIServer -server $vCenterName -Protocol https -User username -Password password

##############################
# Get collection of VMs, create log file
##############################

#$colVMs = get-cluster -name $clusterName | Get-VM
$colVMs = Get-VM
$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\VMware-Tools-Version_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"
$VMwareTools = "d:\scripts\ScriptOutput\VMwareTools_Versions_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).csv"

##############################
# Get variables for each VM
##############################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vmID = get-vm $VM | foreach {get-view $_.Id}
    $vmToolsStatus = $vmID.Guest.ToolsStatus
    $vmToolsVersion = $vmID.Guest.ToolsVersion

##############################
# Prepare Data for CSV
##############################

    # Formatting the output
    $output = New-Object PSObject
    $output | Add-Member -MemberType NoteProperty -name Name -value $VMName
    $output | Add-Member -MemberType NoteProperty -name ToolsStatus -value $vmToolsStatus
    $output | Add-Member -MemberType NoteProperty -name ToolsVersion -value $vmToolsVersion
    $output | Add-Member -MemberType NoteProperty -name OperatingSystem -value $OS
    $output | Add-Member -MemberType NoteProperty -name Cluster -value $clusterName
    Write-Output $output | Export-Csv $VMwareTools -noType -Append

    Write-Host $VMName `t $vmToolsStatus `t $OS -ForegroundColor Green
    Add-Content -Path $logFile -Value ($VMName + "`t`t`t`t" + $vmToolsStatus + "`t`t`t`t" + $vmToolsVersion + "`t`t`t`t" + $OS)
}
##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################

Thursday, September 22, 2011

PowerCLI Custom ESXi5.0 Build ISO

This script came about from myself and Dominic trying to automate the Build ISO process.

********************************************************************

#####################################################
#
# Build ISO for PXE Booting
# Requires PowerCLI v5.0
# Requires WinSCP v4.3.x (For SCP to Linux Server)
#
#####################################################

######################################
# Global Vars
######################################

$VibListOutput = "D:\scripts\ScriptOutput\CustomESXi5.0_VibList_Output_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).txt"
$CustomESXiISO = "D:\esxi_custom\CustomBuildISOs\ESXi5.0_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).iso"

######################################
# Adding Software Depots
######################################

Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\ESXi500-201109001.zip
Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\EMCPower.VMWARE.5.7.b173.zip
Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\hp-esxi5.0uX-bundle-1.0-20.zip
Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\hp-HPUtil-esxi5.0-bundle-1.0-23.zip
Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\hp-nmi-esxi5.0-bundle-2.0-11.zip
Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\be2net-4.0.355.1-offline_bundle-487292.zip
Add-EsxSoftwareDepot -DepotUrl D:\esxi_custom\lpfc820-8.2.2.105.34-offline_bundle-476869.zip

######################################
# Create/Clone Custom Profile
######################################

Get-EsxImageProfile
New-EsxImageProfile -CloneProfile ESXi-5.0.0-20110904001-standard -Name "ESXi5.0Custom"

######################################
# Get Package List to find what you
# want to add into the profile
# - Run in PowerCLI Shell seperatly -
######################################

#Get-EsxSoftwarePackage

######################################
# Add Packages into Profile
######################################

Add-EsxSoftwarePackage -ImageProfile VIG -SoftwarePackage hpnmi,char-hpcru,char-hpilo,hponcfg,hpbootcfg,hp-smx-provider,powerpath.plugin.esx,powerpath.lib.esx,powerpath.cim.esx,net-be2net,scsi-lpfc820

######################################
# Verify Package Contents
#####################################

Get-EsxImageProfile VIG | select-object -ExpandProperty VibList | Out-File $VibListOutput

######################################
# Creat Custom ISO
#####################################

Export-EsxImageProfile ESXi5.0Custom -FilePath $CustomESXiISO -ExportToIso

######################################
# SCP ISO to vmaor14p501mhp
# for Linux PXE
# - Important: Login to Linux PXE
# with Username/Password in WinSCP GUI and
# Cache the Password
#####################################

# SCP Process VARs
$ScriptDir = "d:\scripts\"
$CustomBuildISOs = "d:\esxi_custom\CustomBuildISOs\"
$Resources = "C:\Program Files (x86)\WinSCP"
$LinuxPXEHost = "user@linux.host"

# Create SCP Script File
&{
 trap{Continue}
      
[array]$Script = "Open $LinuxPXEHost"
$Script += "option batch on"
$Script += "option confirm off"
$Script += "lcd $CustomBuildISOs"
$Script += "cd /root"
$Script += "put $CustomESXiISO"      
$Script += "close"
$Script += "exit"
     
# Dump script to file
$Script | out-file -filePath "$ScriptDir\scp2pxe.txt" -encoding "ASCII"
  
# SCP CustomBuildISO
$Process = [diagnostics.process]::Start("$Resources\winscp.com","$($Record.Session) /console /script=$ScriptDir\scp2pxe.txt")
$Process.WaitForExit()
}

##############################
# Email log file
##############################
   
#send-mailmessage -SmtpServer "SMTPServerName" `
#-from "CustomESXi Build ISO <DoNotReply@noreply.gov>" `
#-to "Somebody <somebody@somewhere.gov>" `
#-subject "CustomESXi Build ISO" `
#-body "CustomESXi5.0 Build ISO VibList attached. Custom ISO SCP'd to LinuxPXE Host."
#-Attachment $VibListOutput

##############################






Tuesday, July 19, 2011

Canceling Hung VMware Tools Install

For ESXi hosts :

/usr/bin/vim-cmd vmsvc/getallvms

That will return a list of all vms running on the host in the format:

VMID   VM Name     Datastore            Folder/VMX     VM "type"               
2256     VM             [datastore name]   VM/VM.vmx   winNetStandard64Guest  vmx-04


Note the Vmid of the VM with the stuck install and then use the second command as indicated:

/usr/bin/vim-cmd vmsvc/tools.cancelinstall VMID (in this example 2256)


For ESXfatclassy hosts:

/usr/bin/vmware-cmd /vmfs/volumes/whatever-datastore-name/vm-folder/vmx-file.vmx getid

/usr/bin/vmware-vim-cmd vmsvc/tools.cancelinstall vmidnumber

Tuesday, July 12, 2011

Enable Hot Add Mem & vCPU

<#
# Search for "Hot Add" Capable VMs and Enable
# Requirements: Powershell Version 2.0, PowerCLI 4.01 (not 4.0)
# Version 1.0 - 7/12/2011
#>

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

###############################
# Connect to specified vCenter
###############################

Connect-VIServer -server $vCenterName -Protocol https -User "Username" -Password "Password"

##########################################
# Get collection of VMs & Create Log File
##########################################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\HotAddCapableVMsList_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"

#################################################################
# Enable / Disable Hot Add Functions
#################################################################

Function Enable-vCpuAndMemHotAdd($vm){
  $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $extra1 = New-Object VMware.Vim.optionvalue
  $extra1.Key="mem.hotadd"
  $extra1.Value="true"
  $vmConfigSpec.extraconfig += $extra1
  $extra2 = New-Object VMware.Vim.optionvalue
  $extra2.Key="vcpu.hotadd"
  $extra2.Value="true"
  $vmConfigSpec.extraconfig += $extra2
  $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Enable-MemHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Disable-MemHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Enable-vCpuHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Disable-vCpuHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

#################################################################
#
#################################################################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vHW = $vm.ExtensionData.Config.Version

##############################
# See if VM is powered on
##############################

    if ($VMPower -notmatch "PoweredON") {
        Write-Host $VMName `t "Powered OFF" -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tPowered OFF")
    }    

##############################
# See if VM is vHW 7
##############################

    elseif ($vHW -notmatch "vmx-07") {
        Write-Host $VMName `t "Not vHW 7" -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tNot vHW 7`t`t" + $vHW)
    }
         
###############################
# See if VM has a Windows 7
###############################

    elseif ($OS -like "*Windows 7*"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }
     
###############################
# See if VM has a Windows 2008
###############################

    elseif ($OS -like "*Windows Server 2008*"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }
   
###################################
# See if VM has a Windows 2003 Ent
###################################

    elseif ($OS -like "*Windows Server 2003,Enterprise Edition*"){
        Enable-MemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }

###################################
# See if VM has a SLES 11
###################################

    elseif ($OS -like "Suse Linux Enterprise 11*"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }

###################################
# See if VM has a SLES 10
###################################

    elseif ($OS -like "Suse Linux Enterprise 10*"){
        Enable-MemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }
   
###################################
# See if VM has a RHEL 5 (64-bit)
###################################

    elseif ($OS -like "Red Hat Enterprise Linux 5 (64-bit)"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }

###################################
# Everything Else
###################################

    else {
        Write-Host $VMName `t $OS -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`t" + $OS + "`t`t" + $vHW)
    }

}

##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################
# Email log file
##############################
   
#send-mailmessage -SmtpServer "SMTPServerName" `
#-from "VM-Tools-Upgrade <DoNotReply@noreply.gov>" `
#-to "Somebody <somebody@somewhere.gov>" `
#-subject "VMware Tools Upgrade" `
#-body "VMware Tools upgrade log attached." `
#-Attachment $logFile

##############################

Tuesday, July 5, 2011

FindRDMS.ps1

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

##############################
# Connect to specified vCenter
##############################

Connect-VIServer -server $vCenterName -Protocol https -User "Username" -Password "Password"

$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\VMware-RDMS-_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"
$vms = get-cluster -name $clusterName | Get-VM  | Get-View
foreach($vm in $vms){
  foreach($dev in $vm.Config.Hardware.Device){
    if(($dev.gettype()).Name -eq "VirtualDisk"){
       if(($dev.Backing.CompatibilityMode -eq "physicalMode") -or
          ($dev.Backing.CompatibilityMode -eq "virtualMode")){
          Add-Content -Path $logFile -Value ("________________________________________")
          Add-Content -Path $logFile -Value ($vm.Name)
          Add-Content -Path $logFile -Value ((Get-View $vm.Runtime.Host).Name)
          Add-Content -Path $logFile -Value ($dev.Backing.DeviceName)
          Add-Content -Path $logFile -Value ($dev.Backing.FileName)
          Add-Content -Path $logFile -Value ($dev.Backing.CompatibilityMode)
          Add-Content -Path $logFile -Value ($dev.CapacityInKB)
          Add-Content -Path $logFile -Value ("________________________________________")
       }
     }
  }
}

##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################
# Email log file
##############################
   
#send-mailmessage -SmtpServer "SMTPServerName" `
#-from "VM-Tools-Upgrade <DoNotReply@noreply.gov>" `
#-to "Somebody <somebody@somewhere.gov>" `
#-subject "VMware Tools Upgrade" `
#-body "VMware Tools upgrade log attached." `
#-Attachment $logFile

##############################

windowsServer-Info.ps1

#get list of hosts
$hosts=get-content "h:\scripts\hosts.txt"
$P2VCandidates="h:\scripts\ScriptOutput\P2VCandidatesOutput_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).csv"
$tableInfo=@("DeviceId", "VolumeName", @{Label="FreeSpace(GB)"; Alignment="right"; Expression={"{0:N2}" -f ($_.FreeSpace/1GB)}}, @{Label="Size(GB)"; Alignment="right"; Expression={"{0:N2}" -f ($_.size/1GB)}})
#$hosts 
foreach($server in $hosts)
{

# WMI Commands
$os = Get-WmiObject Win32_OperatingSystem –computerName $server -ErrorAction SilentlyContinue
$machine = get-wmiobject win32_computersystem –computerName $server -ErrorAction SilentlyContinue
$bios = get-wmiobject Win32_bios -ComputerName $server -ErrorAction SilentlyContinue

# add a filter to only return the first CPU object - this is kinda bastard but generally CPUs will all be the same :)
$cpu = Get-WmiObject Win32_Processor –computerName $server -ErrorAction SilentlyContinue | select-object -first 1

#compute RAM in GB
$memory = get-wmiobject Win32_ComputerSystem –computerName $server
$memorygb = [Math]::Round($memory.TotalPhysicalMemory/1GB)



# IP Info
$addr = 0
$IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server
foreach ($IPConfig in $IPConfigSet) {
   if ($Ipconfig.IPaddress) {
      foreach ($addr in $Ipconfig.Ipaddress) {
      Out-String
   }
  }
}
#end IP

#Disk Info
    $totalsize = 0
    $totalfree = 0
    #debug - what are we set to?
    #$totalsize
    #$totalfree
   
    # Get fixed drive info
    $disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";

    foreach($disk in $disks)
    {
        $deviceID = $disk.DeviceID;
        [float]$size = $disk.Size;
        [float]$freespace = $disk.FreeSpace;
       
        #debug - what server?
        #$server
        #debug - what size did we find?
        #write-host current disk ($size/1GB)
        #debug - what is the current total?
        #write-host total disk ($totalsize/1GB)
        $totalsize += $disk.size
        $totalfree += $disk.FreeSpace
    }
#end disk


# Formatting the output
$output = New-Object PSObject
$output | Add-Member NoteProperty Name $server
$output | Add-Member NoteProperty NTDomain $machine.Domain
$output | Add-Member NoteProperty Manufacturer $machine.Manufacturer
$output | Add-Member NoteProperty Model $machine.Model
$output | Add-Member NoteProperty Serial $bios.SerialNumber
$output | Add-Member NoteProperty Type $machine.SystemType
#$output | Add-Member NoteProperty 64bit $cpu
$output | Add-Member NoteProperty TotalRAM $memorygb
$output | Add-Member NoteProperty NumberOfProcessors $machine.NumberOfProcessors
$output | Add-Member NoteProperty CPUName $cpu.Name
$output | Add-Member NoteProperty CPUSpeed $cpu.CurrentClockSpeed
$output | Add-Member NoteProperty IPInfo $addr
$output | Add-Member NoteProperty TotalDisk ($totalsize/1GB)
$output | Add-Member NoteProperty FreeDisk ($totalfree/1GB)
$output | Add-Member NoteProperty OSName $os.caption
$output | Add-Member NoteProperty ServicePack $os.CSDVersion

Write-Output $output | Export-Csv $P2VCandidates -noType -Append

}

DismountVMwareTools.ps1

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

##############################
# Connect to specified vCenter
##############################

Connect-VIServer -server $vCenterName -Protocol https -User "username" -Password "Password"

##############################
# Get collection of VMs, create log file
##############################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\Dismount-VMware-Tools_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"

##############################
# Get variables for each VM
##############################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vmID = get-vm $VM | foreach {get-view $_.Id}
    $vmToolsStatus = $vmID.Guest.ToolsStatus
   
##############################
# See if VM is powered on
##############################

if ($VMPower -notmatch "PoweredON") {
    Write-Host $VMName `t "Powered OFF" -ForegroundColor Red
    Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tPowered OFF")
}
       

##############################
# Update tools on VMs that meet prerequisites
##############################

else {
    Dismount-Tools $VM
    Write-Host $VMName `t $vmToolStatus `t "Dismounting Tools" -ForegroundColor Green
    Add-Content -Path $logFile -Value ($VMName + "`t`tDismounting Tools`t`t" + $vmToolsStatus)
}

}

##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################

OldLinuxVmwareTools.ps1

# Looks for Non-Windows VMs with Old Tools
# Requirements: Powershell Version 2.0, PowerCLI 4.01 (not 4.0)
# Version 1.0 - 6/28/2011



######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

##############################
# Connect to specified vCenter
##############################

Connect-VIServer -server $vCenterName -Protocol https -User "Username" -Password "Password"
##############################
# Get collection of VMs, create log file
##############################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "d:\scripts\ScriptOutput\LinuxVMs-oldTools_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"
$linuxVMs = "d:\scripts\ScriptOutput\LinuxVMs-oldTools_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).csv"

##############################
# Get variables for each VM
##############################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vmID = get-vm $VM | foreach {get-view $_.Id}
    $vmToolsStatus = $vmID.Guest.ToolsStatus

If ($OS -notlike "*Windows*") {
    # Formatting the output
    $output = New-Object PSObject
    $output | Add-Member -MemberType NoteProperty -name Name -value $VMName
    $output | Add-Member -MemberType NoteProperty -name NoToolsStatus -value $vmToolsStatus
    $output | Add-Member -MemberType NoteProperty -name OperatingSystem -value $OS
    $output | Add-Member -MemberType NoteProperty -name Cluster -value $clusterName
    Write-Output $output | Export-Csv $LinuxVMs -noType -Append

    Write-Host $VMName `t $vmToolsStatus `t $OS -ForegroundColor Green
    Add-Content -Path $logFile -Value ($VMName + "`t`t`t`t" + $vmToolsStatus + "`t`t`t`t" + $OS)
}
else {
    Write-Host $VMName `t $vmToolsStatus `t $OS -ForegroundColor Red
    Add-Content -Path $logFile -Value ($VMName + "`t`t`t`t" + $vmToolsStatus + "`t`t`t`t" + $OS)
}
}



##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force


##############################
# Email log file
##############################
   
#send-mailmessage -SmtpServer "SMTPServerName" `
#-from "VM-Tools-Upgrade <DoNotReply@noreply.gov>" `
#-to "Somebody <somebody@somewhere.gov>" `
#-subject "VMware Tools Upgrade" `
#-body "VMware Tools upgrade log attached." `
#-Attachment $logFile

##############################

Thursday, June 30, 2011

UpdateVMwareTools.ps1

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

##############################
# Connect to specified vCenter
##############################

Connect-VIServer -server $vCenterName -Protocol https -User "UserName" -Password "Password"
##############################
# Get collection of VMs, create log file
##############################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "C:\Powershell\temp\VMware-Tools-Update_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"

##############################
# Get variables for each VM
##############################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vmID = get-vm $VM | foreach {get-view $_.Id}
    $vmToolsStatus = $vmID.Guest.ToolsStatus

##############################
# See if VM is powered on
##############################

    if ($VMPower -notmatch "PoweredON") {
        Write-Host $VMName `t "Powered OFF" -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tPowered OFF")
        }

##############################
# See if VM has a Windows OS
##############################

        elseif ($OS -notlike "*Windows*"){
        Write-Host $VMName `t $OS -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`t" + $OS)
        }

##############################
# See if VM's tools need updating
##############################

            elseif ($vmToolsStatus -notmatch "toolsOld") {
            Write-Host $VMName `t $vmToolsStatus -ForegroundColor Red
            Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`t" + $vmToolsStatus)
            }
           
##############################
# Update tools on VMs that meet prerequisites
##############################

                else {
                Update-Tools $VM -NoReboot
                Write-Host $VMName `t $vmToolStatus `t "Upgrading Tools/No Reboot" -ForegroundColor Green
                Add-Content -Path $logFile -Value ($VMName + "`t`tInitialized`t`t" + $vmToolsStatus)
                }
    }

##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################