csv 在foreach循环中使用多个if语句

jm81lzqq  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(149)

在此脚本中,我尝试创建一个组(如果不存在),并添加组中还没有的用户。
但问题是他只接受了第一个if语句,因为他似乎没有接受循环中的下一个语句。

#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
    }
}}

好了,这是我当前的输出表单@Theo最后的改进
Output
好的,我有一个非常有趣的发现,因为当我查看
$existingMembers = @((Get-UnifiedGroupLinks -Identity $Group.DistinguishedName -LinkType Members).PrimarySMTPAddress)
我会得到一些空白点。当我为一个用户运行脚本时,它工作,但为另一个用户运行时,它不工作,所以我查看了不工作的用户,他没有许可证。所以我进一步测试了它。在我的结论中,现在它工作了。所以当一个acc没有许可证时,它看起来不像是被添加到了该特定组。所以他还抛出了第二个if语句,还包括第二个else语句。
这对你有意义吗?

### script name: Users_Verwijderen                                                                                                                                      ###
### Datum updated: 14-12-2022                                                                                                                                           ###
### Auteur: Wessel Rouw                                                                                                                                                 ###
### Purpose script is to add groups and users in to groups in Azure.    ###
#######################################################################

$teams = import-csv #Here your CSV
foreach($team in $teams) {
$team | Format-Table
$Check = (Get-UnifiedgroupLinks -Identity $team.Identity -LinkType $team.Linktype)
$existingMembers = @((Get-UnifiedGroupLinks -Identity $Group.DistinguishedName -LinkType Members).PrimarySMTPAddress)
    $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 ($existingMembers -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
     }
}

现在这是我在@Theo的帮助下使用的当前脚本(只是,没有许可证的用户会收到他们被添加的消息,即使他们已经在组中)。

inb24sb2

inb24sb21#

好的,谢谢你的帮助。现在它运行得更好了。但是在第二部分我想验证一下,如果一个用户已经存在于azure中,它会显示消息,但是如果不存在,它必须被添加?但是这就是现在的问题。

现在这是我运行脚本的输出VERBOSE:返回预先计算的版本信息:3.0.0详细说明:使用-1字节有效负载的POST VERBOSE:接收到内容类型为application/json的2945字节响应;字符集=utf-8详细信息:返回预先计算的版本信息:3.0.0详细说明:使用-1字节有效负载的POST VERBOSE:接收到内容类型为application/json的2906字节响应; charset=utf-8 Test105已存在!正在创建组链接#下面是电子邮件(链接)详细信息:返回预先计算的版本信息:3.0.0详细说明:使用-1字节有效负载的POST VERBOSE:接收到内容类型为application/json的386字节响应;字符集=utf-8

问题是当一个用户已经存在时,它不会转到表示它已经存在的行,而是跳过这一部分,直接转到else语句。
我想我已经很接近了。
'$teams =导入-csv #CSV到此处{ #$team|格式-表#这是为了调试$Check =(Get-UnifiedgroupLinks -标识$团队.标识-链接类型$团队.链接类型)

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

x7rlezfr

x7rlezfr2#

此处仅处理您的ifelseif问题(可能存在其他问题,但我们无法看到您的输入CSV文件...)

$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
    }

    # get an array of the email addresses of the current group links members
    $existingMembers = @((Get-UnifiedGroupLinks -Identity $Group.DistinguishedName -LinkType Members).PrimarySMTPAddress)
    $inputMembers    = @($team.Links.Split(",") -ne '')
    # compare this array to the array taken from the input csv ($inputMembers)
    # this returns the items found in the csv but not in the $existingMembers array
    $newMembers = @($inputMembers | Where-Object { $existingMembers -notcontains $_ })
    if ($newMembers.Count) {
        Write-Host "Adding members to group $($team.Displayname)"
        Add-UnifiedGroupLinks -Identity $Group.DistinguishedName -LinkType $team.Linktype -Links $newMembers
    }
    else {
        Write-Host "All members from the csv already existed in group $($team.Displayname)"
    }
}

相关问题