Check local group membership then add user if not present

What am I doing wrong here? This doesn’t seem to be working and I can’t seem to figure out why. Thanks all.

Eval Code

$user = ‘DomainNameHere\DomainGroupHere’
$LocalGroup = ‘Administrators’
if ((Get-LocalGroupMember -Group $LocalGroup).Name -contains $user)) {
exit 0
} else { Exit 1 }

Remediation Code

$user = ‘DomainNameHere\DomainGroupHere’
$LocalGroup = ‘Administrators’
Add-LocalGroupMember -Group $LocalGroup -Member $user

The Microsoft.PowerShell.LocalAccounts module is not available in 32-bit PowerShell on a 64-bit system.

If you move your code into a scriptblock like this, it will allow you to use that module via Worklet:

# REQUIRES -Version 2.0

#Handle Exit Codes:
trap {  $host.ui.WriteErrorLine($_.Exception); exit 90 }

$scriptblock = {
$runScriptBlock = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock
1 Like