Scripting Question with AD and Exchange

Hey guys, I'm hoping someone can help me out here. This is my issue:

I've got a script that I run to pull all ActiveSync devices we have running, and give me the Display Name, Email, Device Type, Device Model, Device OS, and Last Sync of every device we have in AS. I run this usually to work with security enforced iOS updates. That script is here: 

## start
#### define parameters

param(
    [Parameter(Mandatory = $true)]
    [string] $exportpath = 
	)

#### connect to exchange and define scope

##### define new session and import it
$s = New-PSSession -configurationName microsoft.exchange -connectionuri http://exchangeserver/powershell
import-pssession $s
##### set scope to entire forest to pull other domain
Set-AdServerSettings -ViewEntireForest $True
	
####  get command

$EASDevices = 
$AllEASDevices = @()

$EASDevices = | select User,Email,Type,Model,OS,Sync
$EasMailboxes = Get-Mailbox -resultsize unlimited
foreach ($EASUser in $EasMailboxes) {
$EASDevices.User = $EASUser.displayname
$EASDevices.Email = $EASUser.primarysmtpaddress.tostring()
    foreach ($EASUserDevices in Get-MobileDeviceStatistics -Mailbox $EasUser.alias) {
    $EASDeviceStatistics = $EASUserDevices | Get-MobileDeviceStatistics
    $EASDevices.Type = $EASUserDevices.devicetype
    $EASDevices.Model = $EASUserDevices.devicemodel
    $EASDevices.OS = $EASUserDevices.deviceos
	$EASDevices.Sync = $EASDeviceStatistics.lastsuccesssync
    $AllEASDevices += $EASDevices | select User,Email,Type,Model,OS,Sync
    }
    }
$AllEASDevices = $AllEASDevices | sort Type
$AllEASDevices
$AllEASDevices | Export-Csv $exportpath\ActiveSyncReport.csv

##end


Now, I'm wanting to add another tab to my script for the users department name, and job title, to assist with a migration that we're in the middle of working on. The only place I know that that is accurately stored is AD, so I've got this line that I can use to pull that information on a one-by-one basis:

dsquery * domainroot -filter "(displayname=lastname, firstname)" -attr extensionAttribute2, title

However, I've got 2 issues now, the first is that I don't really know how to automate, and combine that line, into my main script, in such a way that it will just add another column or two to the right with the extensionAttribute2 and Title information.

The other problem, is that I'm not sure that this is the best way to get this information, I was hoping for a dsquery/dsget solution, but our AD information is organized in such a way that those don't seem to work the way I would have expected them to.

Does anyone else have any ideas or solutions for this issue? Or hae you ever run into an issue like this?


July 8th, 2015 4:51pm

Hi,
 
Dsquery is the right command you can use to retrieve information from Active Directory. But I would suggest you use sAMAccountName as the search filter instead of the displayname, which can uniquely identify the user in the Active Directory.
 
Also, for scripting related issue, I would suggest you post in our dedicated script forum - The Official Scripting Guys Forum. The community members and support professionals there are more familiar with Scripting and can help you in a more efficient way:
 
https://social.technet.microsoft.com/Forums/scriptcenter/en-us/home?forum=ITCG
 
The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.
 

Regards,

Eth

Free Windows Admin Tool Kit Click here and download it now
July 9th, 2015 1:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics