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

Friday, October 26, 2018

Get a list of VMs set to upgrade tools at power cycle


I needed to know what was set to upgrade at power cycle to verify a change request so with a little googling I got a good foundation and modified it to this.


Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster "Your_Cluster").id -Filter @{'Config.Tools.ToolsUpgradePolicy' = 'UpgradeAtPowerCycle' } | Select-Object name, @{N='ToolsUpgradePolicy';E={$_.Config.Tools.ToolsUpgradePolicy } }

Sunday, July 3, 2016

Document all VMs and their associated resources

So this one was out of necessity.  When I started my new job I quickly realized that our AV solution was used to lock out workstation directories like AppData and c:\temp.  These security settings meant I couldn’t run RVTools to document my virtual environment.  I have always pulled out data from VMware so I can monitor growth patterns and now I couldn’t use my process so I had to find another way.  That’s when I came up with the script below.  It’s not all the information RVTools would provide but it’s enough to understand growth and when to add hosts to the cluster.
 Start of script
 Connect-VIServer 'YourServer' -user YourDomain\YourUser -Password YourPassword
 Get-VMHost | Select-Object Name, State, ConnectionState, PowerState, VMSwapfileDatastoreId, VMSwapfilePolicy, ParentId, IsStandalone, Manufacturer, Model, NumCpu, CpuTotalMhz, CpuUsageMhz, LicenseKey, MemoryTotalMB, MemoryTotalGB, MemoryUsageMB, MemoryUsageGB, ProcessorType, HyperthreadingActive, TimeZone, Version, Build, Parent, VMSwapfileDatastore, StorageInfo, NetworkInfo, DiagnosticPartition, FirewallDefaultPolicy, ApiVersion, MaxEVCMode, CustomFields, ExtensionData, Id, Uid, Client, DatastoreIdList | Sort-Object Name | Export-Csv 'C:\temp\vCenter.csv' -NoClobber –NoTypeInformation
 Disconnect-VIserver * -confirm:$false
 End of script
 Also if you want to email the exported file you can use the send-MailMessage command.  That can be found here: http://mytechnicalsolution.blogspot.com/search/label/send-MailMessage

Saturday, July 2, 2016

Force all VMs to check and update VMware tools at reboot

When I started my new job I noticed that most of the VMs were up to six versions behind in regards to VMware Tools.  I was having NIC stability issues and weird random lockups so I wanted to take a big swing at this issue.  I decided to google how to turn set the “ToolsUpgradePolicy” to "UpgradeAtPowerCycle".  Below is what I found and worked for me as is. For the record the VMware Tools upgrades did stabilize my VMs.

Start of script

connect-viserver -server "your server" -user “domain\user” -password “your password”

#Turn on auto update at next boot

Foreach ($v in (get-vm)) {
$vm = $v | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ReconfigVM($vmConfigSpec)
}

Disconnect-VIserver * -confirm:$false

End of script

Also though it was good to figure out how to turn this off if all hell breaks loose. 
Here it is:

Start of script

connect-viserver -server "your server" -user “domain\user” -password “your password”

#turn off auto update at next boot

Foreach ($v in (get-vm)) {
$vm = $v | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "manual"
$vm.ReconfigVM($vmConfigSpec)
}

Disconnect-VIserver * -confirm:$false

End of script

Documenting HBA Information with WWNs, PCI Bus, and Card Model

I decided to figure out how to document the HBAs and WWNs for all of my fiber channel connections on all of my standalone ESXi host.  This was a quick solution based off of a previous script I found and modified.  I used that same methodology to come up with this.

Start of script

Import-Csv C:\temp\ESXi_Server_Names.csv | foreach { 
  
Connect-viServer -server $_.Server -User root -Password “your password”

Get-VMHostHba | Select-Object VMHost,Status,Device,PCI,Model,NodeWorldWideName,PortWorldWideName | Format-Table * | Out-File C:\Temp\ESXi_HBA.txt -Width 300 -Append
               
                Disconnect-VIserver * -confirm:$false }


