foreach loop not working in a do while loopPowershell foreach loop with multiple if statementsPowershell Re-Use a hash Table foreach loopset-aduser Homedrive pipe $_.nameUnable to update AD fields with Set-ADUserGuidance on Do While loop until NULL in PSRunning Powershell Script fails on Jenkins ServerNested ForEach statements - Exchange Powershell - bulk remove mailbox calendar permissions -Cannot update user AD account proxyAddresses propertyDisable users from valid list

How do we avoid CI-driven development...?

Shabbat clothing on shabbat chazon

During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?

Dereferencing a pointer in a 'for' loop initializer creates a segmentation fault

Are any jet engines used in combat aircraft water cooled?

Why isn’t SHA-3 in wider use?

Non-OR journals which regularly publish OR research

Why "ch" pronunciation rule doesn't occur for words such as "durch", "manchmal"?

Colors and corresponding numbers

Trying to create a folder with date and time with a space

Why does Intel's Haswell chip allow multiplication to be twice as fast as addition?

Why are the inside diameters of some pipe larger than the stated size?

What does Apple mean by "This may decrease battery life"?

In reversi, can you overwrite two chips in one move?

Acceptable to cut steak before searing?

How can a surrogate pass on genes to a fertilized embryo?

Can a one way NS Ticket be used as an OV-Chipkaart for P+R Parking in Amsterdam?

What are the uses and limitations of Persuasion, Insight, and Deception against other PCs?

Visa National - No Exit Stamp From France on Return to the UK

How do I explain to a team that the project they will work on for six months will certainly be cancelled?

What method to use in a batch apex in order to get authentication token from a remote server?

How many different ways are there to checkmate in the early game?

How do I calculate the difference in lens reach between a superzoom compact and a DSLR zoom lens?

Write an interpreter for *



foreach loop not working in a do while loop


Powershell foreach loop with multiple if statementsPowershell Re-Use a hash Table foreach loopset-aduser Homedrive pipe $_.nameUnable to update AD fields with Set-ADUserGuidance on Do While loop until NULL in PSRunning Powershell Script fails on Jenkins ServerNested ForEach statements - Exchange Powershell - bulk remove mailbox calendar permissions -Cannot update user AD account proxyAddresses propertyDisable users from valid list






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a small bit of code that I have had to run per year level and I wanted to make it run in a do while loop so that there isn't as much code. The idea is that the code will save a var with a string and int
ie.. yr1 yr2 yr3 and so on.
The main issue is that it works if I just run it in a normal for loop so i can't understand why it wont work.



$searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
$searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
$searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
$searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
$searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
$searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
$searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
$searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'

$yr1 = Get-ADUser -Filter * -searchbase $searchbaser -Properties *
$yr2 = Get-ADUser -Filter * -searchbase $searchbase1 -Properties *
$yr3 = Get-ADUser -Filter * -searchbase $searchbase2 -Properties *
$yr4 = Get-ADUser -Filter * -searchbase $searchbase3 -Properties *
$yr5 = Get-ADUser -Filter * -searchbase $searchbase4 -Properties *
$yr6 = Get-ADUser -Filter * -searchbase $searchbase5 -Properties *
$yr7 = Get-ADUser -Filter * -searchbase $searchbase6 -Properties *
$yr8 = Get-ADUser -Filter * -searchbase $searchbases -Properties *

$elem=0
do

$elem+=1
$yr='test'+'$elem'
ForEach ($ADUser in $yr)

$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDirectory=$homeDirectory
Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.sAMAccountname –scriptPath “Student.bat”
if ($homeDirectory)elsemkdir $homeDirectory


while($elem -le 8)









share|improve this question
























  • $yr is everytime assigning test + element number. So why you need foreach then? Anyways, its gonna iterate one time. But your do while will iterate till-le 8

    – Ranadip Dutta
    Mar 27 at 7:50











  • Can you describe what doesn't work? Does it throw an exception? What do you expect vs what actually happens.

    – bunzab
    Mar 27 at 7:55











  • this is the error i get: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. The whole idea is that all the students in the OU's will get their home directory assigned in AD so that when the user logs into the computer they are able to save to the SMB file share. it also adds the studet.bat file to the AD user so that the student setup file will run when they login.

    – Brendan Harris
    Mar 27 at 8:10


















