I’m trying to create a script that checks “LogonAsBatch” rights on remote servers. It returns the SID so I tried to create a function to convert the SID to name by using “wmic useraccount where SID=## get name” command. This command needs to execute on the remote server. The script is not converting the SID to name. I’m not sure if I’m calling the wmic command correctly. I keep getting an error “NotSpecified: ( , RemoteException” along with the server it’s on. Tried ChatGPT but it doesn’t know what it’s doing.
# Import servers from TXT file
$servers = Get-Content -Path C:\tmp\server_input.txt
# Create an array to store results
$results = @()
# Function to translate user account to SID using wmic
function Translate-UserAccountToSID {
param (
[string]$Server,
[string]$Username
)
try {
$sid = Invoke-Command -ComputerName $Server -ScriptBlock {
param($Username)
$output = cmd.exe /c "wmic useraccount where SID='$Username' get name"
# $sid = $output -replace '\s+', '' # Remove whitespace and newline characters
} -ArgumentList $Username
return $output
} catch {
Write-Host "Error translating user account to SID on $($Server): $_"
return $null
}
}
# Iterate through each server
foreach ($server in $servers) {
Write-Host "Checking 'Logon as batch job' rights on $server"
try {
# Get the security settings using secedit
$securitySettings = Invoke-Command -ComputerName $server -ScriptBlock {
secedit /export /cfg "$env:temp\SecuritySettings.inf"
Get-Content "$env:temp\SecuritySettings.inf" | Out-String
}
# Parse the security settings to get SIDs with "Logon as a service" rights
$logonAsBatchUsernames = $securitySettings | Select-String -Pattern "SeBatchLogonRight\s*=\s*(.*)" | ForEach-Object { $_.Matches.Groups[1].Value -split ',' }
# Convert
$accountNames = $logonAsBatchUsernames | ForEach-Object {
$username = $_
$accountName = Translate-UserAccountToSID -Server $server -Username $username
$accountName
}
# Add result to array
$result = [PSCustomObject]@{
ServerName = $server
UsersWithLogonAsBatchRights = $logonAsBatchUsernames -join ', '
AccountNames = $accountNames -join ', '
}
$results += $result
} catch {
Write-Host "Error checking 'Logon as batch job' rights on $($server): $_"
}
Write-Host ""
}
# Output results
$results | Format-Table -AutoSize
# Optionally, export results to CSV file
#$results | Export-Csv -Path c:\tmp\output\LogonAsBatchResults.csv -NoTypeInformation
#Write-Host "Results exported to LogonAsBatchResults.csv"
ERROR:
NotSpecified: ( , RemoteException
No Instance(s) Available.
+ CategoryInfo : NotSpecified: (No Instance(s) Available.:String) , RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : SERVERNAME
NotSpecified: ( , RemoteException