Forum Discussion

mfranhind115's avatar
mfranhind115
Brass Contributor
Jul 21, 2022

how to "parameterize" a for loop cycle

Hi all,

weird question it is.

 

I would like to change the "for each" loop depending on a value got on input.

 

This is what I'm trying:

 

 

$currdomain = Read-Host "Please enter the name of the domain (i.e.: mydomain.com). Leave it empty for ALL domains"

if (!$currdomain) {

  #IF the input value is null, I would cycle on all the domains:
  foreach ($user in Get-MsolUser -All | Sort-Object UserPrincipalName) {

} else {

  #otherwise only on the domain got as input
  foreach ($user in Get-MsolUser -DomainName $currdomain | Sort-Object UserPrincipalName) {

     #HERE the body of the cycle

}

 

 

any chances to do this?

 

thanks

mf

 

Resources