Skip to content

Instantly share code, notes, and snippets.

View Dapacruz's full-sized avatar

David Cruz Dapacruz

View GitHub Profile
@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 / 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 / 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 / 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 / Get-IscsiDiskInfo.ps1
Last active September 20, 2018 16:26
Get iSCSI Disk Information
$output = "$($env:TEMP)\iSCSI_Disks.csv"
$results = @()
$disk = Get-Disk | Where-Object {$_.BusType -eq 'iSCSI'} | Sort-Object -Property Number
foreach ($d in $disk) {
$partition = Get-Partition -DiskNumber $d.Number
$iscsi_session = Get-IscsiSession -Disk $d
$obj = New-Object -TypeName PSObject
Add-Member -InputObject $obj -MemberType NoteProperty -Name DiskNumber -Value $d.Number
@Dapacruz
Dapacruz / Generate-VMwareVsphereDocumentation.ps1
Last active August 17, 2018 17:17
Generate VMware vSphere Documentation
# Host CPU ratio
Get-VMHostCpuRatio | Export-Csv -NoTypeInformation 'VMware Host CPU Ratio.csv'
# Host datastores
Get-VMHostDatastores | Export-Csv -NoTypeInformation 'VMware Host Datastores.csv'
# Host CDP/LLDP info
Get-VMHostNetworkCdpInfo | Export-Csv -NoTypeInformation 'VMware Host CDP Info.csv'
Get-VMHostNetworkLldpInfo | Export-Csv -NoTypeInformation 'VMware Host LLDP Info.csv'
@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
@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 / 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!"