0















I have a small bit of code that I have had to run per year level and I wanted to make it run in a do while loop so that there isn't as much code. The idea is that the code will save a var with a string and int
ie.. yr1 yr2 yr3 and so on.
The main issue is that it works if I just run it in a normal for loop so i can't understand why it wont work.



$searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
$searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
$searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
$searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
$searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
$searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
$searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
$searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'

$yr1 = Get-ADUser -Filter * -searchbase $searchbaser -Properties *
$yr2 = Get-ADUser -Filter * -searchbase $searchbase1 -Properties *
$yr3 = Get-ADUser -Filter * -searchbase $searchbase2 -Properties *
$yr4 = Get-ADUser -Filter * -searchbase $searchbase3 -Properties *
$yr5 = Get-ADUser -Filter * -searchbase $searchbase4 -Properties *
$yr6 = Get-ADUser -Filter * -searchbase $searchbase5 -Properties *
$yr7 = Get-ADUser -Filter * -searchbase $searchbase6 -Properties *
$yr8 = Get-ADUser -Filter * -searchbase $searchbases -Properties *

$elem=0
do

$elem+=1
$yr='test'+'$elem'
ForEach ($ADUser in $yr)

$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDirectory=$homeDirectory
Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.sAMAccountname –scriptPath “Student.bat”
if ($homeDirectory)elsemkdir $homeDirectory


while($elem -le 8)









share|improve this question
























  • $yr is everytime assigning test + element number. So why you need foreach then? Anyways, its gonna iterate one time. But your do while will iterate till-le 8

    – Ranadip Dutta
    Mar 27 at 7:50











  • Can you describe what doesn't work? Does it throw an exception? What do you expect vs what actually happens.

    – bunzab
    Mar 27 at 7:55











  • this is the error i get: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. The whole idea is that all the students in the OU's will get their home directory assigned in AD so that when the user logs into the computer they are able to save to the SMB file share. it also adds the studet.bat file to the AD user so that the student setup file will run when they login.

    – Brendan Harris
    Mar 27 at 8:10














0












0








0








I have a small bit of code that I have had to run per year level and I wanted to make it run in a do while loop so that there isn't as much code. The idea is that the code will save a var with a string and int
ie.. yr1 yr2 yr3 and so on.
The main issue is that it works if I just run it in a normal for loop so i can't understand why it wont work.



$searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
$searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
$searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
$searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
$searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
$searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
$searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
$searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'

$yr1 = Get-ADUser -Filter * -searchbase $searchbaser -Properties *
$yr2 = Get-ADUser -Filter * -searchbase $searchbase1 -Properties *
$yr3 = Get-ADUser -Filter * -searchbase $searchbase2 -Properties *
$yr4 = Get-ADUser -Filter * -searchbase $searchbase3 -Properties *
$yr5 = Get-ADUser -Filter * -searchbase $searchbase4 -Properties *
$yr6 = Get-ADUser -Filter * -searchbase $searchbase5 -Properties *
$yr7 = Get-ADUser -Filter * -searchbase $searchbase6 -Properties *
$yr8 = Get-ADUser -Filter * -searchbase $searchbases -Properties *

$elem=0
do

$elem+=1
$yr='test'+'$elem'
ForEach ($ADUser in $yr)

$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDirectory=$homeDirectory
Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.sAMAccountname –scriptPath “Student.bat”
if ($homeDirectory)elsemkdir $homeDirectory


while($elem -le 8)









share|improve this question














I have a small bit of code that I have had to run per year level and I wanted to make it run in a do while loop so that there isn't as much code. The idea is that the code will save a var with a string and int
ie.. yr1 yr2 yr3 and so on.
The main issue is that it works if I just run it in a normal for loop so i can't understand why it wont work.



$searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
$searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
$searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
$searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
$searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
$searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
$searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
$searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'

