Skip to content

Instantly share code, notes, and snippets.

View dapacruz's full-sized avatar

David Cruz dapacruz

View GitHub Profile
@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 / Get-VmRdm.ps1
Created February 2, 2018 23:27
Get VMware vSphere RDMs
# Get vSphere RDMs
Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName
@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 / 1_Test-FirewallServicesOutside.ps1
Last active December 8, 2018 21:05
Firewall Migration Validation
$services = Import-Csv -Path Services.csv
$fname = "Report-$(get-date -f yyyyMMdd.HHmmss).json"
$report = @()
# Export services to JSON for readability
ConvertTo-Json -InputObject $services | Out-File -FilePath Services.json -Force -Confirm:$false
foreach ($svc in $services) {
foreach ($addr in $svc.Destination_Address.split(',')) {
$obj = New-Object -TypeName PSObject
@dapacruz
dapacruz / Send-MailMessage.ps1
Last active November 26, 2018 17:03
Send Email From PowerShell
$params = @{
To = 'user@domain.com'
From = 'test@client.com'
Subject = 'Test Alert'
Body = ' '
SmtpServer = 'smtp-relay.gmail.com'
UseSsl = $true
}
Send-MailMessage @params
@dapacruz
dapacruz / Configure-VCEmailAlerts.ps1
Last active May 30, 2018 16:50
Set Up VMware vCenter Email Alerts
$smtp_sender = 'vcenter@domain.com'
$smtp_recipients = 'support@domain.com'
$smtp_server = 'smtp.domain.com'
$smtp_user = ''
$smtp_password = ''
$smtp_port = '25'
$vcenter = $global:defaultviservers.Name
$alarms = @(
@{
alarm_definition = 'Datastore usage on disk'
@dapacruz
dapacruz / Configure-ADAuthentication.ps1
Created May 30, 2018 18:05
Configure Active Directory Authentication on VMware ESXi Hosts
$hosts = '*'
$domain = 'domain.local'
$ad_group = 'Domain Admins'
$user = 'user'
$password = 'password'
Get-VMHostAuthentication -VMHost $hosts | Set-VMHostAuthentication -JoinDomain -Domain $domain -User $user -Password $password
Get-AdvancedSetting -Entity $hosts -Name Config.HostAgent.plugins.hostsvc.esxAdminsGroup | Set-AdvancedSetting -Value $ad_group
@dapacruz
dapacruz / Create-NimbleDatastore.ps1
Created May 30, 2018 18:26
Create Nimble Volumes and VMware vSphere Datastores
$vmhost = 'esxi-01.domain.local'
$vcenter = 'vcenter.domain.local'
$vcenter_user = 'administrator@vsphere.local'
# Save encrypted password to a file
# Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File vcenter-password.txt
$vcenter_password = Get-Content vcenter-password.txt | ConvertTo-SecureString
$nimble_mgmt_address = '10.1.1.1'
$nimble_user = 'admin'
# Save encrypted password to a file
@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 / 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 }}}}}