+ Post New Thread
Results 1 to 6 of 6
Enterprise Software Thread, What's wrong with this bit of Powershell? in Technical; I may be missing something basic here: Code: $Groupmembers= Get-MsolGroupMember -All -GroupObjectID 1ee6b30d-8155-49a4-b9ye-5d542bf64b6b foreach ($Groupmember in $Groupmembers) { $Userdata = ...
  1. #1
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    941
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65

    What's wrong with this bit of Powershell?

    I may be missing something basic here:

    Code:
    $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!
    Last edited by Mr.Ben; 9th April 2014 at 11:55 AM.

  2. #2

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,954
    Thank Post
    109
    Thanked 482 Times in 331 Posts
    Blog Entries
    2
    Rep Power
    281
    I havent' run the code, but inspect the contents of $userdata first to see if you're testing the correct properties.

  3. Thanks to jinnantonnixx from:

    Mr.Ben (9th April 2014)

  4. #3

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,658
    Thank Post
    858
    Thanked 646 Times in 429 Posts
    Rep Power
    498
    try storing Get-MsolUser -UserPrincipalName $Groupmember.EmailAddress as a variable before outing it to a file

    Another method I use is an Array

    Code:
    $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

  5. Thanks to Gatt from:

    Mr.Ben (9th April 2014)

  6. #4
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    941
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65
    I'm almost there with the original code - a spelling error in (Licensed/Licenced) caused the lack of output.

    Code:
    $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?

  7. #5
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31
    Try $false in your test, rather than 'False'. The -eq operator is a boolean comparison and not a string comparison operator e.g.

    Code:
    if ($Userdata.isLicensed -eq $false)
    Iain.

  8. Thanks to Iain from:

    Mr.Ben (9th April 2014)

  9. #6
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    941
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65
    Awesome - fixed!

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 10
    Last Post: 19th September 2012, 09:41 AM
  2. What's wrong with this boot script?
    By reggiep in forum Scripts
    Replies: 15
    Last Post: 7th September 2012, 09:00 AM
  3. Can't see what is wrong with this
    By AyatollahPies in forum Scripts
    Replies: 6
    Last Post: 21st July 2010, 01:18 PM
  4. [MS Office - 2010] What's wrong with this picture?
    By AngryTechnician in forum Office Software
    Replies: 7
    Last Post: 5th May 2010, 08:54 AM
  5. What's Wrong With This Printer?
    By enjay in forum Hardware
    Replies: 14
    Last Post: 12th February 2010, 09:41 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •