-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathlast_poweroff.ps1
54 lines (50 loc) · 1.49 KB
/
last_poweroff.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Define Script Parameters
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty]
[string]$Server,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty]
[string]$VM
)
# Check if VMware Snappin is loaded for when ran outside of PowerCli
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host "[*] VMware Automation Snapin is not loaded, attempting to load..." -ForegroundColor Green
Try {
Add-PsSnapin VMware.VimAutomation.Core | Out-Null
}
Catch {
$Host.UI.WriteErrorLine("[-] Could not load snapping check that PowerCli is installed.")
exit
}
}
#verify we're connected to a VM host
Try {
if (get-vmhost -Server $Server -State connected -erroraction "Stop") {
$connected=$True
}
}
Catch {
#Try to connect
Try {
Write-Host -ForegroundColor Green "[*] Connecting to $Server"
$viserver=Connect-VIserver -Server $Server -errorAction "Stop"
$connected=$True
}
Catch {
$msg="[-] Failed to connect to server $Server"
Write-Warning $msg
Write-Warning $error[0].Exception.Message
}
}
$po_events = Get-VIEvent | where {$_.FullFormattedMessage -match "(shutdown|powered off|reset)"}
foreach ($e in $po_events){
if ($e.FullFormattedMessage -match $VM){
Write-Host "User: "$e.username
write-host "Time: "$e.CreatedTime.DateTime
write-host "Action: "$e.FullFormattedMessage
Write-Host
}
}