Jump to content

Recommended Posts

Posted (edited)

I may be missing something basic here:

 

$Groupmembers= Get-MsolGroupMember -All -GroupObjectID 1ee6b30d-8155-49a4-b9ye-5d542bf64b6b

foreach ($Groupmember in $Groupmembers)
{
$Userdata = Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress
if ($Userdata.isLicenced -eq 'False') 
   {
   Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress|out-file -Append C:\outputfalse.txt
   }
} 

 

I'm trying to create a loop to find unlicensed users that belong to a specific group in office 365 - I've connected to the MSOLService and have all of the Azure modules I need installed.

 

The end result should be that all unlicensed users are written to a file (I'll change this to licence them when I've fixed this problem), however the file isn't being created. If I output 'Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress' to a file (rather than trying to read it into a variable) I'm getting the expected result.

 

Any help appreciated!

Edited by Mr.Ben
Posted

try storing Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress as a variable before outing it to a file

 

Another method I use is an Array

 

$results = @()

foreach ($Groupmember in $Groupmembers)
{
      $Userdata = Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress
      if ($Userdata.isLicenced -eq 'False') 
   {
      $groupmember = Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress
      $resultsItem = New-Objects System.Object
      $resultsItem | Add-Member -type NoteProperty -Name "Email Address" -Value $groupmember
      $results += $resultsItem
   }
}

$results | out-file  C:\outputfalse.txt

  • Thanks 1
Posted

I'm almost there with the original code - a spelling error in (Licensed/Licenced) caused the lack of output.

 


$StaffGroupMembers= Get-MsolGroupMember -All -GroupObjectID 1ee6b30d-8155-49a4-b8fe-5d542bf64b6b

foreach ($StaffGroupMember in $StaffGroupMembers)
{
$Userdata = Get-MsolUser -UserPrincipalName $StaffGroupMember.EmailAddress
if ($Userdata.isLicensed -eq 'False') 
   {
     Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress|out-file -Append C:\outputfalse.txt
   }
}

 

 

Bizarrely, the output is the opposite of what I want - It outputs the users who have 'isLicensed' set to true?

 

Any ideas on what I've done wrong?

Posted

Try $false in your test, rather than 'False'. The -eq operator is a boolean comparison and not a string comparison operator e.g.

 

if ($Userdata.isLicensed -eq $false)

 

Iain.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...