$yr1 = Get-ADUser -Filter * -searchbase $searchbaser -Properties *
$yr2 = Get-ADUser -Filter * -searchbase $searchbase1 -Properties *
$yr3 = Get-ADUser -Filter * -searchbase $searchbase2 -Properties *
$yr4 = Get-ADUser -Filter * -searchbase $searchbase3 -Properties *
$yr5 = Get-ADUser -Filter * -searchbase $searchbase4 -Properties *
$yr6 = Get-ADUser -Filter * -searchbase $searchbase5 -Properties *
$yr7 = Get-ADUser -Filter * -searchbase $searchbase6 -Properties *
$yr8 = Get-ADUser -Filter * -searchbase $searchbases -Properties *

$elem=0
do

$elem+=1
$yr='test'+'$elem'
ForEach ($ADUser in $yr)

$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDirectory=$homeDirectory
Set-ADUser -Identity $ADUser.sAMAccountname -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.sAMAccountname –scriptPath “Student.bat”
if ($homeDirectory)elsemkdir $homeDirectory


while($elem -le 8)






powershell powershell-3.0 powershell-4.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 7:46









Brendan HarrisBrendan Harris

193 bronze badges




193 bronze badges















  • $yr is everytime assigning test + element number. So why you need foreach then? Anyways, its gonna iterate one time. But your do while will iterate till-le 8

    – Ranadip Dutta
    Mar 27 at 7:50











  • Can you describe what doesn't work? Does it throw an exception? What do you expect vs what actually happens.

    – bunzab
    Mar 27 at 7:55











  • this is the error i get: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. The whole idea is that all the students in the OU's will get their home directory assigned in AD so that when the user logs into the computer they are able to save to the SMB file share. it also adds the studet.bat file to the AD user so that the student setup file will run when they login.

    – Brendan Harris
    Mar 27 at 8:10


















  • $yr is everytime assigning test + element number. So why you need foreach then? Anyways, its gonna iterate one time. But your do while will iterate till-le 8

    – Ranadip Dutta
    Mar 27 at 7:50











  • Can you describe what doesn't work? Does it throw an exception? What do you expect vs what actually happens.

    – bunzab
    Mar 27 at 7:55











  • this is the error i get: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. The whole idea is that all the students in the OU's will get their home directory assigned in AD so that when the user logs into the computer they are able to save to the SMB file share. it also adds the studet.bat file to the AD user so that the student setup file will run when they login.

    – Brendan Harris
    Mar 27 at 8:10

















$yr is everytime assigning test + element number. So why you need foreach then? Anyways, its gonna iterate one time. But your do while will iterate till-le 8

– Ranadip Dutta
Mar 27 at 7:50





$yr is everytime assigning test + element number. So why you need foreach then? Anyways, its gonna iterate one time. But your do while will iterate till-le 8

– Ranadip Dutta
Mar 27 at 7:50













Can you describe what doesn't work? Does it throw an exception? What do you expect vs what actually happens.

– bunzab
Mar 27 at 7:55





Can you describe what doesn't work? Does it throw an exception? What do you expect vs what actually happens.

– bunzab
Mar 27 at 7:55













this is the error i get: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. The whole idea is that all the students in the OU's will get their home directory assigned in AD so that when the user logs into the computer they are able to save to the SMB file share. it also adds the studet.bat file to the AD user so that the student setup file will run when they login.

– Brendan Harris
Mar 27 at 8:10






this is the error i get: Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. The whole idea is that all the students in the OU's will get their home directory assigned in AD so that when the user logs into the computer they are able to save to the SMB file share. it also adds the studet.bat file to the AD user so that the student setup file will run when they login.

– Brendan Harris
Mar 27 at 8:10













2 Answers
2






active

oldest

votes


















0














I have rewritten your script to a way I find more easy personally.



$searchAreas = @
searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'


foreach($area in $searchAreas.Values)
$yr = Get-ADUser -Filter * -searchbase $area -Properties *
foreach($ADUser in $yr)
$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

try
Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDirectory=$homeDirectory
catch
Write-Host "Failed to set user because $($_.Exception.Message)" -ForegroundColor Red

Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.SamAccountName –scriptPath “Student.bat”
if($homeDirectory)

else
mkdir $homeDirectory





