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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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
soostibiCommented:
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

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
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

Chris DentPowerShell 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
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
VMware

From novice to tech pro — start learning today.