get-content and foreach loop

%3CLINGO-SUB%20id%3D%22lingo-sub-2533916%22%20slang%3D%22en-US%22%3Eget-content%20and%20foreach%20loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2533916%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20set%26nbsp%3B%20permissions%26nbsp%3B%20to%20all%20GPOs%20names%20in%20the%20file.%20I%20got%20the%20first%20PS%20script%20working%20by%20using%20-eq%26nbsp%3B%20to%20name%20of%20a%20GPO%20in%20the%20file%2C%20but%20i%20need%20to%20set%20permissions%20to%20all%20GPOs%20in%20the%20file%20.%3C%2FP%3E%3CP%3EI%20cant%20seems%20to%20figure%20it%20out%26nbsp%3B%20.%20what%20am%20i%20doing%20wrong%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%20for%20your%20help!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EFirst%20script%26nbsp%3B%20works%20just%20for%20one%20GPO%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24gpos%20%3DGet-Content%20%22D%3A%5Cgpolist1.txt%22%0A%0A%24gpos%20%7C%20foreach%20%7B%0A%0Aif%20(%24_%20-eq%20%22test%22)%20%7B%20Set-GPPermission%20-PermissionLevel%20GpoEditDeleteModifySecurity%20-TargetName%20%22GPOAdmins%22%20-TargetType%20Group%20%24_%20%7D%0A%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ENot%20working%20%2C%20need%20to%20set%20permissions%26nbsp%3B%26nbsp%3Bfor%20all%20GPOs%20in%20the%20file%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24gpos%20%3D%20get-content%20%22d%3A%5Cgpolist1.txt%22%0AWrite-Output%20%24gpos%0A%0Aforeach%20(%24gpo%20in%20%24gpos)%20%7B%0A%23call%20displayname%0Aif%20(%24_%20-eq%20%24gpos)%20%7BSet-GPPermission%20-PermissionLevel%20GpoEditDeleteModifySecurity%20-TargetName%20%22GPOAdmins%22%20-TargetType%20Group%20%24_%20%7D%0A%0Aelse%20%7B%20write-output%20%22no%20result%22%20%7D%0A%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2533916%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPoweShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2535262%22%20slang%3D%22en-US%22%3ERe%3A%20get-content%20and%20foreach%20loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2535262%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F223343%22%20target%3D%22_blank%22%3E%40DarrenRD%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%3C%2FP%3E%3CP%3ESurely%20this%20won't%20be%20matching%2C%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20are%20comparing%20a%20single%20item%20to%20an%20array%20of%20items%20with%20will%20never%20match.%3C%2FP%3E%3CP%3EAlso%2C%20please%20tell%20me%20more%20about%20this%20line%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Eforeach%20(%24gpo%20in%20%24gpos)%20%7B%0A%0Aif%20(%24_%20-eq%20%24gpos)%20%7BSet-GPPermission%20...%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EYou%20are%20checking%20each%20item%20from%20a%20text%20file%20you%20have%20and%20checking%20if%20each%20line%20(Item%3D%24GPO)%20of%20the%20text%20is%20part%20of%20the%20%24gpos.%3C%2FP%3E%3CP%3EYes%2C%20sure%20it's%20all%20part%20you%20get%20the%20data%20from%20there.%3C%2FP%3E%3CP%3EI%20guess%20you%20need%20to%20check%20if%20each%20item%20%24gpo%20name%20is%20exist%20between%20all%20other%20domain%20GPO.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2535494%22%20slang%3D%22en-US%22%3ERe%3A%20get-content%20and%20foreach%20loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2535494%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1099457%22%20target%3D%22_blank%22%3E%40psophos%3C%2FA%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3B%20TY%20you%20both!%20.%20your%20comments%20got%20me%20thinking%26nbsp%3B%20..%20since%20my%20file%20may%20also%20contain%20non-exist%20GPOs%20in%20the%20domain%20i%20decided%20to%20use%20the%20Try%20and%20Catch.%20and%20seem%26nbsp%3B%20to%20be%20the%20only%20way%20i%20got%20it%20working!%26nbsp%3B%20%26nbsp%3Bsee%20the%20screenshot%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22DarrenRD_0-1625972489628.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F294829i16BE6D0D01D9871C%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22DarrenRD_0-1625972489628.png%22%20alt%3D%22DarrenRD_0-1625972489628.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi

I am trying to set  permissions  to all GPOs names in the file. I got the first PS script working by using -eq  to name of a GPO in the file, but i need to set permissions to all GPOs in the file .

I cant seems to figure it out  . what am i doing wrong? 

 

Thanks in advance for your help! 

 

First script  works just for one GPO

$gpos =Get-Content "D:\gpolist1.txt"

$gpos | foreach {

if ($_ -eq "test") { Set-GPPermission -PermissionLevel GpoEditDeleteModifySecurity -TargetName "GPOAdmins" -TargetType Group $_ }

}

 

Not working , need to set permissions  for all GPOs in the file

$gpos = get-content "d:\gpolist1.txt"
Write-Output $gpos

foreach ($gpo in $gpos) {
#call displayname
if ($_ -eq $gpos) {Set-GPPermission -PermissionLevel GpoEditDeleteModifySecurity -TargetName "GPOAdmins" -TargetType Group $_ }

else { write-output "no result" }

}
3 Replies

@DarrenRD 

Hi

Surely this won't be matching, 

You are comparing a single item to an array of items with will never match.

Also, please tell me more about this line

foreach ($gpo in $gpos) {

if ($_ -eq $gpos) {Set-GPPermission ...

You are checking each item from a text file you have and checking if each line (Item=$GPO) of the text is part of the $gpos.

Yes, sure it's all part you get the data from there.

I guess you need to check if each item $gpo name is exist between all other domain GPO.

 

$_ references the current item in the pipeline but not in a foreach loop.

Inside your foreach loop $gpo references each item loaded from the file.
So your if statement and Set-GPPermission should reference $gpo and not $_

@psophos @farismalaeb  TY you both! . your comments got me thinking  .. since my file may also contain non-exist GPOs in the domain i decided to use the Try and Catch. and seem  to be the only way i got it working!   see the screenshot 

 

DarrenRD_0-1625972489628.png