Powershell foreach array example

im not sure why i get a error for this example.  i have a array that contains SIDS (in bold). I want to convert the SIDS to usernames but i get a error in the 1st line. code was snipped for simplicity sake. array is properly defined.

ForEach-Object ($a in $profileregistryarray)
{$objSID = New-Object System.Security.Principal.SecurityIdentifier `
    ($a)
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value}

the example below works fine but its not an array and it doesnt have the foreach option.

{$objSID = New-Object System.Security.Principal.SecurityIdentifier `
    ("S-1-5-21-3652174762-1322114035-3259599120-1000")
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value}
LVL 5
Kylo RenSystem EngineerAsked:
Who is Participating?
 
soostibiConnect With a Mentor Commented:
It is foreach, not foreach-object!


ForEach ($a in $profileregistryarray)
{$objSID = New-Object System.Security.Principal.SecurityIdentifier ($a)
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value}

0
 
Dale HarrisProfessional Services EngineerCommented:
Have you tried the .tostring() of the value?
$objSID = New-Object System.Security.Principal.SecurityIdentifier `
    ($a.tostring())

0
 
Kylo RenSystem EngineerAuthor Commented:
i have not because i assumed the error is with the 1st line

Unexpected token 'in' in expression or statement.

ForEach-Object ($a in $profileregistryarray)


0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

ForEach-Object processes objects passed down a pipeline. It uses $_ to access each element.

e.g.
Get-Process | ForEach-Object { Write-Host $_.Name }

Open in new window

ForEach processes a defined array. It uses a named variable to access each element.

e.g.
ForEach ($Process in (Get-Process)) {
  Write-Host $Process.Name
}

Open in new window

ForEach and ForEach-Object behave differently. ForEach is faster, but the array you pass must be complete in advance (stored in memory). ForEach-Object, because it handles a pipeline, consumes less memory, but is slightly more work.

For large data-sets, ForEach-Object is normally more appropriate because you can avoid committing to memory.

HTH

Chris
0
 
GusGallowsCommented:
The error is stating that it doesn't like a token. I think it is the () around $a. $a should already have the () in it. Basically $a should equal ("S-1-5-21-3652174762-1322114035-3259599120-1000") so putting the extra () around it would actually translate to (("S-1-5-21-3652174762-1322114035-3259599120-1000")).

Try using the following instead:

 
ForEach-Object ($a in $profileregistryarray)
{$objSID = New-Object System.Security.Principal.SecurityIdentifier $a
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value}

Open in new window


If that doesn't work, you may need to change $a to omit the () in it and then use the code the way you have written. You can remove the () by doing the following:
 
 
ForEach-Object ($a in $profileregistryarray)
{
	[string]$a = $a -Replace "(",""
	[string]$a = $a -Replace ")",""
	$objSID = New-Object System.Security.Principal.SecurityIdentifier ($a)
	$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
	$objUser.Value
}

Open in new window

Give one of those a try.
0
 
Chris DentPowerShell DeveloperCommented:
With respect Gus, did you try those? Because the use of ForEach-Object along with the "($a in $profileregistryarray)" there will throw an error.

$a does not have to be in parentheses.

Chris
0
 
Kylo RenSystem EngineerAuthor Commented:
Thanks Guys!

Soostibi was right.  Thanks for the explanation Chris

Enclosing the $a variable in parenthesis did not make a difference.  it worked both ways
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.