Skip to main content

Get Local Administrators

Summary

This task lists the local administrators on windows machine and stores the result in a custom field Local Admins List.

Sample Run

Sample Run 1

Sample Run 2

Dependencies

Task Creation

Script Details

Step 1

Navigate to AutomationTasks
step1

Step 2

Create a new Script Editor style task by choosing the Script Editor option from the Add dropdown menu
step2

The New Script page will appear on clicking the Script Editor button:
step3

Step 3

Fill in the following details in the Description section:

  • Name: Get Local Administrators
  • Description: This script lists the local administrators on windows machine and stores the result in a custom field "Local Admins List".
  • Category: Custom

Summary

Script Editor

Click the Add Row button in the Script Editor section to start creating the script
AddRow

A blank function will appear:
BlankFunction

Row 1 Function: PowerShell script

  • Use Generative AI Assist for script creation: False
  • Expected time of script execution in seconds: 900
  • Operating System: Windows
  • Continue on Failure: True
  • PowerShell Script Editor:
try {
$AllUsers = net localgroup administrators 2>&1

if ($LASTEXITCODE -ne 0) {
throw "Result: Failed to execute 'net localgroup administrators'. Output: $AllUsers"
}

$users = $AllUsers |
Where-Object {
$_ -and
$_ -notmatch 'command completed|---|Alias name|Members|comment'
} |
ForEach-Object { $_.Trim() }

if (-not $users -or $users.Count -eq 0) {
write-output "Result: No local administrator accounts were found."
exit 0
}

$users -join '|'
}
catch {
Write-Error "Result: Error fetching local administrators: $($_.Exception.Message)"
throw
}

image4

Row 2 Function: Script Log

  • Script Log Message: %Output%
  • Continue on Failure: False
  • Operating System: Windows

Image5

Step 3 Logic: If/Then

Click on Add Logic > select If/Then

Row 3a Condition: Output Contains

  • Condition: Output
  • Operator: Does not Contain
  • Input Values: Result

Image6

Row 3b Function: Set Custom Field

  • Select Local Admins List from dropdown
  • Add %output% in the Value

Image7

Save Task

Click the Save button at the top-right corner of the screen to save the script.
SaveButton

Completed Task

Completed Task

Output

  • Script Log

Schedule Task

Task Details

  • Name: Get Local Administrators
  • Description: This script lists the local administrators on windows machine and stores the result in a custom field "Local Admins List".
  • Category: Custom

Image9

Schedule

  • Schedule Type: Schedule
  • Timezone: Local Machine Time
  • Start: <Current Date>
  • Trigger: Time At <Current Time>
  • Recurrence: Every day

Image10

Targeted Resource

Device Group: Machines Opted for Local Admin Detection

Image11

Completed Scheduled Task

Image12