End of script

vSwitch Documentation with CDP Information

I was asked today to document all the vSwitches in all of my standalone ESXi host.  Initially I was going to log into each and document everything in excel but I knew there was a more efficient way.  So I started to google and got dozens of sites back and dug through them all and finally found one that worked for me.  I was able to run through one host at a time, but I wanted it to be better.  So I added an import and for each with some squiggly brackets then piped that out to a text file.  See below for full PowerCLI script with my modifications.  Original script before my changes can be found here: http://kunaludapi.blogspot.com

Start of script

 Add-PSSnapin VMware.VimAutomation.core 
 Add-PSSnapin VMware.VimAutomation.Vds 
  
Import-Csv C:\temp\ESXi_Server_Names.csv | foreach { 
  
 Connect-viServer -server $_.Server -User root -Password "your password"
   
 $Collection = @() 
  
 $Esxihosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} 
 foreach ($Esxihost in $Esxihosts) { 
   $Esxcli = Get-EsxCli -VMHost $Esxihost 
   $Esxihostview = Get-VMHost $EsxiHost | get-view 
   $NetworkSystem = $Esxihostview.Configmanager.Networksystem 
   $Networkview = Get-View $NetworkSystem 
      
   $DvSwitchInfo = Get-VDSwitch -VMHost $Esxihost 
   if ($DvSwitchInfo -ne $null) { 
     $DvSwitchHost = $DvSwitchInfo.ExtensionData.Config.Host 
     $DvSwitchHostView = Get-View $DvSwitchHost.config.host 
     $VMhostnic = $DvSwitchHostView.config.network.pnic 
     $DVNic = $DvSwitchHost.config.backing.PnicSpec.PnicDevice 
   } 
    
   $VMnics = $Esxihost | get-vmhostnetworkadapter -Physical 
   Foreach ($VMnic in $VMnics){ 
       $realInfo = $Networkview.QueryNetworkHint($VMnic) 
       $pNics = $esxcli.network.nic.list() | where-object {$vmnic.name -eq $_.name} | Select-Object Description, Link          
       $Description = $esxcli.network.nic.list() 
       $CDPextended = $realInfo.connectedswitchport 
         if ($vmnic.Name -eq $DVNic) { 
            
           $vSwitch = $DVswitchInfo | where-object {$vmnic.Name -eq $DVNic} | select-object -ExpandProperty Name 
         } 
         else { 
           $vSwitchname = $Esxihost | Get-VirtualSwitch | Where-object {$_.nic -eq $VMnic.DeviceName} 
           $vSwitch = $vSwitchname.name 
         } 
   $CDPdetails = New-Object PSObject 
   $CDPdetails | Add-Member -Name EsxName -Value $esxihost.Name -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name vSwitch -Value $vSwitch -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Link -Value $pNics.Link -MemberType NoteProperty  
   $CDPdetails | Add-Member -Name PortNo -Value $CDPextended.PortId -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Device-ID -Value $CDPextended.devID -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Switch-IP -Value $CDPextended.Address -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name MacAddress -Value $vmnic.Mac -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name SpeedMB -Value $vmnic.ExtensionData.LinkSpeed.SpeedMB -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Duplex -Value $vmnic.ExtensionData.LinkSpeed.Duplex -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Pnic-Vendor -Value $pNics.Description -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Pnic-drivers -Value $vmnic.ExtensionData.Driver -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name PCI-Slot -Value $vmnic.ExtensionData.Pci -MemberType NoteProperty 
   $collection += $CDPdetails 
   } 
 } 
  
 $Collection | Sort-Object esxname, vmnic | ft * | Out-File C:\Temp\ESXi_Net.txt -Width 300 -append
  
 Disconnect-VIserver * -confirm:$false }


End of script