0

I'm trying to run the below code and get the results to an HTML email. If I just run it as is, all of the output is on one line, which is a mess. I thought I could save the foreach right to an array, but that's not working. . . Just getting a blank array. If I run it without, I get output to the screen, so I know I'm getting results.

Connect-MsolService -Credential $Credential

$customers = Get-MsolPartnerContract -All
$body = @(foreach ($customer in $customers) {

  if ($customer.defaultdomainname -notlike "*Domain1*" -and $customer.defaultdomainname -notlike "*DomainABC*") {
    Get-MsolUser -All -EnabledFilter EnabledOnly -TenantId $($customer.TenantId) | ? {$_.StrongAuthenticationRequirements.State -eq $null -and $_.isLicensed -eq "TRUE"}  | select UserPrincipalName
  }
})
6
  • 1
    Capturing foreach output like this works. How do you output $body? I guess there's where the error lies. Commented Mar 31, 2021 at 17:56
  • I'm assuming you want to get the UPNs of all Azure AD enabled users with License and without Strong Auth that are not on Domain1 or DomainABC domain/tenant. Is that right? Commented Mar 31, 2021 at 17:56
  • What do you do with $body afterwards? How are you composing the html? Commented Mar 31, 2021 at 17:57
  • Really, just for testing, all I've done so far is a $body to see some results on the screen. Nothing. Tried $body[1] - nothing. Commented Mar 31, 2021 at 17:59
  • What is $body after doing this? Is it an array? If so, an array of what? Commented Mar 31, 2021 at 18:51

1 Answer 1

1

I don't have an instance to play/validate with, but try it this way (use whatever code or other formatting you choose of course):

Connect-MsolService -Credential $Credential

[System.Collections.ArrayList]$body = @()

(Get-MsolPartnerContract -All) | 
ForEach-Object {

  if ($PSItem.defaultdomainname -notlike '*Domain1*' -and 
      $PSItem.defaultdomainname -notlike '*DomainABC*'
  ) 
  {
    $body.Add(
                (
                    Get-MsolUser -All -EnabledFilter EnabledOnly -TenantId $($PSItem.TenantId) | 
                    Where-Object {
                        $PSItem.StrongAuthenticationRequirements.State -eq $null -and 
                        $PSItem.isLicensed -eq 'TRUE'
                    }
                ).UserPrincipalName
    )
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I thought += with an array was frowned upon as it creates a new array every time?
Yeppers it is, but you still see it used in tons of code, see in the PowerShell repo, and I already updated with the other option. So, choices, choices...
@Pat If you only add a few elements to an array it doesn't make a relevant difference. Still I prefer to avoid += which often results in cleaner code.
No worries, glad, it helped. Happy scripting and stay safe out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.