none
I need help in my loop please RRS feed

  • Question

  • i'm not sure if this is the right way but i've included the author's name anyway.

    this is not my script. i found it on the Internet while searching for how to list ActiveSync users. i've edited it to suit my needs and was able to get my report. however, my problem now is why it is skipping some users.

    in order to process only users whose ActiveSync is enabled, i've included an IF statement. but i soon found that the IF statement skips some users. although when tested manually, it returns true.

    appreciate any help.

    ### BEGINNING OF SCRIPT
    
    #####
    #
    # Get-ActiveSyncDeviceReport
    # Author: Paul Ponzeka
    # Website: port25guy.com
    # email ponzekap2 at gmail dot com
    #
    ######
        $SMTPServer = "mail.acme.com"    
        $SMTPFrom = "00000@acme.com"
        $SMTPTo = "bbunny@acme.com" 
        $exportpath = "c:\scripts\"
    
    #######
    #
    # HTML Formatting Section
    # Thanks to Paul Cunningham at http://exchangeserverpro.com/
    #
    #######
    #
    # 
    #
    ######
    $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
    $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
    $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
    $style = $style + "TD{border: 1px solid black; padding: 5px; }"
    $style = $style + "</style>"
    
    
    $messageSubject = "ActiveSync Device Report"
    
    $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
    $message.Subject = $messageSubject
    $message.IsBodyHTML = $true
    
    ####  Get Mailbox
    
    $EASDevices = ""
    $AllEASDevices = @()
    
    #$EASDevices = ""| select 'Company','Dept','ID','User','PrimarySMTPAddress','DeviceType','DeviceModel','DeviceOS', 'LastSyncAttemptTime','LastSuccessSync'
    $EASDevices = ""| select 'Company','Dept','ID','User','PrimarySMTPAddress','DeviceType','DeviceModel','DeviceOS','LastSuccessSync'
    #$EasMailboxes = Get-Mailbox -OrganizationalUnit "OU=Users,OU=IT,OU=Headquarter,DC=ACME,DC=com" -ResultSize unlimited
    $EasMailboxes = Get-Mailbox -OrganizationalUnit "OU=Headquarter,DC=ACME,DC=com" -ResultSize unlimited
    foreach ($EASUser in $EasMailboxes) {
    
    $EASDevices.Company = get-aduser $EASUser.alias -Properties company | Select-Object -expand company
    $EASDevices.Dept = get-aduser $EASUser.alias -Properties department | Select-Object -expand department
    $EASDevices.user = $EASUser.name
    $EASDevices.ID = $EASUser.alias
    
    # below IF statement by me
    if ( Get-CASMailbox $EASDevices.ID | Select-Object -ExpandProperty ActiveSyncEnabled ) {
    
      $EASDevices.PrimarySMTPAddress = $EASUser.PrimarySMTPAddress.tostring()
        foreach ($EASUserDevices in Get-MobileDevice -OrganizationalUnit "OU=Headquarter,DC=ACME,DC=com" -Mailbox $EasUser.alias) {
    	$EASDeviceStatistics = $EASUserDevices | Get-MobileDeviceStatistics
        $EASDevices.devicetype = $EASUserDevices.devicetype
        $EASDevices.devicemodel = $EASUserDevices.devicemodel
        $EASDevices.deviceos = $EASUserDevices.deviceos
    #	$EASDevices.lastsyncattempttime = $EASDeviceStatistics.lastsyncattempttime
    	$EASDevices.lastsuccesssync = $EASDeviceStatistics.lastsuccesssync
    #    $AllEASDevices += $EASDevices | select company,dept,ID,user,primarysmtpaddress,devicetype,devicemodel,deviceos,lastsyncattempttime,lastsuccesssync
        $AllEASDevices += $EASDevices | select company,dept,ID,user,primarysmtpaddress,devicetype,devicemodel,deviceos,lastsuccesssync
        }
    
      }
    }
    $AllEASDevices = $AllEASDevices | sort user
    $AllEASDevices
    $AllEASDevices | Export-Csv $exportpath\ActiveSyncReport.csv -NoTypeInformation
    
    ######
    #
    # Send Email Report
    #
    ########
    
    $message.Body = $AllEasDevices | ConvertTo-Html -Head $style
    
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($message)
    
    ##END OF SCRIPT

    Sunday, March 27, 2016 2:02 PM

Answers

  • Start with only this and see if you see all users.

    $EasMailboxes = Get-Mailbox -OrganizationalUnit "OU=Headquarter,DC=ACME,DC=com" -ResultSize unlimited
    
    foreach ($EASUser in $EasMailboxes) {
    	
    	if ((Get-CASMailbox $EASUser.Alias).ActiveSyncEnabled) {
    		$EASUser.Alias
    	}
    }
    


    \_(ツ)_/

    • Marked as answer by Reno Mardo Sunday, March 27, 2016 2:50 PM
    Sunday, March 27, 2016 2:30 PM

All replies

  • This appears to be what you are trying to do:

    if(Get-CASMailbox$EASDevices.ID).ActiveSyncEnabled){



    \_(ツ)_/


    • Edited by jrv Sunday, March 27, 2016 2:18 PM
    Sunday, March 27, 2016 2:17 PM
  • same thing actually. still skipping some users whose ActiveSync IS enabled.
    Sunday, March 27, 2016 2:23 PM
  • Start with only this and see if you see all users.

    $EasMailboxes = Get-Mailbox -OrganizationalUnit "OU=Headquarter,DC=ACME,DC=com" -ResultSize unlimited
    
    foreach ($EASUser in $EasMailboxes) {
    	
    	if ((Get-CASMailbox $EASUser.Alias).ActiveSyncEnabled) {
    		$EASUser.Alias
    	}
    }
    


    \_(ツ)_/

    • Marked as answer by Reno Mardo Sunday, March 27, 2016 2:50 PM
    Sunday, March 27, 2016 2:30 PM
  • got it! the command
    Get-MobileDevice 
    in the for-each statement returns NIL. so i think that even though the user have ActiveSync enabled, he have no devices configured to use ActiveSync.
    Sunday, March 27, 2016 2:41 PM