Also check the try catch methods around Set-ADUser. I strongly advise to always add try catch to all your calls (so you will have to add them to all other Set-ADUser cmdlets and to mkdir etc.



Hope this helps






share|improve this answer



























  • Note that instead of a hashtable for the values, you could also create an array! :)

    – Bernard Moeskops
    Mar 27 at 8:33


















0














User Powershell_ISE. If you save the script and add a breakpoint (F9) at Set-ADUser. The script will break. Now select the $ADUser.sAMAccountname and press (F8). You will see the value of the parameter. Now you will understand more easily why it is not working.



It must have something to do with the fact that you set 'test' + '$elem'. Also, single quotes around the variable will make it a string instead of variable. So check this output:



$yr='test'+'$elem'
$yr


Result is:




test$elem




Hope this helps






share|improve this answer






















  • 1





    Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

    – Brendan Harris
    Mar 27 at 8:25














Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372104%2fforeach-loop-not-working-in-a-do-while-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I have rewritten your script to a way I find more easy personally.



$searchAreas = @
searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'


foreach($area in $searchAreas.Values)
$yr = Get-ADUser -Filter * -searchbase $area -Properties *
foreach($ADUser in $yr)
$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

try
Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDirectory=$homeDirectory
catch
Write-Host "Failed to set user because $($_.Exception.Message)" -ForegroundColor Red

Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.SamAccountName –scriptPath “Student.bat”
if($homeDirectory)

else
mkdir $homeDirectory





Also check the try catch methods around Set-ADUser. I strongly advise to always add try catch to all your calls (so you will have to add them to all other Set-ADUser cmdlets and to mkdir etc.



Hope this helps






share|improve this answer



























  • Note that instead of a hashtable for the values, you could also create an array! :)

    – Bernard Moeskops
    Mar 27 at 8:33















0














I have rewritten your script to a way I find more easy personally.



$searchAreas = @
searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'


foreach($area in $searchAreas.Values)
$yr = Get-ADUser -Filter * -searchbase $area -Properties *
foreach($ADUser in $yr)
$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

try
Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDirectory=$homeDirectory
catch
Write-Host "Failed to set user because $($_.Exception.Message)" -ForegroundColor Red

Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.SamAccountName –scriptPath “Student.bat”
if($homeDirectory)

else
mkdir $homeDirectory





Also check the try catch methods around Set-ADUser. I strongly advise to always add try catch to all your calls (so you will have to add them to all other Set-ADUser cmdlets and to mkdir etc.



Hope this helps






share|improve this answer



























  • Note that instead of a hashtable for the values, you could also create an array! :)

    – Bernard Moeskops
    Mar 27 at 8:33













0












0








0







I have rewritten your script to a way I find more easy personally.



$searchAreas = @
searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'


foreach($area in $searchAreas.Values)
$yr = Get-ADUser -Filter * -searchbase $area -Properties *
foreach($ADUser in $yr)
$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

try
Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDirectory=$homeDirectory
catch
Write-Host "Failed to set user because $($_.Exception.Message)" -ForegroundColor Red

Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.SamAccountName –scriptPath “Student.bat”
if($homeDirectory)

else
mkdir $homeDirectory





