Skip to content

Instantly share code, notes, and snippets.

View Dapacruz's full-sized avatar

David Cruz Dapacruz

View GitHub Profile
@Dapacruz
Dapacruz / Migrate-VMToVDSwitch.ps1
Created May 30, 2018 18:40
Migrate Virtual Machines (VM) to a Virtual Distribututed Switch
$vmhost = 'esxi-01.domain.local'
$virtual_machines = Get-VM -Location $vmhost
$vcenter = 'vcenter.domain.local'
$vcenter_user = 'administrator@vsphere.local'
# 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
$vcenter_password = Get-Content 'Distributed Switch Migration\password.txt' | ConvertTo-SecureString
if(-not $global:DefaultVIServers) {
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $vcenter_user, $vcenter_password
@Dapacruz
Dapacruz / Move-VmsToNewVcenter.ps1
Last active May 30, 2018 18:42
Move virtual machines from one VMware vCenter to another
$vm_names = 'SQL2', 'APP2'
$orig_vcenter = 'vc01.client.local'
$new_vcenter = 'vcenter.client.local'
foreach ($vm_name in $vm_names) {
$vm = Get-VM -Server $orig_vcenter -Name $vm_name
$vm_path = $vm.extensiondata.config.files.vmpathname
'Shutting down {0} ...' -f $vm.Name
Stop-VMGuest -Server $orig_vcenter -VM $vm -Confirm:$false | Out-Null
@Dapacruz
Dapacruz / Ping-VMs.ps1
Last active May 30, 2018 21:56
Ping VMware Virtual Machines on an ESXi Host
$ignore = ''
Write-Host; Get-VMHost * |
Get-VM | ? { $_.PowerState -match 'on' } |
Select-Object -Property Name, @{n='IPAddr'; e={$_.Guest.IPAddress}} |
% { foreach ($ip in $_.ipaddr) { $vm_name = $_.name; $vm_ip = $ip; if ($ip -match '^\d{1,3}\.' -and $ip -notin $ignore) { ping -n 1 $ip |
% { if (Select-String -InputObject $_ -SimpleMatch 'Request timed out') { '{0} ({1})' -f $vm_name, $vm_ip; Select-String -InputObject $_ -SimpleMatch "Pinging|Reply|Request"; Write-Host }}}}}
@Dapacruz
Dapacruz / Remove-Datastores.ps1
Last active June 19, 2018 21:45
Remove VMware vSphere Datastores
break
$eql_group_addr = '1.1.1.1'
$eql_user = 'grpadmin'
$vcenter_addr = 'vcenter.domain.loc'
$vcenter_user = 'administrator@vsphere.local'
$vmhosts = '*'
$creds = Get-Credential -UserName $vcenter_user -Message 'Enter Password'
Connect-VIServer -Server $vcenter_addr -Credential $creds
@Dapacruz
Dapacruz / Get-ComputerIPv4NetworkInfo.ps1
Last active June 20, 2018 15:32
Get Windows Computer IPv4 Network Information
$hostname = 'localhost'
Get-WmiObject -ComputerName $hostname -Class win32_networkadapterconfiguration |
select PSComputerName, Description, MacAddress, @{n='IPAddress'; e={$_.IPAddress -match "(\d{1,3}\.){3}\d{1,3}"}}, DHCPEnabled, @{n='DefaultIPGateway'; e={$_.DefaultIPGateway -match "(\d{1,3}\.){3}\d{1,3}"}}, DNSDomain, ServiceName |
? {$_.MacAddress -and $_.IPAddress}
@Dapacruz
Dapacruz / Validate-VMhostNetworking.ps1
Last active July 12, 2018 21:22
Validate VMware vSphere Host Networking
# Test ESXi Host Networking
$vmhosts = 'test_esxi-01.domain.net', 'test_esxi-02.domain.net'
$mgmt_vmks = 'vmk0'
$mgmt_addrs = Get-VMHostNetworkAdapter -VMHost $vmhosts -Name $mgmt_vmks | select -ExpandProperty IP
$vmotion_vmks = 'vmk2', 'vmk3'
$vmotion_addrs = Get-VMHostNetworkAdapter -VMHost $vmhosts -Name $vmotion_vmks | select -ExpandProperty IP
@Dapacruz
Dapacruz / Nmap-Ping-Sweep.ps1
Last active July 12, 2018 21:29
Nmap Ping Sweep of Multiple Subnets
$subnets = @(
'10.1.1.253/22'
'10.10.10.252/24'
'10.1.20.253/24'
'10.120.10.252/22'
'10.220.10.252/22'
'10.254.254.253/24'
'10.100.1.249/22'
'10.200.1.249/22'
'10.30.1.251/22'
@Dapacruz
Dapacruz / Watch-Device.ps1
Last active July 19, 2018 14:54
Monitor a Device During a Reboot and Receive a Text-to-Speech Notification When it is Back Online
# Monitor a device during a reboot and receive an text-to-speech notification when it is back online
# Requires PowerShell Community Extensions (Install-Module -Name PSCX)
$ip_address = '8.8.4.4'
while (-not (Test-Connection -ComputerName $ip_address -Quiet -Count 1)) {sleep 5}; Out-Speech "Host $ip_address is back online!"
@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 / Recover-VM.ps1
Last active July 23, 2018 17:13
Recover Virtual Machine
break
$vmhost = '*'
$vcenter = 'vcenter.domain.local'
$vcenter_user = 'administrator@vsphere.local'
$guest_creds = Get-Credential 'administrator'
# Save an encrypted password to a file and retrieve it later for use in a script
# Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File vc_password.txt
$vcenter_password = Get-Content 'vc_password.txt' | ConvertTo-SecureString