Skip to content

Instantly share code, notes, and snippets.

View Dapacruz's full-sized avatar

David Cruz Dapacruz

View GitHub Profile
@Dapacruz
Dapacruz / VM Snapshot Report.ps1
Last active November 19, 2018 18:45
Get a VM snapshot report and output to clipboard, CSV or console.
## CSV format out to clipboard
Get-Snapshot -VM * | select VM,Name,Created,PowerState,Description,@{n='SizeGB';e={'{0:N2}' -f $_.sizegb}} | sort SizeGB | ConvertTo-Csv -NoTypeInformation -Delimiter `t | clip
## Out to CSV
Get-Snapshot -VM * | select VM,Name,Created,PowerState,Description,@{n='SizeGB';e={'{0:N2}' -f $_.sizegb}} | sort SizeGB | Export-Csv -NoTypeInformation -Path 'VMware Virtual Machine Snapshots.csv'
## Out to console
Get-Snapshot -VM * | select VM,Name,Created,PowerState,Description,@{n='SizeGB';e={'{0:N2}' -f $_.sizegb}} | sort SizeGB | ft -auto
@Dapacruz
Dapacruz / Get-VmInventory.ps1
Last active July 20, 2018 01:10
Get a list of all VMware virtual machines (VMs), including the VMX file location (VM Inventory).
# VMware PowerCLI
# Virtual machine (VM) inventory report
$properties = @(
'Name'
@{n='HostName';e={$_.ExtensionData.Guest.HostName}}
@{n='IpAddress';e={$_.Guest.IpAddress.where{$_ -match '(\d{1,3}\.){3}\d{1,3}'} -join ', '}}
@{n='GuestFullName';e={$_.ExtensionData.Guest.GuestFullName}}
@{n='ToolsVersion';e={$_.Guest.ToolsVersion}}
@{n='ToolsBuild';e={$_.Guest.VM.ExtensionData.Guest.ToolsVersion}}
@{n='ToolsStatus';e={$_.Guest.VM.ExtensionData.Guest.ToolsStatus}}
@Dapacruz
Dapacruz / ping_iscsi.sh
Last active October 5, 2017 16:54
Ping All VMware ESXi IP Addresses
# Get a list of IP addresses with PowerCLI
$Vmhosts = 'sac0esxi*'
$Vmks = 'vmk2', 'vmk3', 'vmk5', 'vmk6'
Get-VMHostNetworkAdapter -VMHost $Vmhosts -Name $Vmks | sort VMHost, Name | ft -auto VMHost, Name, IP
# ESXi host Bash function to ping all iSCSI addresses
ping_iscsi () {
mtu=8972
failures=0
vmks="vmk2
@Dapacruz
Dapacruz / get_vmhost_lldp_info.sh
Last active October 5, 2017 16:39
Get VMware ESXi vmnic LLDP info (get_vmhost_lldp_info.sh)
# VMware ESXi shell script
# Get LLDP info for vmnics (use the get_vmhost_lldp_info_raw function if output is not complete)
vmnics="vmnic4 vmnic5"
get_vmhost_lldp_info(){
for vmnic in $vmnics; do
printf "$vmnic [=]----[=] "
pktcap-uw --uplink $vmnic --ethtype 0x88cc -c 1 -o /tmp/vmnic_lldp.pcap > /dev/null
hexdump -C /tmp/vmnic_lldp.pcap | awk -F'|' '{printf $2}' |
sed -E 's|.+\.([etgETG][teiTEI][a-zA-Z]+ ?-?([0-9]+/)+[0-9]+)(.\.+.{1,2}\.+)+([a-zA-Z][a-zA-Z0-9]+)\..*|\1 (\4)\n|'
@Dapacruz
Dapacruz / Disable-VmCbt.ps1
Last active January 29, 2018 18:30
Enable/Disable Changed Block Tracking (CBT) on VMware Virtual Machines
# Disable CBT on virtual machines
$vms = (Get-VM | Get-View).Where{$_.ChangeTrackingEnabled -match 'True'}.Name
foreach ($vm in $vms) {
$vm_view = Get-vm $vm | get-view
$vm_config_spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vm_config_spec.changeTrackingEnabled = $false
$vm_view.reconfigVM($vm_config_spec)
$snap = New-Snapshot (Get-VM $vm) -Name "Disable CBT"
$snap | Remove-Snapshot -confirm:$false
@Dapacruz
Dapacruz / Enable-VmConsoleCopyPaste.ps1
Last active March 28, 2018 18:57
Enable Copy/Paste Between the Guest Operating System and VMware Virtual Machine Remote Console
$vm = 'vm1', 'vm2'
Get-VM $vm | New-AdvancedSetting -name "isolation.tools.paste.disable" -Value $false -Confirm:$false -Force
Get-VM $vm | New-AdvancedSetting -name "isolation.tools.copy.disable" -Value $false -Confirm:$false -Force
# Power off the VM and back on
Stop-VMGuest -VM $vm -Confirm:$false | Out-Null
while ((Get-VM -Name $vm).PowerState -ne 'PoweredOff') {Start-Sleep 2}
Start-VM -VM $vm -ErrorAction SilentlyContinue
@Dapacruz
Dapacruz / 1 Save-EncryptedPassword.ps1
Last active April 27, 2018 17:34
Save an encrypted password to a file and retrieve it later for use in a PowerShell script
# Save an encrypted password to a file and retrieve it later for use in a script
Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File password.txt
$user = 'user'
$password = Get-Content password.txt | ConvertTo-SecureString
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
@Dapacruz
Dapacruz / Configure-Ntp.ps1
Created December 7, 2017 00:02
Configure/Enable NTP on a VMware ESXi Host
$hosts = '*'
$ntp_servers = '10.10.1.10', '10.10.1.5'
Get-VMHostService -VMHost $hosts | Where-Object {$_.key -eq 'ntpd'}
Get-VMHostNtpServer -VMHost $hosts
Add-VMHostNtpServer -VMHost $hosts -NtpServer $ntp_servers
Get-VMHostService -VMHost $hosts | Where-Object {$_.key -eq 'ntpd'} | Start-VMHostService | Set-VMHostService -Policy 'automatic'
@Dapacruz
Dapacruz / Redirect-VMHostLogs.ps1
Last active October 16, 2018 21:29
Redirect VMware ESXi Host Logs
$hosts = '*'
$datastore_name = 'NS-VMFS-01'
$datastore = Get-Datastore -Name $datastore_name | Select-Object -First 1
$log_dir = '[{0}] Logs' -f $datastore.Name
$scratch_location = '{0}/Logs' -f $datastore.ExtensionData.Info.Url -replace 'ds://'
foreach ($h in (Get-VMHost $hosts | Sort-Object -Property Name)) {
Write-Host "$($h.Name): Redirecting log files ..."
$result = Get-AdvancedSetting -Entity $h -Name Syslog.global.logDir | Set-AdvancedSetting -Value "$log_dir/$h" -Confirm:$false
Write-Host "New syslog location: $($result.Value)"
@Dapacruz
Dapacruz / Copy-FilesToDatastore.ps1
Last active May 22, 2018 18:11
Copy Files to a VMware vSphere Datastore from PowerCLI
$datastore = Get-Datastore -Name 'ISOs' | Select-Object -First 1
$src = 'C:\Temp\VMware-VCSA-all-6.5.0-8307201.iso'
$ds = New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root '\'
Copy-DatastoreItem -Item $src -Destination ds:\
Remove-PSDrive $ds