create Folder latest / move Update out of Version to rename
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
# Funktion zum Konvertieren des LastLogonTimestamp in Datum
|
||||
function Convert-LastLogonTimestamp {
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[long]$Timestamp
|
||||
)
|
||||
|
||||
$DateTime = [DateTime]::FromFileTime($Timestamp)
|
||||
return $DateTime
|
||||
}
|
||||
|
||||
# Funktion zum Abrufen der lokalen IP-Adresse
|
||||
function Get-LocalIPAddress {
|
||||
$ipAddress = [System.Net.Dns]::GetHostAddresses([System.Net.Dns]::GetHostName()) |
|
||||
Where-Object { $_.AddressFamily -eq 'InterNetwork' } |
|
||||
Select-Object -First 1
|
||||
return $ipAddress.IPAddressToString
|
||||
}
|
||||
|
||||
# Gruppe "Users" abrufen
|
||||
$groupName = "Reporting"
|
||||
$group = Get-ADGroup -Filter { Name -eq $groupName }
|
||||
|
||||
if ($group -eq $null) {
|
||||
Write-Error "Gruppe '$groupName' wurde nicht gefunden."
|
||||
exit
|
||||
}
|
||||
|
||||
# Mitglieder der Gruppe abrufen
|
||||
$groupMembers = Get-ADGroupMember -Identity $group -Recursive | Where-Object { $_.objectClass -eq 'user' }
|
||||
|
||||
# Abrufen der lokalen IP-Adresse
|
||||
$localIPAddress = Get-LocalIPAddress
|
||||
|
||||
# Erstellen einer Hash-Tabelle zum Speichern der letzten Anmeldeinformationen für jeden Benutzer
|
||||
$userLogins = @{}
|
||||
|
||||
# Umwandeln der Benutzerinformationen und Ausgabe zur Konsole
|
||||
foreach ($member in $groupMembers) {
|
||||
$user = Get-ADUser -Identity $member.SamAccountName -Properties LastLogonTimestamp,createTimeStamp
|
||||
$lastLogonDateTime = if ($user.LastLogonTimestamp) { (Convert-LastLogonTimestamp -Timestamp $user.LastLogonTimestamp).ToString("yyyy-MM-dd HH:mm:ss") } else { ($user.createTimeStamp).ToString("yyyy-MM-dd HH:mm:ss") }
|
||||
|
||||
$convertedUser = [PSCustomObject]@{
|
||||
username = $user.SamAccountName
|
||||
lastaccess = $lastLogonDateTime
|
||||
ipaddress = $localIPAddress
|
||||
}
|
||||
|
||||
# Ausgabe des konvertierten Benutzers zur Konsole
|
||||
Write-Output $convertedUser
|
||||
|
||||
# Rückgabe des konvertierten Benutzers für JSON-Umwandlung
|
||||
$userLogins[$user.SamAccountName] = $convertedUser
|
||||
}
|
||||
|
||||
# Define the JSON file path with current date and hour
|
||||
$dateString = (Get-Date).ToString("yyyyMMdd_HH-mm")
|
||||
$jsonPath = "C:\Scripte\LastLogins_$dateString.json"
|
||||
|
||||
# Output the last login event for each user to the JSON file
|
||||
$userLoginsArray = $userLogins.GetEnumerator() | ForEach-Object {
|
||||
$_.Value
|
||||
}
|
||||
|
||||
$userLoginsArray | ConvertTo-Json | Set-Content -Path $jsonPath -Encoding UTF8
|
||||
|
||||
Write-Output "JSON file created at $jsonPath"
|
||||
|
||||
# API endpoint URL
|
||||
$apiUrl = "http://api.stines.de:8001/report"
|
||||
|
||||
# Your API key
|
||||
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
|
||||
$headers.Add("Content-Type", "application/json")
|
||||
$headers.Add("access_token", "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8")
|
||||
|
||||
# Loop through the collected user logins and send each as a JSON payload to the API
|
||||
foreach ($userLogin in $userLoginsArray) {
|
||||
$userLoginObject = $userLogin | Select-Object username, lastaccess, ipaddress
|
||||
|
||||
# Convert the user login object to JSON
|
||||
$jsonPayload = $userLoginObject | ConvertTo-Json -Depth 3
|
||||
|
||||
# Encode JSON payload in UTF-8
|
||||
$utf8JsonPayload = [System.Text.Encoding]::UTF8.GetBytes($jsonPayload)
|
||||
|
||||
# Send the JSON payload to the API
|
||||
try {
|
||||
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body $utf8JsonPayload -ContentType 'application/json' -Headers $headers
|
||||
Write-Output "Sent login data for user $($userLoginObject.username) to the API. Response: $response"
|
||||
} catch {
|
||||
Write-Error "Failed to send login data for user $($userLoginObject.username). Error: $_"
|
||||
}
|
||||
}
|
||||
|
||||
Write-Output "Finished sending login data to the API"
|
||||
Reference in New Issue
Block a user