# Force PowerShell to use TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Disable SSL certificate validation (For demonstration purposes only, not recommended for production)
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
# Prompt the user for vCenter details
$vcServer = Read-Host "Enter vCenter IP or FQDN"
$username = Read-Host "Enter vCenter username"
$password = Read-Host "Enter vCenter password" -AsSecureString
# Convert the secure password to an unsecure string for API call (Not recommended for production, use secure methods instead)
$unsecurePassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
# API Endpoints
$sessionURL = "https://$vcServer/rest/com/vmware/cis/session"
$vmListURL = "https://$vcServer/rest/vcenter/vm"
# Step 1: Obtain the Session Token using Invoke-RestMethod
# Prepare the credential headers for the API request
$basicAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${username}:${unsecurePassword}"))
$headers = @{
Authorization = "Basic $basicAuth"
}
# Execute the POST request to get the session token
$sessionTokenResponse = Invoke-RestMethod -Uri $sessionURL -Method Post -Headers $headers
# Check if we've got a token
if (-not $sessionTokenResponse.value) {
Write-Error "Failed to obtain session token. Response: $($sessionTokenResponse | Out-String)"
exit
}
# Extract the session token
$sessionToken = $sessionTokenResponse.value
# Step 2: Use the Session Token to List VMs using Invoke-RestMethod
# Prepare the authorization header using the session token
$headers = @{
"vmware-api-session-id" = $sessionToken
}
# Execute the GET request to list VMs
$vmListResponse = Invoke-RestMethod -Uri $vmListURL -Method Get -Headers $headers
# Check if we received a response
if (-not $vmListResponse.value) {
Write-Error "Failed to retrieve VM list. Response: $($vmListResponse | Out-String)"
exit
}
# Step 3: Display VM Information, Power Status, and VMware Tools Status
# Loop through the VMs and display their names, IDs, Power status, and Tools status
$vmListResponse.value | ForEach-Object {
# Prepare the VM power state URL
$vmpowerStateURL = "$vmListURL/$($_.vm)/power"
# Execute the GET request to get the power state of the current VM
$vmPowerStateResponse = Invoke-RestMethod -Uri $vmpowerStateURL -Method Get -Headers $headers
# Check if we received a response
if (-not $vmPowerStateResponse.value) {
Write-Error "Failed to retrieve VM power state. Response: $($vmPowerStateResponse | Out-String)"
exit
}
# Prepare the VM Tools status URL
$vmToolsURL = "$vmListURL/$($_.vm)/tools"
# Execute the GET request to get the VMware Tools status
$vmToolsResponse = Invoke-RestMethod -Uri $vmToolsURL -Method Get -Headers $headers
# Check if we received a response
if (-not $vmToolsResponse.value) {
Write-Error "Failed to retrieve VMware Tools status. Response: $($vmToolsResponse | Out-String)"
exit
}
# Extract and Print VM Power state and VMware Tools status with VM name and VM id
Write-Host "VM Name: $($_.name), VM ID: $($_.vm), VM PowerState: $($vmPowerStateResponse.value.state)"
Write-Host "VM Tools Status:"
Write-Host " Auto Update Supported: $($vmToolsResponse.value.auto_update_supported)"
Write-Host " Upgrade Policy: $($vmToolsResponse.value.upgrade_policy)"
Write-Host " Install Attempt Count: $($vmToolsResponse.value.install_attempt_count)"
Write-Host " Version Status: $($vmToolsResponse.value.version_status)"
Write-Host " Version Number: $($vmToolsResponse.value.version_number)"
Write-Host " Version: $($vmToolsResponse.value.version)"
Write-Host " Install Type: $($vmToolsResponse.value.install_type)"
Write-Host " Run State: $($vmToolsResponse.value.run_state)"
}
Leave a Comment