Powershell script Unexpected Token

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"

Open in new window



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
crash1624Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
@first glance.. In line 10 and 13 you didn't close the double quotes..

?{$_.IdentityReference -eq “S-1-5-32-561}

([System.Security.Principal.SecurityIdentifier]“S-1-5-32-561



It should be

?{$_.IdentityReference -eq "S-1-5-32-561"}

([System.Security.Principal.SecurityIdentifier]"S-1-5-32-561"

Open in new window

Modified code..
$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"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Guys, please use indention for code blocks!
$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"

Open in new window

is so much better to read ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.