简体   繁体   English

Powershell中的Foreach嵌套循环问题

[英]Foreach Nested loops issue in powershell

What I need to do is to compare the Licence attribute associated with each Username from $O365Users with Enabled attribute using the matching Employee ID in $userID (if it exists). 我需要做的是使用$ userID中的匹配Employee ID(如果存在),将与具有Enabled属性的$ O365Users中与每个用户名关联的License属性进行比较。 With the standard nested ForEach (above) We use this script to help manage our local Active Directory and MSOL (Microsoft Online – Office 365) objects. 使用标准的嵌套ForEach(如上所述),我们使用此脚本来帮助管理本地Active Directory和MSOL(Microsoft Online – Office 365)对象。 My question is : I have got an issue related to the foreach loop so same object returns multiple (forever) I want to do it line-by-line for each user 我的问题是:我遇到了一个与foreach循环有关的问题,因此同一对象返回多个(永远),我想为每个用户逐行执行此操作

Import-Module ActiveDirectory
Import-Module MSOnline

$password = ConvertTo-SecureString 'PASSWORD' -AsPlainText -Force
$LiveCred = New-Object System.Management.Automation.PSCredential ("username@domain.com", $password)
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Connect-MsolService -Credential $Livecred

$O365Users = Get-MsolUser -All
ForEach ($O365User in $O365Users)
{
  $userID = Import-CSV "c:\Export\list.csv"
  $ADuser = Get-ADUser -Filter "EmployeeID -eq $($userID.EmployeeID)" -Properties whenCreated, Enabled, SAMAccountName
  If (($ADUser.Enabled -eq $True) -and ($O365User.isLicensed = $true))
    {

     Get-MsolUSer -UserPrincipalName $ADuser.UserPrincipalName
        Set-MsolUserLicense -UserPrincipalName $ADuser.UserPrincipalName -RemoveLicenses "company:ENTERPRISEPACK"
    }
}

CSV file : CSV档案:

EmployeeID
52576
1234
8599

Here you go this should work 在这里,这应该工作

$userID = Import-Csv "c:\export\list.csv"

foreach ($user in $userID){

    $ADuser = Get-ADUser -Filter "EmployeeId -eq $($user.EmployeeID)" -Properties whenCreated, Enabled, SAMAccountName
    $O365User = Get-MsolUser -UserPrincipalName $ADuser.UserPrincipalName

    if(($ADuser.Enabled -eq $true) -and ($O365User.isLicensed -eq $true)){
        Get-MsolUSer -UserPrincipalName $ADuser.UserPrincipalName
        Set-MsolUserLicense -UserPrincipalName $ADuser.UserPrincipalName -RemoveLicenses "company:ENTERPRISEPACK"
    }

}

Regarding the follow-up question on performance (just what i think might increase performance, no warranty and not tested): 关于性能的后续问题(我认为这可能会提高性能,不提供保修且未经测试):

$userID = Import-Csv "c:\export\list.csv"
$adusers = Get-ADUser -Filter * -properties EmployeeID,whenCreated,Enabled,SAMAccountname
$msolusers = Get-MsolUser -All

foreach ($user in $userID){

$ADuser = $adusers | where {$_.EmployeeID -eq $user.EmployeeID}
$O365User = $msolusers | where {$_.UserPrincipalName -eq $ADuser.UserPrincipalName}

if(($ADuser.Enabled -eq $true) -and ($O365User.isLicensed -eq $true)){
    Set-MsolUserLicense -UserPrincipalName $ADuser.UserPrincipalName -RemoveLicenses "company:ENTERPRISEPACK"
}

}

Depending on how many of the AD / MSOL users there are and you have to match this might de- or increase the execution time, you will have to test since i cant. 根据存在的AD / MSOL用户数量,您必须匹配它可能会降低或增加执行时间,因为我无法进行测试。 I also removed the get-msoluser in your if statement since it´s only function is generating (unnecessary?) output. 我还删除了if语句中的get-msoluser,因为它的唯一功能是生成(不必要的)输出。 If there are any problems with my "improvements" let me know and we can see what we can do ;) 如果我的“改进”有任何问题,请告诉我,我们将看到可以做什么;)

Please try: 请试试:

Import-Module ActiveDirectory
Import-Module MSOnline

$password = ConvertTo-SecureString 'PASSWORD' -AsPlainText -Force
$LiveCred = New-Object System.Management.Automation.PSCredential ("username@domain.com", $password)
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Connect-MsolService -Credential $Livecred


$userIDs = Import-CSV "c:\Export\list.csv"
$O365Users = Get-MsolUser -All

ForEach ($O365User in $O365Users)
{
  foreach ($userID in $userIDs) 
    {
    $ADuser = Get-ADUser -Filter "EmployeeID -eq $($userID.EmployeeID)" -Properties whenCreated, Enabled, SAMAccountName,ObjectGUID
    $valuetoconvert=$ADuser.ObjectGUID
    $guid = [GUID]$valuetoconvert
    $bytearray = $guid.tobytearray()
    $ImmutableID = [system.convert]::ToBase64String($bytearray)


  If (($ADUser.Enabled -eq $True) -and ($O365User.isLicensed = $true) -and ($ImmutableID -eq $O365User.ImmutableID ) )
    {
        Get-MsolUSer -UserPrincipalName $ADuser.UserPrincipalName
        Set-MsolUserLicense -UserPrincipalName $ADuser.UserPrincipalName -RemoveLicenses "company:ENTERPRISEPACK"
    }

    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM