troubleshooting Question

Powershell script Unexpected Token

Avatar of crash1624
crash1624 asked on
Active DirectoryPowershell
2 Comments1 Solution2814 ViewsLast Modified:
Hello,

I'm running this script on a DC and getting an "unexpected token" error.  The syntax looks correct to me but I'm seeing this error nevertheless.  Thanks in advance for any assistance.

$URL = “LDAP://DC= MYDOMAIN,DC=local”;
$root = New-Object DirectoryServices.DirectoryEntry $URL
$ds = New-Object DirectoryServices.DirectorySearcher
$ds.SearchRoot = $root
$ds.filter = “objectCategory=Person”
$src = $ds.FindAll()
Write-Host $src.Count ” user objects found.`n”
$src | %{
$de = $_.GetDirectoryEntry()
$accessrules = $de.get_ObjectSecurity().GetAccessRules($true, $false,[System.Security.Principal.SecurityIdentifier]) | ?{$_.IdentityReference -eq “S-1-5-32-561¿}
if ((Measure-Object -inputobject $accessrules).Count -eq 0)
{
$ar = New-Object System.DirectoryServices.ActiveDirectoryAccessRule([System.Security.Principal.SecurityIdentifier]“S-1-5-32-561¿, “ReadProperty, WriteProperty”, “Allow”, [guid]“5805bc62-bdc9-4428-a5e2-856a0f4c185e”)
$de.get_ObjectSecurity().AddAccessRule($ar)
$de.CommitChanges()
Write-Host -f yellow (“Added:`t” + $de.properties["sAMAccountName"])
Start-Sleep -m 200
}
else
{
Write-Host -f green (“Unchanged:`t” + $de.properties["sAMAccountName"])
}
}

Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Write-Host
Write-Host "A"
Write-Host "B"
Write-Host "C"


Output from Powershell =

Unexpected token 'S-1-5-32-561?' in expression or statement.
At C:\userattributes.ps1:13 char:129
+ $ar = New-Object System.DirectoryServices.ActiveDirectoryAccessRule([System.S
ecurity.Principal.SecurityIdentifier]"S-1-5-32-561? <<<< , "ReadProperty, Write
Property", "Allow", [guid]"5805bc62-bdc9-4428-a5e2-856a0f4c185e")
    + CategoryInfo          : ParserError: (S-1-5-32-561?:String) [], ParentCo
   ntainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros