├── Cluster_hosts_vCPU_pCPU_report.ps1 ├── CreateLocalUserOnESXihosts.ps1 ├── configure-esxi-vmks.ps1 ├── create-vms.ps1 ├── get-cpuinfo.ps1 ├── get-esxi-vib-signatures.ps1 ├── get-vm-advanced_params.ps1 ├── get-vm-creation-date.ps1 ├── get-vm-guest-os-details.ps1 ├── remove-floppy-drive-from-windows-vms.ps1 ├── report-vmtools.ps1 ├── set-cluster-RDM-IsPerenniallyReserved.ps1 ├── set-esxi-scratch_location.ps1 ├── set-vm-advanced_params.ps1 ├── upgrade-vmtools-vmhw.ps1 └── vcenter-sessions.ps1 /Cluster_hosts_vCPU_pCPU_report.ps1: -------------------------------------------------------------------------------- 1 | # The script will calculate the ESXi host CPU core to VM vCPU oversubscription and create a HTML report 2 | #———————————————— 3 | # Start of script parameters section 4 | # 5 | # vCenter Server configuration 6 | $vcenter = “vc01.home.uw.cz“ 7 | $vcenteruser = “readonly“ 8 | $vcenterpw = “readonly“ 9 | $loginsight = "192.168.4.51" 10 | # 11 | # End of script parameter section 12 | #—————————————— # 13 | 14 | $o = Add-PSSnapin VMware.VimAutomation.Core 15 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 16 | # 17 | # Connect to vCenter Server 18 | $vc = connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw 19 | # 20 | 21 | Clear-Host 22 | 23 | # Send Message to LogInsight 24 | function Send-LogInsightMessage ([string]$ip, [string]$message) 25 | { 26 | $uri = "http://" + $ip + ":9000/api/v1/messages/ingest/1" 27 | $content_type = "application/json" 28 | $body = '{"messages":[{"text":"'+ $message +' "}]}' 29 | $r = Invoke-RestMethod -Uri $uri -ContentType $content_type -Method Post -Body $body 30 | } 31 | 32 | foreach ($esx in (Get-VMHost | Sort-Object Name)) { 33 | $pCPUs = $esx.NumCpu 34 | $vCPUs = ($esx | get-vm | Measure-Object -Sum NumCPU).Sum 35 | $CPU_ratio = $vCPUs / $pCPUs 36 | $date = (Get-Date).ToUniversalTime() 37 | $cluster_name = get-cluster -VMHost $esx 38 | 39 | $message = "UTC date time: $date Cluster: $cluster_name ESX name: $esx.Name pCPUs: $pCPUs vCPUs: $vCPUs vCPU/pCPU ratio: $CPU_ratio" 40 | Write-Host $message -Foreground Green 41 | Try { 42 | Send-LogInsightMessage $loginsight $message 43 | } Catch { 44 | Write-Host "Cannot connect to LogInsight. Breaking the script." -ForegroundColor Red 45 | Break 46 | } 47 | } 48 | 49 | disconnect-viserver -Server $vc -Force -Confirm:$false 50 | -------------------------------------------------------------------------------- /CreateLocalUserOnESXihosts.ps1: -------------------------------------------------------------------------------- 1 | # The script will add local user to all ESXi hosts in vCenter 2 | #———————————————— 3 | # Start of script parameters section 4 | # 5 | $account_action = "c" # actions can be - c=create, r=remove 6 | $account_name = "secaudit" 7 | $account_password = "VMware1!..QASDF123" 8 | $account_description = "New temporary user for security audit" 9 | 10 | $role_name = "ReadOnly" 11 | 12 | $vcenter_hostname = "vc01.home.uw.cz" 13 | # 14 | # End of script par 15 | 16 | $o = Add-PSSnapin VMware.VimAutomation.Core 17 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 18 | 19 | Clear-Host 20 | 21 | # Ask if it will be create or remove action 22 | do { 23 | write-host "What action do you want to perform? Available options: c=create, r=remove, q=quit" -BackgroundColor Yellow -ForegroundColor black 24 | $choice = Read-Host -Prompt "Enter your choice: [c,r,q]" 25 | write-host "Your choice is $choice" 26 | if ($choice -eq "q") { 27 | write-host "Quit script" 28 | exit 29 | } 30 | } until ( ($choice -eq "c") -or ($choice -eq "r") ) 31 | 32 | $account_action = $choice; 33 | 34 | # Connect to vCenter 35 | Write-Host "Connecting to vCenter ..." 36 | $CRED_VC = Get-Credential -Message "Enter vCenter credentials ..." 37 | $CRED_ESX = Get-Credential -Message "Enter ESXi host credentials ..." 38 | 39 | $VIServer_VC = Connect-VIServer -Server $vcenter_hostname -Credential $CRED_VC -ErrorAction Stop 40 | 41 | # Perform action for each ESXi managed by vCenter 42 | foreach ($esx in (Get-VMHost | Sort-Object Name)) { 43 | # Connect to particular ESXi host 44 | $VIServer_ESX = Connect-VIServer -Server $esx.Name -Credential $CRED_ESX 45 | 46 | if ($account_action -eq "c") { 47 | # Create new local user 48 | Write-host "Creating new local user on ESX host:" $esx.Name; 49 | New-VMHostAccount -Server $VIServer_ESX -Id $account_name -Password $account_password -Description $account_description 50 | 51 | # Add permission 52 | $role = Get-VIRole -Server $VIServer_ESX -Name $role_name 53 | $x = New-VIPermission -Server $VIServer_ESX -Entity $esx.name -Principal $account_name -Role $role -Propagate:$true 54 | } 55 | 56 | if ($account_action -eq "r") { 57 | # Remove new local user 58 | Write-host "Removing local user from ESX host:" $esx.Name; 59 | $host_account = Get-VMHostAccount -Server $VIServer_ESX -Id $account_name 60 | Remove-VMHostAccount -Server $VIServer_ESX -HostAccount $host_account -Confirm:$false 61 | } 62 | 63 | # Disconnect from particular ESXi host 64 | Disconnect-VIserver -Server $VIServer_ESX -Force -Confirm:$false 65 | } 66 | 67 | 68 | Disconnect-VIserver -Server $VIServer_VC -Force -Confirm:$false -------------------------------------------------------------------------------- /configure-esxi-vmks.ps1: -------------------------------------------------------------------------------- 1 | # 2 | # Script inspiration 3 | # http://www.punchingclouds.com/2016/03/24/vmware-virtual-san-automated-deployments-powercli/ 4 | # 5 | # Script assumptions 6 | # A1: ESXi host has single vmkernel interface (vmk0) for management. 7 | # vmk0 is not configured. 8 | # vmk0 real configuration will be validated against expected configuration in the future version 9 | # A2: 10 | 11 | 12 | Clear-Host 13 | 14 | $o = Add-PSSnapin VMware.VimAutomation.Core 15 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 16 | 17 | # Connect to vCenter 18 | Write-Host "Connecting to vCenter ..." 19 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 20 | Write-Host "Enter vCenter credentials ..." 21 | $CRED = Get-Credential 22 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 23 | 24 | 25 | # CONFIGURATION - JSON created and validated at https://jsonformatter.curiousconcept.com/ 26 | $conf_json = ' 27 | {"ESXs": 28 | [ 29 | { 30 | "hostname": "esx11.home.uw.cz", 31 | "configure": 1, 32 | "configure_vmks": 0, 33 | "vmks": [ 34 | { 35 | "name": "vmk0", 36 | "descr": "MGMT", 37 | "portgroup": "MGMT", 38 | "ip": "192.168.4.111", 39 | "subnet": "255.255.255.0", 40 | "management": 1, 41 | "vmotion": 0, 42 | "vsan": 0 43 | }, 44 | { 45 | "name": "vmk1", 46 | "descr": "VMOTION", 47 | "portgroup": "VMOTION", 48 | "ip": "192.168.22.111", 49 | "subnet": "255.255.255.0", 50 | "management": 0, 51 | "vmotion": 1, 52 | "vsan": 0 53 | }, 54 | { 55 | "name": "vmk2", 56 | "descr": "NFS", 57 | "portgroup": "NFS", 58 | "ip": "192.168.25.111", 59 | "subnet": "255.255.255.0", 60 | "management": 0, 61 | "vmotion": 0, 62 | "vsan": 0 63 | }, 64 | { 65 | "name": "vmk3", 66 | "descr": "ISCSI", 67 | "portgroup": "ISCSI", 68 | "ip": "192.168.24.111", 69 | "subnet": "255.255.255.0", 70 | "management": 0, 71 | "vmotion": 0, 72 | "vsan": 0 73 | }, 74 | { 75 | "name": "vmk4", 76 | "descr": "VSAN", 77 | "portgroup": "VSAN", 78 | "ip": "192.168.26.111", 79 | "subnet": "255.255.255.0", 80 | "management": 0, 81 | "vmotion": 0, 82 | "vsan": 1 83 | } 84 | ] 85 | }, 86 | { 87 | "hostname": "esx12.home.uw.cz", 88 | "configure" : 1, 89 | "configure_vmks": 0, 90 | "vmks": [ 91 | { 92 | "name": "vmk0", 93 | "descr": "MGMT", 94 | "portgroup": "MGMT", 95 | "ip": "192.168.4.112", 96 | "subnet": "255.255.255.0", 97 | "management": 1, 98 | "vmotion": 0, 99 | "vsan": 0 100 | }, 101 | { 102 | "name": "vmk1", 103 | "descr": "VMOTION", 104 | "portgroup": "VMOTION", 105 | "ip": "192.168.22.112", 106 | "subnet": "255.255.255.0", 107 | "management": 0, 108 | "vmotion": 1, 109 | "vsan": 0 110 | }, 111 | { 112 | "name": "vmk2", 113 | "descr": "NFS", 114 | "portgroup": "NFS", 115 | "ip": "192.168.25.112", 116 | "subnet": "255.255.255.0", 117 | "management": 0, 118 | "vmotion": 0, 119 | "vsan": 0 120 | }, 121 | { 122 | "name": "vmk3", 123 | "descr": "ISCSI", 124 | "portgroup": "ISCSI", 125 | "ip": "192.168.24.112", 126 | "subnet": "255.255.255.0", 127 | "management": 0, 128 | "vmotion": 0, 129 | "vsan": 0 130 | }, 131 | { 132 | "name": "vmk4", 133 | "descr": "VSAN", 134 | "portgroup": "VSAN", 135 | "ip": "192.168.26.112", 136 | "subnet": "255.255.255.0", 137 | "management": 0, 138 | "vmotion": 0, 139 | "vsan": 1 140 | } 141 | ] 142 | }, 143 | { 144 | "hostname": "esx13.home.uw.cz", 145 | "configure" : 1, 146 | "configure_vmks": 0, 147 | "vmks": [ 148 | { 149 | "name": "vmk0", 150 | "descr": "MGMT", 151 | "portgroup": "MGMT", 152 | "ip": "192.168.4.113", 153 | "subnet": "255.255.255.0", 154 | "management": 1, 155 | "vmotion": 0, 156 | "vsan": 0 157 | }, 158 | { 159 | "name": "vmk1", 160 | "descr": "VMOTION", 161 | "portgroup": "VMOTION", 162 | "ip": "192.168.22.113", 163 | "subnet": "255.255.255.0", 164 | "management": 0, 165 | "vmotion": 1, 166 | "vsan": 0 167 | }, 168 | { 169 | "name": "vmk2", 170 | "descr": "NFS", 171 | "portgroup": "NFS", 172 | "ip": "192.168.25.113", 173 | "subnet": "255.255.255.0", 174 | "management": 0, 175 | "vmotion": 0, 176 | "vsan": 0 177 | }, 178 | { 179 | "name": "vmk3", 180 | "descr": "ISCSI", 181 | "portgroup": "ISCSI", 182 | "ip": "192.168.24.113", 183 | "subnet": "255.255.255.0", 184 | "management": 0, 185 | "vmotion": 0, 186 | "vsan": 0 187 | }, 188 | { 189 | "name": "vmk4", 190 | "descr": "VSAN", 191 | "portgroup": "VSAN", 192 | "ip": "192.168.26.113", 193 | "subnet": "255.255.255.0", 194 | "management": 0, 195 | "vmotion": 0, 196 | "vsan": 1 197 | } 198 | ] 199 | }, 200 | { 201 | "hostname": "esx14.home.uw.cz", 202 | "configure" : 1, 203 | "configure_vmks": 1, 204 | "vmks": [ 205 | { 206 | "name": "vmk0", 207 | "descr": "MGMT", 208 | "portgroup": "MGMT", 209 | "ip": "192.168.4.114", 210 | "subnet": "255.255.255.0", 211 | "management": 1, 212 | "vmotion": 0, 213 | "vsan": 0 214 | }, 215 | { 216 | "name": "vmk1", 217 | "descr": "VMOTION", 218 | "portgroup": "VMOTION", 219 | "ip": "192.168.22.114", 220 | "subnet": "255.255.255.0", 221 | "management": 0, 222 | "vmotion": 1, 223 | "vsan": 0 224 | }, 225 | { 226 | "name": "vmk2", 227 | "descr": "NFS", 228 | "portgroup": "NFS", 229 | "ip": "192.168.25.114", 230 | "subnet": "255.255.255.0", 231 | "management": 0, 232 | "vmotion": 0, 233 | "vsan": 0 234 | }, 235 | { 236 | "name": "vmk3", 237 | "descr": "ISCSI", 238 | "portgroup": "ISCSI", 239 | "ip": "192.168.24.114", 240 | "subnet": "255.255.255.0", 241 | "management": 0, 242 | "vmotion": 0, 243 | "vsan": 0 244 | }, 245 | { 246 | "name": "vmk4", 247 | "descr": "VSAN", 248 | "portgroup": "VSAN", 249 | "ip": "192.168.26.114", 250 | "subnet": "255.255.255.0", 251 | "management": 0, 252 | "vmotion": 0, 253 | "vsan": 1 254 | } 255 | ] 256 | } 257 | 258 | ] 259 | } 260 | ' 261 | 262 | # Validate JSON configuration in PowerShell 263 | if (1) { # always do - this is just a block which can be collapsed in PowerShell ISE 264 | try { 265 | $conf = ConvertFrom-Json -InputObject $conf_json -ErrorAction Stop; 266 | $validJson = $true; 267 | } catch { 268 | $validJson = $false; 269 | } 270 | 271 | if ($validJson) { 272 | Write-Host "Provided configuration has been correctly parsed to JSON"; 273 | } else { 274 | Write-Host "Provided configuration is not a valid JSON string"; 275 | exit 276 | } 277 | } 278 | 279 | # Get virtual switch 280 | $VDS = Get-VirtualSwitch -Name PAYLOAD 281 | 282 | # Configure ESXi hosts 283 | foreach ($esx in $conf.ESXs) { 284 | $hostname = $esx.hostname 285 | $configure = $esx.configure 286 | Write-Host -BackgroundColor Gray -ForegroundColor Black "==========================================" 287 | Write-Host -BackgroundColor Gray -ForegroundColor Black "ESX: $hostname" 288 | if (-Not $configure) { 289 | Write-Host -ForegroundColor Yellow "Configuration of $hostname is not required. Skip to next host." 290 | Continue 291 | } 292 | Write-Host -ForegroundColor Green "ESXi configuration ..." 293 | 294 | # start of vmKernel configuration section 295 | $configure_vmks = $esx.configure_vmks; 296 | if (-Not $configure_vmks) { 297 | Write-Host -ForegroundColor Yellow "Configuration of vmKernel interface is not required. Skip to next type of configuration." 298 | } else { 299 | Write-Host -ForegroundColor Green " vmKernel interfaces configuration ..." 300 | foreach ($vmk in $esx.vmks) { 301 | $vmk_name = [String]($vmk.name).ToLower(); 302 | $vmk_ip = $vmk.ip; 303 | $vmk_subnet = $vmk.subnet; 304 | $vmk_portgroup = $vmk.portgroup 305 | $vmk_management = [Boolean]($vmk.management) 306 | $vmk_vmotion = [Boolean]($vmk.vmotion) 307 | $vmk_vsan = [Boolean]($vmk.vsan) 308 | $vmk_mtu = [Int]($vmk.mtu) 309 | if ($vmk_mtu -lt 1500) {$vmk_mtu = 1500} 310 | 311 | Write-host " vmk_name = $vmk_name" 312 | Write-host " vmk_ip = $vmk_ip" 313 | Write-host " vmk_subnet = $vmk_subnet" 314 | Write-host " vmk_portgroup = $vmk_portgroup" 315 | Write-host " vmk_management = $vmk_management" 316 | Write-host " vmk_vmotion = $vmk_vmotion" 317 | Write-host " vmk_vsan = $vmk_vsan" 318 | Write-host " vmk_mtu = $vmk_mtu" 319 | if ($vmk_name -eq "vmk0") { 320 | Write-host -ForegroundColor Yellow " We do not configure vmk0 interface. vmk0 validation will be implemented in the future." 321 | } else { 322 | # Creates a VMkernel port VMotion on vSwitch0 323 | Write-Host -ForegroundColor Green "$vmk_name configuration ..." 324 | New-VMHostNetworkAdapter -VMhost $hostname -PortGroup $vmk_portgroup -VirtualSwitch $VDS -IP $vmk_ip -SubnetMask $vmk_subnet -ManagementTrafficEnabled $vmk_management -VMotionEnabled $vmk_vmotion -VsanTrafficEnabled $vmk_vsan -mtu $vmk_mtu 325 | } 326 | Write-host " --------------------------" 327 | } 328 | } # end of vmKernel configuration section 329 | 330 | } # enf of ESXi hosts foreach loop 331 | 332 | Disconnect-VIserver -Server $VC -Force -Confirm:$false 333 | -------------------------------------------------------------------------------- /create-vms.ps1: -------------------------------------------------------------------------------- 1 | # PowerCLI script to create Virtual Machines 2 | # =========================================== 3 | # It can create new VM or deploy VM from template if template name is specified. 4 | # 5 | #———————————————— 6 | # Start of script parameters section 7 | # 8 | # vCenter Server configuration 9 | $vcenter = “vc01.home.uw.cz“ 10 | $vcenteruser = “readwrite“ 11 | $vcenterpw = “readwrite“ 12 | # 13 | # Specify number of VMs you want to create 14 | $vm_count = “5“ 15 | # 16 | # Specify vCenter Server datastore 17 | $Datastore = “TEST“ 18 | # 19 | # Specify vCenter Server Virtual Machine & Templates folder 20 | $Folder = “TEST” 21 | # 22 | # Specify the vSphere Cluster 23 | $Cluster = “TEST“ 24 | # 25 | # Specify the VM name to the left of the – sign 26 | $VM_prefix = “XTEST-“ 27 | # 28 | # Specify the VM provisioning type (sync/async) - true = async (parallel), false = sync (sequentional) 29 | $VM_create_async = $false 30 | # 31 | # Specify if VM should be created from template 32 | $VM_from_template="TEST_template" 33 | # 34 | # Specify if VM should be Power On 35 | $VM_power_on = $false 36 | # 37 | # Parameters below are used only for new VM. 38 | # VM created from template has these parameters included in the template. 39 | # 40 | # Specify number of VM CPUs 41 | $numcpu = “1“ 42 | # 43 | # Specify number of VM MB RAM 44 | $MBram = “512“ 45 | # 46 | # Specify VM disk size (in MB) 47 | $MBguestdisk = “1000“ 48 | # 49 | # Specify VM disk type, available options are Thin, Thick, EagerZeroedThick 50 | $Typeguestdisk =”Thick“ 51 | # 52 | # Specify VM guest OS 53 | $guestOS = “winNetStandardGuest“ 54 | # 55 | # End of script parameter section 56 | #—————————————— # 57 | 58 | clear-host 59 | 60 | $o = Add-PSSnapin VMware.VimAutomation.Core 61 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 62 | # 63 | # Connect to vCenter Server 64 | write-host “Connecting to vCenter Server $vcenter” -foreground green 65 | $vc = connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw 66 | # 67 | 68 | #$O_cluster=Get-Cluster $Cluster 69 | 70 | 1..$vm_count | foreach { 71 | $VM_postfix=”{0:D2}” -f $_ 72 | $VM_name= $VM_prefix + $VM_postfix 73 | #$O_ESXi=Get-Cluster $Cluster_name | Get-VMHost -state connected | Get-Random 74 | 75 | if ($VM_from_template -eq "") { 76 | write-host “Creation of VM $VM_name initiated” -foreground green 77 | New-VM -RunAsync:$VM_create_async -Name $VM_Name -ResourcePool $Cluster -numcpu $numcpu -MemoryMB $MBram -DiskMB $MBguestdisk -DiskStorageFormat $Typeguestdisk -Datastore $Datastore -GuestId $guestOS -Location $Folder 78 | } else { 79 | write-host “Deployment of VM $VM_name from template $VM_from_template initiated” -foreground green 80 | New-VM -RunAsync:$VM_create_async -Name $VM_Name -Template $VM_from_template -ResourcePool $Cluster -Datastore $Datastore -Location $Folder 81 | } 82 | 83 | if ($VM_power_on) { 84 | write-host “Power On of the VM $VM_name initiated" -foreground green 85 | Start-VM -VM $VM_name -confirm:$false -RunAsync 86 | } 87 | } -------------------------------------------------------------------------------- /get-cpuinfo.ps1: -------------------------------------------------------------------------------- 1 | # Get-CPUInfo.ps1 2 | # Code produced by Juan Manuel Rey (@jreypo) 3 | # 4 | 5 | $strComputer = "." 6 | $colItems = Get-WmiObject -class "Win32_Processor" -namespace "root/CIMV2" -computername $strComputer 7 | 8 | foreach ($objItem in $colItems) { 9 | Write-Host 10 | Write-Host "CPU ID: " -foregroundcolor yellow -NoNewLine 11 | Write-Host $objItem.DeviceID -foregroundcolor white 12 | Write-Host "CPU Model: " -foregroundcolor yellow -NoNewLine 13 | Write-Host $objItem.Name -foregroundcolor white 14 | Write-Host "CPU Cores: " -foregroundcolor yellow -NoNewLine 15 | Write-Host $objItem.NumberOfCores -foregroundcolor white 16 | Write-Host "CPU Max Speed: " -foregroundcolor yellow -NoNewLine 17 | Write-Host $objItem.MaxClockSpeed 18 | Write-Host "CPU Status: " -foregroundcolor yellow -NoNewLine 19 | Write-Host $objItem.Status 20 | Write-Host 21 | } -------------------------------------------------------------------------------- /get-esxi-vib-signatures.ps1: -------------------------------------------------------------------------------- 1 | Clear-Host 2 | 3 | # We need VMware PowerCLI snapin 4 | $o = Add-PSSnapin VMware.VimAutomation.Core 5 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 6 | 7 | # Connect to vCenter 8 | Write-Host "Connecting to vCenter ..." 9 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 10 | Write-Host "Enter vCenter credentials ..." 11 | $CRED = Get-Credential 12 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 13 | 14 | $ESXiKernelMonules = @() 15 | 16 | Foreach ($VMHost in Get-VMHost) { 17 | $ESXCli = Get-EsxCli -VMHost $VMHost 18 | $ESXCli.system.module.list() | 19 | Foreach { 20 | $ESXiKernelMonules += $ESXCli.system.module.get($_.Name) | Select @{N="VMHost";E={$VMHost}}, Module, License, Modulefile, Version, SignedStatus, SignatureDigest, SignatureFingerPrint 21 | } 22 | } 23 | 24 | $ESXiKernelMonules | Out-GridView -------------------------------------------------------------------------------- /get-vm-advanced_params.ps1: -------------------------------------------------------------------------------- 1 | Clear-Host 2 | 3 | $o = Add-PSSnapin VMware.VimAutomation.Core 4 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 5 | 6 | # Connect to vCenter 7 | Write-Host "Connecting to vCenter ..." 8 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 9 | Write-Host "Enter vCenter credentials ..." 10 | $CRED = Get-Credential 11 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 12 | 13 | # Array of virtual machine names 14 | #$vm_names = "W2K8R2-test1","W2K8R2-test2" 15 | $vm_names = "W2K8R2-test" 16 | 17 | foreach ($vm_name in $vm_names) { 18 | Write-Host "VM: [$vm_name]" 19 | 20 | try { 21 | $vm = get-vm -Name $vm_name -ErrorAction Stop 22 | #Get-AdvancedSetting -Entity $vm -Name svga.vgaOnly 23 | } catch { 24 | Write-Warning -Message "VM doesn't exist"; 25 | } 26 | 27 | } 28 | 29 | Disconnect-VIserver -Server $VC -Force -Confirm:$false 30 | -------------------------------------------------------------------------------- /get-vm-creation-date.ps1: -------------------------------------------------------------------------------- 1 | #Enter your vCenter Host below 2 | $vcenter = "vc01.home.uw.cz" 3 | #Enter the CSV file to be created 4 | $csvfile = "c:\tmp\VM_creation_date.CSV" 5 | ################################ 6 | 7 | #Load the VMware Powershell snapin if the script is being executed in PowerShell 8 | Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue' 9 | 10 | #Connect to the vCenter server defined above. Ignore certificate errors 11 | Write-Host "Connecting to vCenter" 12 | Connect-VIServer $vcenter -wa 0 13 | Write-Host "Connected" 14 | Write-Host "" 15 | 16 | #Check to see if the file exists, if it does then overwrite it. 17 | if (Test-Path $csvfile) { 18 | Write-Host "Overwriting $csvfile" 19 | del $csvfile 20 | } 21 | 22 | #Create the CSV title header 23 | Add-Content $csvfile "VM,Born on,Creator,Creation Type,Event Message" 24 | 25 | #Gather all VM's from vCenter 26 | $vms = Get-VM | sort Name 27 | 28 | foreach ($VM in $vms) { 29 | Write-Host "Gathering info for $VM" 30 | 31 | #Search for events where the VM was deployed from a template 32 | $vmevents = Get-VIEvent $VM -Start (Get-Date).AddHours(-1) -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Deploying*"} |Select CreatedTime, UserName, FullFormattedMessage 33 | if ($vmevents) 34 | { 35 | $type = "From Template" 36 | } 37 | 38 | #If no events were found, search for events where the VM was created from scratch 39 | if (!$vmevents) { 40 | $vmevents = Get-VIEvent $VM -Start (Get-Date).AddHours(-1) -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Created*"} |Select CreatedTime, UserName, FullFormattedMessage 41 | Write-Host "Searching by Created" 42 | $type = "From Scratch" 43 | } 44 | 45 | #If no events were found, search for events where the VM was cloned 46 | if (!$vmevents) { 47 | $vmevents = Get-VIEvent $VM -Start (Get-Date).AddHours(-1) -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Clone*"} |Select CreatedTime, UserName, FullFormattedMessage 48 | Write-Host "Searching by Cloned" 49 | $type = "Cloned" 50 | } 51 | 52 | #If no events were found, search for events where the VM was discovered 53 | if (!$vmevents) { 54 | $vmevents = Get-VIEvent $VM -Start (Get-Date).AddHours(-1) -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Discovered*"} |Select CreatedTime, UserName, FullFormattedMessage 55 | Write-Host "Searching by Discovered" 56 | $type = "Discovered" 57 | } 58 | 59 | #If no events were found, search for events where the VM was connected (typically from Backup Restores) 60 | if (!$vmevents) { 61 | $vmevents = Get-VIEvent $VM -Start (Get-Date).AddHours(-1) -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "* connected"} |Select CreatedTime, UserName, FullFormattedMessage 62 | Write-Host "Searching by Connected" 63 | $type = "Connected" 64 | } 65 | 66 | #I have no idea how this VM came to be. 67 | if (!$vmevents) { 68 | Write-Host "No clue how this VM got here!" 69 | $type = "Immaculate Conception" 70 | } 71 | 72 | #In some cases there may be more than one event found (typically from VM restores). This will include each event in the CSV for the user to interpret. 73 | foreach ($event in $vmevents) { 74 | 75 | #Prepare the entries 76 | $birthday = $event.CreatedTime.ToString("MM/dd/yy") 77 | $parent = $event.Username 78 | $message = $event.FullFormattedMessage 79 | 80 | #Add the entries to the CSV 81 | $write = "$VM, $birthday, $parent, $type, $message" 82 | Add-Content $csvfile $write 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /get-vm-guest-os-details.ps1: -------------------------------------------------------------------------------- 1 | #Load the VMware Powershell snapin if the script is being executed in PowerShell 2 | Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue' 3 | 4 | #Connect to the vCenter server defined above. Ignore certificate errors 5 | Write-Host "Connecting to vCenter" 6 | Connect-VIServer -wa 0 7 | Write-Host "Connected" 8 | Write-Host "" 9 | 10 | #Gather all VM's from vCenter 11 | $vms = Get-VM | sort Name 12 | 13 | foreach ($VM in $vms) { 14 | Write-Host "Gathering info for " $VM.name 15 | Write-Host " Configured Guest OS: " $VM.ExtensionData.Config.GuestFullName 16 | Write-Host " VMtool reported OS: "$VM.ExtensionData.Guest.GuestFullName 17 | #$VM | ForEach-Object {$_.ExtensionData} 18 | #$VM | ForEach-Object {$_.ExtensionData.Guest} 19 | #$VM | ForEach-Object {$_.ExtensionData.Runtime} 20 | 21 | Write-Host "=====================================" 22 | } 23 | 24 | -------------------------------------------------------------------------------- /remove-floppy-drive-from-windows-vms.ps1: -------------------------------------------------------------------------------- 1 | #Enter your vCenter Host below 2 | $vcenter = "vc01" 3 | ################################ 4 | 5 | #Load the VMware Powershell snapin if the script is being executed in PowerShell 6 | Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue' 7 | 8 | #Connect to the vCenter server defined above. Ignore certificate errors 9 | Write-Host "Connecting to vCenter" 10 | $VCSESSION=Connect-VIServer $vcenter -wa 0 -ErrorAction Stop 11 | Write-Host "Connected" 12 | Write-Host "" 13 | 14 | Write-Host -BackgroundColor Gray -ForegroundColor Black "The script goes through all Powered Off Virtual Machines and removes floppy drive for VMs having Guest ID configured as Windows OS." 15 | 16 | #Gather all Powered Off VM's from vCenter 17 | $vms=GET-VM | Where-Object {$_.PowerState -eq "PoweredOff" } 18 | ForEach ($vm in $vms) 19 | { 20 | $osvmtools = Get-VMguest -VM $vm | select OSFullName, GuestFamily 21 | $osconfig = $vm.ExtensionData.Config | select GuestFullName, GuestID 22 | Write-Host "VM name: " $vm.Name 23 | Write-Host "VM Config OS Full Name : " $osconfig.GuestFullName 24 | Write-Host "VM Config Guest ID : " $osconfig.GuestID 25 | Write-Host "VM Tools OS Full Name : " $osvmtools.OSFullname 26 | Write-Host "VM Tools Guest Family : " $osvmtools.GuestFamily 27 | if ($osconfig.GuestID.ToString() -match "windows") 28 | { 29 | $fd = Get-FloppyDrive -VM $vm 30 | if ($fd) { 31 | Write-Host -BackgroundColor Red -ForegroundColor Black "Removing floppy drive" 32 | $fd | Remove-FloppyDrive -Confirm:$false 33 | } else { 34 | Write-Host -BackgroundColor Green -ForegroundColor Black "No floppy drive" 35 | } 36 | } else { 37 | Write-Host -BackgroundColor Gray -ForegroundColor Black "No action as it is not Windows machine" 38 | } 39 | Write-Host "----" 40 | } 41 | 42 | Disconnect-VIserver -Server $VCSESSION -Force -Confirm:$false -------------------------------------------------------------------------------- /report-vmtools.ps1: -------------------------------------------------------------------------------- 1 | ###################################################################################################################################### 2 | # Author: David Pasek 3 | # E-mail: david.pasek@gmail.com 4 | # Twitter: david_pasek 5 | # Creation Date: 2016-11-25 6 | # 7 | # Use case: 8 | # Key use case of this script is to report VMtools from all VMs in vCenter 9 | # 10 | # Disclaimer: 11 | # Use it on your own risk. Author is not responsible for any impacts caused by this script. 12 | ###################################################################################################################################### 13 | # 14 | # CHANGE FOLLOWING VARIABLES BASED ON YOUR SPECIFIC REQUIREMENTS 15 | # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 16 | # 17 | # Report type - table, grid, file, csv-file 18 | $REPORT_TYPE = "grid" 19 | # Report file name without file extension. Extension is automatically added. File is created in current working directory. 20 | $REPORT_FILE_NAME = "report-vmtools" 21 | ###################################################################################################################################### 22 | 23 | 24 | Clear-Host 25 | 26 | # We need VMware PowerCLI snapin 27 | $o = Add-PSSnapin VMware.VimAutomation.Core 28 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 29 | 30 | # Connect to vCenter 31 | Write-Host "Connecting to vCenter ..." 32 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 33 | Write-Host "Enter vCenter credentials ..." 34 | $CRED = Get-Credential 35 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 36 | 37 | # Add new property (ToolsVersion) to VM 38 | New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.tools.ToolsVersion' -Force | Out-Null 39 | 40 | # Initalize report 41 | $Report = @() 42 | foreach ($vm in Get-VM) { 43 | # Numbers mapping is from https://packages.vmware.com/tools/versions 44 | Switch ($vm.ToolsVersion) { 45 | 7302 {$GuestToolsVersion = "7.4.6"} 46 | 7303 {$GuestToolsVersion = "7.4.7"} 47 | 7304 {$GuestToolsVersion = "7.4.8"} 48 | 8192 {$GuestToolsVersion = "8.0.0"} 49 | 8194 {$GuestToolsVersion = "8.0.2"} 50 | 8195 {$GuestToolsVersion = "8.0.3"} 51 | 8196 {$GuestToolsVersion = "8.0.4"} 52 | 8197 {$GuestToolsVersion = "8.0.5"} 53 | 8198 {$GuestToolsVersion = "8.0.6"} 54 | 8199 {$GuestToolsVersion = "8.0.7"} 55 | 8290 {$GuestToolsVersion = "8.3.2"} 56 | 8295 {$GuestToolsVersion = "8.3.7"} 57 | 8300 {$GuestToolsVersion = "8.3.12"} 58 | 8305 {$GuestToolsVersion = "8.3.17"} 59 | 8306 {$GuestToolsVersion = "8.3.18"} 60 | 8307 {$GuestToolsVersion = "8.3.19"} 61 | 8384 {$GuestToolsVersion = "8.6.0"} 62 | 8389 {$GuestToolsVersion = "8.6.5"} 63 | 8394 {$GuestToolsVersion = "8.6.10"} 64 | 8395 {$GuestToolsVersion = "8.6.11"} 65 | 8396 {$GuestToolsVersion = "8.6.12"} 66 | 8397 {$GuestToolsVersion = "8.6.13"} 67 | 8398 {$GuestToolsVersion = "8.6.14"} 68 | 8399 {$GuestToolsVersion = "8.6.15"} 69 | 8400 {$GuestToolsVersion = "8.6.16"} 70 | 8401 {$GuestToolsVersion = "8.6.17"} 71 | 9216 {$GuestToolsVersion = "9.0.0"} 72 | 9217 {$GuestToolsVersion = "9.0.1"} 73 | 9221 {$GuestToolsVersion = "9.0.5"} 74 | 9226 {$GuestToolsVersion = "9.0.10"} 75 | 9227 {$GuestToolsVersion = "9.0.11"} 76 | 9228 {$GuestToolsVersion = "9.0.12"} 77 | 9229 {$GuestToolsVersion = "9.0.13"} 78 | 9231 {$GuestToolsVersion = "9.0.15"} 79 | 9232 {$GuestToolsVersion = "9.0.16"} 80 | 9233 {$GuestToolsVersion = "9.0.17"} 81 | 9344 {$GuestToolsVersion = "9.4.0"} 82 | 9349 {$GuestToolsVersion = "9.4.5"} 83 | 9350 {$GuestToolsVersion = "9.4.6"} 84 | 9354 {$GuestToolsVersion = "9.4.10"} 85 | 9355 {$GuestToolsVersion = "9.4.11"} 86 | 9356 {$GuestToolsVersion = "9.4.12"} 87 | 9359 {$GuestToolsVersion = "9.4.15"} 88 | 9536 {$GuestToolsVersion = "9.10.0"} 89 | 9537 {$GuestToolsVersion = "9.10.1"} 90 | 9541 {$GuestToolsVersion = "9.10.5"} 91 | 10240 {$GuestToolsVersion = "10.0.0"} 92 | 10245 {$GuestToolsVersion = "10.0.5"} 93 | 10246 {$GuestToolsVersion = "10.0.6"} 94 | 10247 {$GuestToolsVersion = "10.0.8"} 95 | 10249 {$GuestToolsVersion = "10.0.9"} 96 | 10252 {$GuestToolsVersion = "10.0.12"} 97 | 10272 {$GuestToolsVersion = "10.1.0"} 98 | 0 {$GuestToolsVersion = "Not installed"} 99 | 2147483647 {$GuestToolsVersion = "3rd party - guest managed"} 100 | default {$GuestToolsVersion = "Unknown"} 101 | } 102 | 103 | $vminfo = New-Object -Type PSObject -Property @{ 104 | Name = $vm.Name 105 | VMhardwareVersion = $vm.Version 106 | ToolsVersion = $vm.ToolsVersion 107 | GuestToolsVersion = $GuestToolsVersion 108 | } 109 | 110 | $Report += $vminfo 111 | } 112 | 113 | # Show report 114 | Switch ($REPORT_TYPE) { 115 | "grid" { $Report | select Name,VMhardwareVersion,ToolsVersion,GuestToolsVersion | Out-GridView } 116 | "file" { $Report | select Name,VMhardwareVersion,ToolsVersion,GuestToolsVersion | Out-File -FilePath "$REPORT_FILE_NAME.txt" } 117 | "csv-file" { $Report | select Name,VMhardwareVersion,ToolsVersion,GuestToolsVersion | export-csv "$REPORT_FILE_NAME.csv" } 118 | default { $Report | select Name,VMhardwareVersion,ToolsVersion,GuestToolsVersion | Format-Table } 119 | } 120 | 121 | Disconnect-VIserver -Server $VC -Force -Confirm:$false 122 | -------------------------------------------------------------------------------- /set-cluster-RDM-IsPerenniallyReserved.ps1: -------------------------------------------------------------------------------- 1 | # This script will set the parameter Perennially Reservations to True on RDM Luns in a cluster 2 | #$vcenter = #"vCenter Name " 3 | #$dCenter = #"Datacenter Name" 4 | #$cluster = #"Cluster Name" 5 | 6 | $vcenter = "vc01.home.uw.cz" 7 | $dCenter = "LAB" 8 | $cluster = "Cluster01" 9 | 10 | #------------------------------------------------------------------------- 11 | 12 | # Do not modify bellow script 13 | #------------------------------------------------------------------------- 14 | 15 | #Add-PSSnapIn VMware* -ErrorAction SilentlyContinue 16 | 17 | $connected = Connect-VIServer -Server $vcenter | Out-Null 18 | 19 | $clusterInfo = Get-Datacenter -Name $dCenter | get-cluster $cluster 20 | $vmHosts = $clusterInfo | get-vmhost | select -ExpandProperty Name 21 | $RDMNAAs = $clusterInfo | Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select -ExpandProperty ScsiCanonicalName -Unique 22 | 23 | foreach ($vmhost in $vmHosts) { 24 | $myesxcli = Get-EsxCli -VMHost $vmhost 25 | 26 | foreach ($naa in $RDMNAAs) { 27 | $diskinfo = $myesxcli.storage.core.device.list("$naa") | Select -ExpandProperty IsPerenniallyReserved 28 | $vmhost + " " + $naa + " " + "IsPerenniallyReserved= " + $diskinfo 29 | if($diskinfo -eq "false") { 30 | write-host "Configuring Perennial Reservation for LUN $naa......." 31 | $myesxcli.storage.core.device.setconfig($false,$naa,$true) 32 | $diskinfo = $myesxcli.storage.core.device.list("$naa") | Select -ExpandProperty IsPerenniallyReserved 33 | $vmhost + " " + $naa + " " + "IsPerenniallyReserved= " + $diskinfo 34 | } 35 | write-host "----------------------------------------------------------------------------------------------" 36 | } 37 | } 38 | 39 | Disconnect-VIServer $vcenter -confirm:$false | Out-Null 40 | 41 | -------------------------------------------------------------------------------- /set-esxi-scratch_location.ps1: -------------------------------------------------------------------------------- 1 | ###################################################################################################################################### 2 | # Author: David Pasek 3 | # E-mail: david.pasek@gmail.com 4 | # Twitter: david_pasek 5 | # Creation Date: 2016-10-26 6 | # 7 | # Use case: 8 | # Key use case of this script is to create directories in pre-defined datastore and redirect all ESXi scratch partitions to this particular 9 | # shared datastore. It is done just for ESXi hosts in pre-defined clusters. 10 | # 11 | # Description: 12 | # This script does three following actions. 13 | # Action 1/ creates directory "scratch/[ESXi_FQDN]" on datastore defined by name in variable $DATASTORE_NAME_FOR_SCRATCH_LOCATION 14 | # Action 2/ Set ESXi advanced parameter ScratchConfig.ConfiguredScratchLocation to point into particular subdirectory 15 | # Action 3/ Set ESXi advanced parameter Syslog.global.logDir to the default scratch location "[] /scratch/log" because 16 | # scratch location is already redirected to shared datastore 17 | # 18 | # All actions above are done for each ESXi host in clusters defined in variable $CLUSTER_NAMES 19 | # 20 | # Disclaimer: 21 | # Use it on your own risk. Author is not responsible for any impacts caused by this script. 22 | ###################################################################################################################################### 23 | # 24 | # CHANGE FOLLOWING VARIABLES BASED ON YOUR SPECIFIC REQUIREMENTS 25 | # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 26 | # 27 | # Array of vSphere Cluster names 28 | $CLUSTER_NAMES = "DEV", "PROD","TEST" 29 | # Datastore name for scratch directories 30 | $DATASTORE_NAME_FOR_SCRATCH_LOCATION = "NFS-SYNOLOGY-SSD" 31 | ###################################################################################################################################### 32 | 33 | Clear-Host 34 | 35 | # Keep old path. At the end of the script we will return working path to the old path. It is necessary before unmounting datastore. 36 | $old_path = Get-Location 37 | 38 | # We need VMware PowerCLI snapins 39 | $o = Add-PSSnapin VMware.VimAutomation.Core 40 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 41 | 42 | # Connect to vCenter 43 | Write-Host "Connecting to vCenter ..." 44 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 45 | Write-Host "Enter vCenter credentials ..." 46 | $CRED = Get-Credential 47 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 48 | 49 | # Validate existence of datastore where scratch directories should be created 50 | try { 51 | $DATASTORE_FOR_SCRATCH_LOCATION = get-datastore -name $DATASTORE_NAME_FOR_SCRATCH_LOCATION -ErrorAction Stop 52 | } catch { 53 | Write-Warning -Message "Datastore $DATASTORE_NAME_FOR_SCRATCH_LOCATION doesn't exist and script cannot continue"; 54 | exit 55 | } 56 | 57 | # Mount a datastore read/write as a PSDrive 58 | try { 59 | Write-Host "Mounting datastore" $DATASTORE_FOR_SCRATCH_LOCATION.Name 60 | New-PSDrive -Name "mounteddatastore" -Root \ -PSProvider VimDatastore -Datastore $DATASTORE_FOR_SCRATCH_LOCATION -ErrorAction Stop | Out-Null 61 | } catch [VMware.VimAutomation.ViCore.Cmdlets.Provider.Exceptions.DriveException] { 62 | Write-Warning -Message "The specified mount name is already in use."; 63 | } catch { 64 | Write-Error "Exception full name: " + $_.Exception.GetType().fullname 65 | exit 66 | } 67 | 68 | foreach ($cluster_name in $cluster_names) { 69 | Write-Host 70 | Write-Host "*****************************" -ForegroundColor Yellow -BackgroundColor Black 71 | Write-Host "Cluster name: [$cluster_name]" -ForegroundColor Yellow -BackgroundColor Black 72 | Write-Host "*****************************" -ForegroundColor Yellow -BackgroundColor Black 73 | 74 | try { 75 | $cluster = get-cluster -name $cluster_name -ErrorAction Stop 76 | foreach ($esx in ($cluster | Get-VMHost)) { 77 | Write-Host "ESXi host name: [$esx.name]" -ForegroundColor:Green -BackgroundColor:Black 78 | # Access the new PSDrive 79 | Set-Location mounteddatastore:\ 80 | # Create a uniquely-named directory for this particular ESXi host 81 | $directory_name = "scratch/"+$esx.name 82 | Write-Host "Creating new directory $directory_name on datastore $DATASTORE_NAME_FOR_SCRATCH_LOCATION" 83 | try { 84 | New-Item $directory_name -ItemType directory -ErrorAction Stop | Out-Null 85 | } catch [Microsoft.PowerShell.Commands.WriteErrorException] { 86 | Write-Warning -Message "The directory already exists."; 87 | } catch { 88 | Write-Warning $_.Exception 89 | } 90 | # Check the current value of the ScratchConfig.ConfiguredScratchLocation configuration option 91 | $current_scratch_location = $esx | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation" 92 | Write-Host "Current scratch location:" $current_scratch_location.Value 93 | # Set the ScratchConfig.ConfiguredScratchLocation configuration option, specifying the full path to the uniquely-named directory 94 | $new_scratch_location = "/vmfs/volumes/$DATASTORE_NAME_FOR_SCRATCH_LOCATION/$directory_name" 95 | Write-Host "New scratch location: $new_scratch_location" 96 | $esx | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation" | Set-AdvancedSetting -Value $new_scratch_location -Confirm:$false 97 | 98 | # Check the current value of the Syslog.global.logDir configuration option 99 | $current_syslog_dir_location = $esx | Get-AdvancedSetting -Name "Syslog.global.logDir" 100 | Write-Host "Current syslog directory location:" $current_syslog_dir_location.Value 101 | # Set the Syslog.global.logDir configuration option, specifying the default scratch location "[] /scratch/log" 102 | $new_syslog_dir_location = "[] /scratch/log" 103 | Write-Host "New syslog directory location: $new_syslog_dir_location" 104 | $esx | Get-AdvancedSetting -Name "Syslog.global.logDir" | Set-AdvancedSetting -Value $new_syslog_dir_location -Confirm:$false 105 | } 106 | } catch { 107 | Write-Warning -Message "Cluster $cluster_name doesn't exist"; 108 | } 109 | 110 | } 111 | 112 | # Unount a datastore used as a PSDrive 113 | try { 114 | Write-Host "Unmounting datastore" $DATASTORE_FOR_SCRATCH_LOCATION.Name 115 | Set-Location -Path $old_path 116 | Remove-PSDrive -Name "mounteddatastore" -PSProvider VimDatastore -ErrorAction Stop 117 | } catch { 118 | Write-Error "Exception full name: $_.Exception.GetType().fullname" 119 | } 120 | 121 | Disconnect-VIserver -Server $VC -Force -Confirm:$false 122 | -------------------------------------------------------------------------------- /set-vm-advanced_params.ps1: -------------------------------------------------------------------------------- 1 | Clear-Host 2 | 3 | $o = Add-PSSnapin VMware.VimAutomation.Core 4 | $o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false 5 | 6 | # Connect to vCenter 7 | Write-Host "Connecting to vCenter ..." 8 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 9 | Write-Host "Enter vCenter credentials ..." 10 | $CRED = Get-Credential 11 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 12 | 13 | # Array of virtual machine names 14 | #$vm_names = "W2K8R2-test1","W2K8R2-test2" 15 | $vm_names = "W2K8R2-test" 16 | 17 | foreach ($vm_name in $vm_names) { 18 | Write-Host "VM: [$vm_name]" 19 | 20 | try { 21 | $vm = get-vm -Name $vm_name -ErrorAction Stop 22 | New-AdvancedSetting -Entity $vm -Name tools.syncTime -Value 0 -Confirm:$false -Force:$true 23 | New-AdvancedSetting -Entity $vm -Name time.synchronize.continue -Value 0 -Confirm:$false -Force:$true 24 | New-AdvancedSetting -Entity $vm -Name time.synchronize.restore -Value 0 -Confirm:$false -Force:$true 25 | New-AdvancedSetting -Entity $vm -Name time.synchronize.resume.disk -Value 0 -Confirm:$false -Force:$true 26 | New-AdvancedSetting -Entity $vm -Name time.synchronize.shrink -Value 0 -Confirm:$false -Force:$true 27 | New-AdvancedSetting -Entity $vm -Name time.synchronize.tools.startup -Value 0 -Confirm:$false -Force:$true 28 | New-AdvancedSetting -Entity $vm -Name time.synchronize.tools.enable -Value 0 -Confirm:$false -Force:$true 29 | New-AdvancedSetting -Entity $vm -Name time.synchronize.resume.host -Value 0 -Confirm:$false -Force:$true 30 | New-AdvancedSetting -Entity $vm -Name svga.vgaOnly -Value 0 -Confirm:$false -Force:$true 31 | } catch { 32 | Write-Warning -Message "VM doesn't exist"; 33 | } 34 | 35 | } 36 | 37 | Disconnect-VIserver -Server $VC -Force -Confirm:$false 38 | -------------------------------------------------------------------------------- /upgrade-vmtools-vmhw.ps1: -------------------------------------------------------------------------------- 1 | cls 2 | 3 | #import vmware modules 4 | Import-module VMware.VimAutomation.Core 5 | Import-module VMware.VimAutomation.Vds 6 | Import-module VMware.VimAutomation.Cloud 7 | Import-module VMware.VimAutomation.PCloud 8 | Import-module VMware.VimAutomation.Cis.Core 9 | Import-module VMware.VimAutomation.Storage 10 | Import-module VMware.VimAutomation.HorizonView 11 | Import-module VMware.VimAutomation.HA 12 | Import-module VMware.VimAutomation.vROps 13 | Import-module VMware.VumAutomation 14 | Import-module VMware.DeployAutomation 15 | Import-module VMware.ImageBuilder 16 | Import-module VMware.VimAutomation.License 17 | 18 | # define new property to be used in the reports 19 | New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.tools.ToolsVersion' -Force 20 | New-VIProperty -Name ToolsVersionStatus -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.ToolsVersionStatus'-Force 21 | 22 | cls 23 | $vCenter = Read-Host "Please enter name or IP address of the source vCenter Server" 24 | connect-viserver $vCenter 25 | 26 | Write-Host "please select file from wich you want to read list of VMs to be upgraded" -ForegroundColor Yellow -BackgroundColor Black 27 | 28 | Function Get-OpenFile($initialDirectory) 29 | { 30 | [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | 31 | Out-Null 32 | 33 | $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog 34 | $OpenFileDialog.initialDirectory = $initialDirectory 35 | $OpenFileDialog.filter = "Text files (*.txt)|*.txt" 36 | $OpenFileDialog.ShowDialog() | Out-Null 37 | $OpenFileDialog.filename 38 | $OpenFileDialog.ShowHelp = $true 39 | } 40 | 41 | $InputFile = Get-OpenFile 42 | $MyVMs = Get-Content $InputFile 43 | 44 | # nebo mozno zadat cestu k souboru 45 | # $MyVMs = get-content C:\PS\MK\A2_NHQ.txt 46 | 47 | # Attach baselines 48 | Get-Baseline -server $vCenter -Name "VMware Tools Upgrade to Match Host (Predefined)" | Attach-Baseline -Entity $MyVMs 49 | Get-Baseline -server $vCenter -Name "VM Hardware Upgrade to Match Host (Predefined)" | Attach-Baseline -Entity $MyVMs 50 | 51 | # Scan 52 | Scan-Inventory -Entity $MyVMs 53 | 54 | cls 55 | # Report before upgrade 56 | Write-Host "VMware Tools and VM HW report before upgrade" -ForegroundColor Yellow -BackgroundColor Black 57 | Get-VM $MyVMs | select Name, Version, ToolsVersion, ToolsVersionStatus |ft -AutoSize 58 | 59 | Write-host "VMware Tools and VM Hardware will be upgraded on following VMs "$MyVMs -ForegroundColor yellow -BackgroundColor Black -Separator "," 60 | 61 | # upgrade VM Tools 62 | $VMTools = Get-Baseline -server $vCenter -Name "VMware Tools Upgrade to Match Host (Predefined)" 63 | foreach ($VM in $MyVMs){Update-Entity -server $vCenter -Baseline $VMTools -Entity $VM} 64 | 65 | # upgrade VM HW 66 | $VMHW = Get-Baseline -server $vCenter -Name "VM Hardware Upgrade to Match Host (Predefined)" 67 | #se snapshotem 68 | foreach ($VM in $MyVMs){Update-Entity -server $vCenter -Baseline $VMHW -Entity $VM -GuestCreateSnapshot:$true -GuestKeepSnapshotHours 24 -GuestSnapshotName BeforeHWUpgrade} 69 | #bez snapshotu 70 | #foreach ($VM in $MyVMs){Update-Entity -server $vCenter -Baseline $VMHW -Entity $VM} 71 | 72 | # Report after upgrade 73 | Write-Host "VMware Tools and VM HW report after upgrade" -ForegroundColor Green -BackgroundColor Black 74 | Get-VM $MyVMs | select Name, Version, ToolsVersion, ToolsVersionStatus |ft -AutoSize -------------------------------------------------------------------------------- /vcenter-sessions.ps1: -------------------------------------------------------------------------------- 1 | Function Get-ViSession { 2 | <# 3 | .SYNOPSIS 4 | Lists vCenter Sessions. 5 | 6 | .DESCRIPTION 7 | Lists all connected vCenter Sessions. 8 | 9 | .EXAMPLE 10 | PS C:\> Get-VISession 11 | 12 | .EXAMPLE 13 | PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 } 14 | #> 15 | $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager 16 | $AllSessions = @() 17 | $SessionMgr.SessionList | Foreach { 18 | $Session = New-Object -TypeName PSObject -Property @{ 19 | Key = $_.Key 20 | UserName = $_.UserName 21 | FullName = $_.FullName 22 | LoginTime = ($_.LoginTime).ToLocalTime() 23 | LastActiveTime = ($_.LastActiveTime).ToLocalTime() 24 | 25 | } 26 | If ($_.Key -eq $SessionMgr.CurrentSession.Key) { 27 | $Session | Add-Member -MemberType NoteProperty -Name Status -Value “Current Session” 28 | } Else { 29 | $Session | Add-Member -MemberType NoteProperty -Name Status -Value “Idle” 30 | } 31 | $Session | Add-Member -MemberType NoteProperty -Name IdleMinutes -Value ([Math]::Round(((Get-Date) – ($_.LastActiveTime).ToLocalTime()).TotalMinutes)) 32 | $AllSessions += $Session 33 | } 34 | $AllSessions 35 | } 36 | 37 | Function Disconnect-ViSession { 38 | <# 39 | .SYNOPSIS 40 | Disconnects a connected vCenter Session. 41 | 42 | .DESCRIPTION 43 | Disconnects a open connected vCenter Session. 44 | 45 | .PARAMETER SessionList 46 | A session or a list of sessions to disconnect. 47 | 48 | .EXAMPLE 49 | PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 } | Disconnect-ViSession 50 | 51 | .EXAMPLE 52 | PS C:\> Get-VISession | Where { $_.Username -eq “User19” } | Disconnect-ViSession 53 | #> 54 | [CmdletBinding()] 55 | Param ( 56 | [Parameter(ValueFromPipeline=$true)] 57 | $SessionList 58 | ) 59 | Process { 60 | $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager 61 | $SessionList | Foreach { 62 | Write “Disconnecting Session for $($_.Username) which has been active since $($_.LoginTime)” 63 | $SessionMgr.TerminateSession($_.Key) 64 | } 65 | } 66 | } 67 | 68 | Clear-Host 69 | 70 | # We need VMware PowerCLI snapin 71 | $o = Add-PSSnapin VMware.VimAutomation.Core 72 | 73 | # Enter vCenter server and credetials 74 | Write-Host "vCenter information ..." 75 | $VC = Read-Host "Enter one vCentre Server or multiple vCenter servers delimted by comma." 76 | Write-Host "Enter vCenter credentials ..." 77 | $CRED = Get-Credential 78 | 79 | 80 | for($i=1; $i -le 2000; $i++) { 81 | Write-Host "Connecting to vCenter ... $i" 82 | Connect-VIServer -Server $VC -Credential $CRED -ErrorAction Stop | Out-Null 83 | # Disconnect-VIserver -Server $VC -Force -Confirm:$false 84 | } 85 | 86 | Write-Host "Get Sessions ..." 87 | Get-ViSession | measure 88 | 89 | # Disconnect sessions onliner 90 | # get-VISession | Where { $_.IdleMinutes -gt 30 } | disconnect-VISession --------------------------------------------------------------------------------