Assistance at your fingertips...

VM PS Templates: Query VMs PoweredOn

This script renumerates through all the VCSAs and makes a big list of the PoweredOn VMs and runs a job against them, like TC (ping)

'RunAgainstAllOnVMs-GetRequestedData.ps1'
#cleanup logs
rm $attachmentPath -force |Out-Null
#Remember to set this before you run the whole script
# Set variable for credential option and store for using in PowerShell scripts over and over
#rm $env:USERPROFILE\localvmcredential.xml
$localvmcredFile = "$env:USERPROFILE\localvmcredential.xml"
if(Test-Path $localvmcredFile){
    $PSCredential_VMWARE = Import-Clixml $localvmcredFile
}
else{
    $localvmusername = "Administrator@WonderWhatThisCouldBe.local"
    $PSCredential_VMWARE = Get-Credential -Credential "$localvmusername"
    $PSCredential_VMWARE | Export-Clixml $localvmcredFile
}
#Remember to set this before you run the whole script
# Get list of VCenters
$vCenterServers = @(
    "DallasVCSA",
    "JohannesburgVCSA",
    "NYCVCSA",
    "YaMuddahsVCSA"
)

# Import the required module
Import-Module VMware.VimAutomation.Core

# Connect to vCenter servers
foreach ($vCenter in $vCenterServers) {
    Connect-VIServer -Server $vCenter -Credential $PSCredential_VMWARE
}

# Get the list of VMs
$VMs = Get-VM

# Initialize an empty array to store the VM status
$VMStatusList = @()

# Check if VMs are online
foreach ($VM in $VMs) {
    # Create a new object for each VM
    $VMStatus = New-Object PSObject
    $VMStatus | Add-Member -MemberType NoteProperty -Name "VMName" -Value $VM.Name

    if ($VM.PowerState -eq "PoweredOn") {
#Two pings makes sure to get an IP#
        $ping = Test-Connection -ComputerName $VM.Name -Count 2 -Quiet
        if (!$ping) {  
            $VMStatus | Add-Member -MemberType NoteProperty -Name "Status" -Value "NoICMPAccess"
            $VMStatus | Add-Member -MemberType NoteProperty -Name "IPAddress" -Value $VM.Guest.IPAddress
            $VMStatus | Add-Member -MemberType NoteProperty -Name "ESXiHost" -Value $VM.VMHost.Name
            # Add the VM status to the list
            $VMStatusList += $VMStatus
            $VMStatus
        }
    }
}

# Disconnect from vCenter servers
foreach ($vCenter in $vCenterServers) {
    Disconnect-VIServer -Server $vCenter -Confirm:$false
}

if ($VMStatusList.count -gt 0) {
    $VMStatusList |out-file d:\powershell\output\$day-VMStatusList.txt ascii -Force -NoOverwrite -NoNewline
    $resultsCount = (gc d:\powershell\output\$day-VMStatusList.txt).count
    $serversCount= $VMs.count
    Write-host "Ran, $resultsCount servers were pinged successfully out of $serversCount total servers."
}

#Email results
If($resultsCount -eq 0){break}
# Define email parameters
$from = "Administrator@AdminServer"
$to = "JoeyG@NoneYaBidness.com"
$subject = "Test of Pinged PoweredOn VMs creation completed"
$body = "RunAgainstAllOnVMs-GetRequestedData.ps1 Ran, $resultsCount servers were pinged successfully out of $serversCount total servers."

# Define SMTP server parameters
$smtpServer = "MyMansMail.NoneYaBidness.com"

# Create a new attachment object
$attachmentPath = "D:\PowerShell\output\*-VMStatusList.txt"
$attachment = New-Object System.Net.Mail.Attachment($attachmentPath)

# Send the email
Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpServer -Attachments $attachment

# Output a confirmation message
Write-Host "Email sent to $to from $from with attachment."

Posted

in

by