Also check the try catch methods around Set-ADUser. I strongly advise to always add try catch to all your calls (so you will have to add them to all other Set-ADUser cmdlets and to mkdir etc.



Hope this helps






share|improve this answer















I have rewritten your script to a way I find more easy personally.



$searchAreas = @
searchbase1 = 'OU=test 1,OU=USR,DC=contoso,DC=local'
searchbase2 = 'OU=test 2,OU=USR,DC=contoso,DC=local'
searchbase3 = 'OU=test 3,OU=USR,DC=contoso,DC=local'
searchbase4 = 'OU=test 4,OU=USR,DC=contoso,DC=local'
searchbase5 = 'OU=test 5,OU=USR,DC=contoso,DC=local'
searchbase6 = 'OU=test 6,OU=USR,DC=contoso,DC=local'
searchbaser = 'OU=test r,OU=USR,DC=contoso,DC=local'
searchbases = 'OU=del,OU=USR,DC=contoso,DC=local'


foreach($area in $searchAreas.Values)
$yr = Get-ADUser -Filter * -searchbase $area -Properties *
foreach($ADUser in $yr)
$homeDirectory = "\dc1database$($ADUser.sAMAccountname)"
$homeDrive = "H"

try
Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDirectory=$homeDirectory
catch
Write-Host "Failed to set user because $($_.Exception.Message)" -ForegroundColor Red

Set-ADUser -Identity $ADUser.SamAccountName -Replace @HomeDrive=$homeDrive
Set-ADUser -Identity $ADUser.SamAccountName –scriptPath “Student.bat”
if($homeDirectory)

else
mkdir $homeDirectory





Also check the try catch methods around Set-ADUser. I strongly advise to always add try catch to all your calls (so you will have to add them to all other Set-ADUser cmdlets and to mkdir etc.



Hope this helps







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 9:08

























answered Mar 27 at 8:32









Bernard MoeskopsBernard Moeskops

4503 silver badges8 bronze badges




4503 silver badges8 bronze badges















  • Note that instead of a hashtable for the values, you could also create an array! :)

    – Bernard Moeskops
    Mar 27 at 8:33

















  • Note that instead of a hashtable for the values, you could also create an array! :)

    – Bernard Moeskops
    Mar 27 at 8:33
















Note that instead of a hashtable for the values, you could also create an array! :)

– Bernard Moeskops
Mar 27 at 8:33





Note that instead of a hashtable for the values, you could also create an array! :)

– Bernard Moeskops
Mar 27 at 8:33













0














User Powershell_ISE. If you save the script and add a breakpoint (F9) at Set-ADUser. The script will break. Now select the $ADUser.sAMAccountname and press (F8). You will see the value of the parameter. Now you will understand more easily why it is not working.



It must have something to do with the fact that you set 'test' + '$elem'. Also, single quotes around the variable will make it a string instead of variable. So check this output:



$yr='test'+'$elem'
$yr


Result is:




test$elem




Hope this helps






share|improve this answer






















  • 1





    Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

    – Brendan Harris
    Mar 27 at 8:25
















0














User Powershell_ISE. If you save the script and add a breakpoint (F9) at Set-ADUser. The script will break. Now select the $ADUser.sAMAccountname and press (F8). You will see the value of the parameter. Now you will understand more easily why it is not working.



It must have something to do with the fact that you set 'test' + '$elem'. Also, single quotes around the variable will make it a string instead of variable. So check this output:



$yr='test'+'$elem'
$yr


Result is:




test$elem




Hope this helps






share|improve this answer






















  • 1





    Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

    – Brendan Harris
    Mar 27 at 8:25














0












0








0







User Powershell_ISE. If you save the script and add a breakpoint (F9) at Set-ADUser. The script will break. Now select the $ADUser.sAMAccountname and press (F8). You will see the value of the parameter. Now you will understand more easily why it is not working.



It must have something to do with the fact that you set 'test' + '$elem'. Also, single quotes around the variable will make it a string instead of variable. So check this output:



$yr='test'+'$elem'
$yr


Result is:




test$elem




Hope this helps






share|improve this answer















User Powershell_ISE. If you save the script and add a breakpoint (F9) at Set-ADUser. The script will break. Now select the $ADUser.sAMAccountname and press (F8). You will see the value of the parameter. Now you will understand more easily why it is not working.



It must have something to do with the fact that you set 'test' + '$elem'. Also, single quotes around the variable will make it a string instead of variable. So check this output:



$yr='test'+'$elem'
$yr


Result is:




test$elem




Hope this helps







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 8:47









mklement0

152k25 gold badges272 silver badges315 bronze badges




152k25 gold badges272 silver badges315 bronze badges










answered Mar 27 at 8:22









Bernard MoeskopsBernard Moeskops

4503 silver badges8 bronze badges




4503 silver badges8 bronze badges










  • 1





    Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

    – Brendan Harris
    Mar 27 at 8:25













  • 1





    Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

    – Brendan Harris
    Mar 27 at 8:25








1




1





Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

– Brendan Harris
Mar 27 at 8:25






Thanks, I can see what you mean now. looks like I will need to keep it just in for loops as I have tried without quotes: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property

– Brendan Harris
Mar 27 at 8:25


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372104%2fforeach-loop-not-working-in-a-do-while-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript