Mr.Ben Posted April 9, 2014 Posted April 9, 2014 (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 April 9, 2014 by Mr.Ben
jinnantonnixx Posted April 9, 2014 Posted April 9, 2014 I havent' run the code, but inspect the contents of $userdata first to see if you're testing the correct properties. 1
Gatt Posted April 9, 2014 Posted April 9, 2014 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 1
Mr.Ben Posted April 9, 2014 Author Posted April 9, 2014 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?
Iain Posted April 9, 2014 Posted April 9, 2014 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. 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now