Home > Software engineering >  Using multiple if statemens in foreach loop
Using multiple if statemens in foreach loop

Time:12-13

In this script, I am trying to make a group (if not exist) and add users thats are not already in the group.

But the problem is he only takes the first if statement I think, because it don't seems like he is taking the next statements in the loop.

#Tweede test met if
$teams = #Here comes the csv file.
Foreach($team in $teams)
{
$Test = (Get-UnifiedGroup $team.DisplayName)
    if  (Get-UnifiedGroup $team.DisplayName)
    {
        Write-Host -ForegroundColor Green "$($team.Displayname) already exists!"
     }
     elseif ($Test -eq "false")
     {
        $Group = New-UnifiedGroup -DisplayName $team.DisplayName -Alias $team.Alias -AccessType $team.AccessType
        }
   foreach($Member in $Members)
        {
    
   elseif (get-UnifiedgroepLinks $team.Links)
    {
        write-host -ForegroundColor Green "$($team.Links) already exists!"
    }
        else
    {
        Add-UnifiedGroupLinks -Identity $team.Identity -LinkType $team.Linktype -Links $team.Links
    }
}}

CodePudding user response:

Oke, so thanks already for the help. Now it works better. But in the second part I want to verify that if a user already exists in azure it displays the message, but if don't it has to be added? But this is now the problem.

This is now the output of my running script VERBOSE: Returning precomputed version info: 3.0.0 VERBOSE: POST with -1-byte payload VERBOSE: received 2945-byte response of content type application/json;charset=utf-8 VERBOSE: Returning precomputed version info: 3.0.0 VERBOSE: POST with -1-byte payload VERBOSE: received 2906-byte response of content type application/json;charset=utf-8 Test105 already exists! Creating group Links #Here stand the email (Links) VERBOSE: Returning precomputed version info: 3.0.0 VERBOSE: POST with -1-byte payload VERBOSE: received 386-byte response of content type application/json;charset=utf-8

The problem is when a user already exist it does not goes to the line that says that it already exits but instead it skips this part i think and goed directy to the else statement.

I think I am pretty close now.

`$teams = import-csv #CSV here { #$team | Format-Table #This was voor debugging $Check = (Get-UnifiedgroupLinks -Identity $team.Identity -LinkType $team.Linktype)

$Group = (Get-UnifiedGroup $team.DisplayName)
if  ($Group) 
{
    Write-Host "$($team.Displayname) already exists!" -ForegroundColor Green
}
else 
{
    Write-Host "Creating group $($team.Displayname)"
    $Group = New-UnifiedGroup -DisplayName $team.DisplayName -Alias $team.Alias -AccessType $team.AccessType
}
 if ($Check -contains $team.Links)
     {
    Write-Host "$($team.Links) already exists!" -ForegroundColor Green
     }
 else 
 {
    Write-Host "Creating group Links $($team.Links)"
    Add-UnifiedGroupLinks -Identity $team.Identity -LinkType $team.Linktype -Links $team.Links
 }

}`

These are the columns of my csv.

CodePudding user response:

Only dealing with your if and elseif problem here (there may be other problems but we can't see your input CSV file..)

$teams = Import-Csv -Path 'X:\Somewhere\teams.csv'  # import your teams CSV
foreach($team in $teams) {
    $Group = (Get-UnifiedGroup $team.DisplayName)
    if  ($Group) {
        Write-Host "$($team.Displayname) already exists!" -ForegroundColor Green
    }
    else {
        Write-Host "Creating group $($team.Displayname)"
        $Group = New-UnifiedGroup -DisplayName $team.DisplayName -Alias $team.Alias -AccessType $team.AccessType
    }
    if (Get-UnifiedgroupLinks $Group.DistinguishedName) {
        Write-Host "$($team.Links) already exists!" -ForegroundColor Green
    }
    else {
        Write-Host "Creating group Links $($team.Displayname)"
        Add-UnifiedGroupLinks -Identity $Group.DistinguishedName -LinkType $team.Linktype -Links $team.Links.Split(",")
    }
}

P.S. In your code you suddenly start a loop foreach($Member in $Members), but it is not clear where variable $Members comes from..

  • Related