Figure out what locked a user in AD by using this script, the output will return a username, endpoint device that locked the user, what DC received the event & a timestamp.
$DC = Get-ADDomainController -Filter * | Select Hostname
$properties = @(
@{n='User';e={$_.Properties[0].Value}},
@{n='Locked by';e={$_.Properties[1].Value}},
@{n='TimeStamp';e={$_.TimeCreated}},
@{n='DCName';e={$_.Properties[4].Value}}
)
Foreach ($D in $DC)
{
Get-WinEvent -ComputerName $D.Hostname -FilterHashTable @{LogName='Security'; ID=4740} |
Select $properties
}
2. Figure out who shutdown a server, if it was by a user or an application did it. The output will return timestamp, user, reason they wrote & if it was a shutdown or reboot
$properties = @(
@{n='TimeStamp';e={$_.TimeCreated}},
@{n='User';e={$_.Properties[6].Value}},
@{n='Reason/Application';e={$_.Properties[0].Value}},
@{n='Action';e={$_.Properties[4].Value}}
)
Get-WinEvent -FilterHashTable @{LogName='System'; ID=1074} |
Select $properties | Sort-Object "$_.TimeCreated"
3. A minor script to gather OS information from servers on your domain the output will return;
Servername, OS, RAM, Free space on C, and totalsize of C, can easily add more drives to monitor / gather information from.
$Serverlist = Get-ADComputer -Filter * -Properties OperatingSystem, DnsHostname | Where OperatingSystem -Like "*Server*" | Select DnsHostname
$MasterList = @()
foreach ($Server in $Serverlist) {
$MyObject = New-Object PSObject -Property @{
Servername = $Server.DnsHostname
OS = (Get-CimInstance -ComputerName $Server.DnsHostname -ClassName Win32_OperatingSystem).Caption
RAM = (Invoke-command $Server.DnsHostname {(systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim()})
CFreeGB = (Invoke-Command $Server.DnsHostname {(Get-WmiObject win32_logicaldisk -Filter "DeviceID='C:'").FreeSpace / 1gb -as [int]})
CTotalGB = (Invoke-Command $Server.DnsHostname {(Get-WmiObject win32_logicaldisk -Filter "DeviceID='C:'").Size / 1gb -as [int]})
}
$MasterList += $MyObject
}
# Select fields in a specific order rather than random.
$MasterList | Select Servername, OS, RAM, CTotalGB, CFreeGB | Export-csv C:\Users\$env:username\Desktop\ServerOverview.csv -NoTypeInformation -Encoding Unicode
4. This script will prompt you for a servername, when entered it will create 2 AD-groups
"Servername Remote" & "Servername Admin" and give it the needed descriptions to differenciate between the two. When the group is created it will remote powershell to the server you entered and add the groups into the respective local groups so "Servername remote" goes into "Remote desktop users" and "Servername admin" goes into "Administrators" local groups. This way it is easy to manage who have access to what, and fast to create.
$ServerName = Read-host 'Enter servername'
New-ADGroup -Name "$ServerName Remote" -Description "Remote Desktop permissions to $ServerName" -GroupCategory Security -GroupScope Global -Path "OU=Groups,DC=domain,DC=com"
New-ADGroup -Name "$ServerName Admin" -Description "Local administrator permissions to $ServerName" -GroupCategory Security -GroupScope Global -Path "OU=Groups,DC=domain,DC=com"
Timeout 5
Invoke-Command $ServerName -ScriptBlock {Net Localgroup "Remote Desktop Users" "domain.com\$Using:ServerName Remote" /add}
Invoke-Command $ServerName -ScriptBlock {Net Localgroup Administrators "domain.com\$Using:ServerName Admin" /add}