Avatar of crash1624
crash1624

asked on 

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
PowershellActive Directory

Avatar of undefined
Last Comment
Qlemo
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Qlemo
Qlemo
Flag of Germany image

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 ...
Active Directory
Active Directory

Active Directory (AD) is a Microsoft brand for identity-related capabilities. In the on-premises world, Windows Server AD provides a set of identity capabilities and services, and is hugely popular (88% of Fortune 1000 and 95% of enterprises use AD). This topic includes all things Active Directory including DNS, Group Policy, DFS, troubleshooting, ADFS, and all other topics under the Microsoft AD and identity umbrella.

86K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo