Server CORE-KMS ADK Get last activation from DB LastActivation Invoke-

 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
$Server = "CORE-KMS\ADK"
# Get last activation from DB;
$LastActivation = Invoke-Sqlcmd -ServerInstance $Server -Query "SELECT TOP 1 EventDate FROM KMS.dbo.kms_activations ORDER BY ID DESC;"
$DateTime = Get-Date $LastActivation['EventDate']
# Get activations from Event Logs after last in DB;
$Activations = Get-EventLog "Key Management Service" -After $DateTime
#$Activations = $Activations[0..($Activations.Length-2)]
#[array]::Reverse($Activations)
$Activations | ForEach-Object {
$Activation = $_.Message.Split(",")
$EventDate = $_.TimeGenerated
$ComputerName = $Activation[2]
$CMID = $Activation[3]
$Timestamp = $Activation[4]
$LicenseState = $Activation[6]
$TimeToState = $Activation[7]
$ProductID = $Activation[8]
#"INSERT INTO KMS.dbo.kms_activations (ComputerName, CMID, Timestamp, LicenseState, TimeToState, ProductID) VALUES ('$ComputerName', '$CMID', '$Timestamp', '$LicenseState', '$TimeToState', '$ProductID');" | Out-File -Append C:\scripts\todb.sql
Invoke-Sqlcmd -ServerInstance $Server -Query "INSERT INTO KMS.dbo.kms_activations (EventDate, ComputerName, CMID, Timestamp, LicenseState, TimeToState, ProductID) VALUES ('$EventDate', '$ComputerName', '$CMID', '$Timestamp', '$LicenseState', '$TimeToState', '$ProductID');"
}