Difference between revisions of "Script Extracts and Examples (PowerCLI)"

Jump to navigation Jump to search
→‎Script Extracts: Added "ESX Log Tail"
(→‎Virtual Machine: Added Snapshot info)
(→‎Script Extracts: Added "ESX Log Tail")
Line 529: Line 529:
     $result
     $result
     Write-Host
     Write-Host
}
</source>
=== ESX Log Tail ===
Pointless for ESX (you can just use tail from the Service Console on the [ESX#Useful_paths_.2F_logfiles|appropriate log]), but a godsend it you're using ESXi (and have got used to tailing ESX logs).
<source lang="powershell">
$PollInterval = 1000    # msec (default - can be overrided by user)
#Get ESX to poll
$ESXs = Get-VMHost | Sort-Object -Property Name
Write-Host "`nWhich ESX's log do you want to tail?"
$num = 0
foreach ($esx in $ESXs) {
    Write-Host "[$num]" $ESXs[$num].Name
    $num++
}
$num = 0
$num = Read-Host "? [$num] "
$ESX = Get-VMHost $ESXs[$num]
Write-Host " "
# Get Log keys to look at
$keys = Get-LogType -VMHost $ESX
Write-Host "`nWhich log do you want to tail?"
$num = 0
foreach ($key in $keys) {
    Write-Host "[$num]" $keys[$num].Key
    $num++
}
$num = 0
$num = Read-Host "? [$num] "
$logKey = $keys[$num].Key
# Set polling interval
$PollInterval = Read-Host "`nWhat polling interval do you want to use (msec)? [$PollInterval]"
Write-Host "`nStarting log tail (press Ctrl+C to escape)...`n"
# First pass of log
$ESXLog = Get-Log $LogKey -VMHost $ESX
#Display last $lines
$LineNo = $ESXLog.LastLineNum - $lines
While ($LineNo -le $ESXLog.LastLineNum) {
    Write-Host $ESXlog.Entries[$LineNo]
    $LineNo++
}
#Polling loop
While (1) {
    Start-Sleep -Milliseconds $PollInterval
    try {
        $ESXLog = Get-Log $logKey -VMHost $ESX -StartLineNum $LineNo
    } catch {
        if (Select-String -InputObject $_ -pattern "The 0 argument is less than the minimum allowed range of 1" -Quiet) {
            Write-Host "ESX log is rolling over..."
            $ESXLog = Get-Log $logKey -VMHost $ESX
        } else {
            Write-Host "UNEXPECTED ERROR: $_"
            Exit
        }
    }
    $ESXlog.Entries
    $LineNo = $ESXLog.LastLineNum
}
}
</source>
</source>

Navigation menu