Share via


Command to extract pager attribute from Active Directory

Question

Thursday, December 4, 2014 11:47 AM

Hello Powershell Gurus,

I'm struggling to find the command that extracts the pager attribute from all users in Active Directory and exports to a .csv

I've used get-aduser -filter * -properties pager | export-csv C:\temp\pager.csv but it does not extract the pager attribute. I've also tried with a -properties * and it still does not export the pager attribute.

Any ideas guys?

Thank you very much in advance!

cheers

Keir 

All replies (18)

Thursday, December 4, 2014 12:03 PM ✅Answered

I don't think it is selectable with get-aduser until it holds a value - I couldn't retrieve it either until I added a pager number for myself. You can try with get-adobject

Get-ADObject -filter "ObjectClass -eq 'user' -and ObjectClass -ne 'Computer'" -Properties Samaccountname, Pager | Select @{N="samaccountname";E={$_.SamaccountName}},@{N="pager"; E={$_.pager}}

Thursday, December 4, 2014 1:02 PM ✅Answered

The original code works perfectly as written:

get-aduser -filter * -properties pager | 
    Select *,pager |
    Export-Csv C:\temp\pager.csv 

I recommend reducing the select to only what you want like name, samname.

get-aduser -filter * -properties pager | 
    select samaccountname, GivenName, Surname, Enabled,Pager |
    Export-Csv C:\temp\pager.csv 

¯\(ツ)_/¯


Thursday, December 4, 2014 12:08 PM

Try this:

get-aduser -filter * -properties pager | 
Select *,Pager -ErrorAction SilentlyContinue |
export-csv C:\temp\pager.csv 

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 1:10 PM

The main issue is that optional properties will not be created in a CSV if they do not exist int he first record.  Using "Select-Object" forces the value to exist even if it is missing.

The error action is unnecessary.  No errors will be generated by a select except via bad calculated properties.

You could also dor this.

get-aduser -filter * -properties pager|
     select samaccountname, GivenName, Surname, Enabled,
            @{N='Pager';E={if($_.pager){$_.pager}else{''}}}|ft

¯\(ツ)_/¯


Thursday, December 4, 2014 1:18 PM

The original code works perfectly as written:

get-aduser -filter * -properties pager | 
    Select *,pager |
    Export-Csv C:\temp\pager.csv 

I recommend reducing the select to only what you want like name, samname.

get-aduser -filter * -properties pager | 
    select samaccountname, GivenName, Surname, Enabled,Pager |
    Export-Csv C:\temp\pager.csv 

¯\(ツ)_/¯

If you use -Select*,Pager it will generate an error whenever it encounters an object that does have a Pager property, complaining that the Pager property has already been selected (because it was included in *).

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 1:28 PM

The original code works perfectly as written:

get-aduser -filter * -properties pager | 
    Select *,pager |
    Export-Csv C:\temp\pager.csv 

I recommend reducing the select to only what you want like name, samname.

get-aduser -filter * -properties pager | 
    select samaccountname, GivenName, Surname, Enabled,Pager |
    Export-Csv C:\temp\pager.csv 

¯\(ツ)_/¯

If you use -Select*,Pager it will generate an error whenever it encounters an object that does have a Pager property, complaining that the Pager property has already been selected (because it was included in *).

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

Not on my servers.  I have only two users with pagers and I select all with no errors. In a select any misisng field is just set to null.

try this:

Get-Process | select *,Pager

No errors

¯\(ツ)_/¯


Thursday, December 4, 2014 1:32 PM

Now try:

Get-Process | select *,ID

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 3:20 PM

Now try:

Get-Process | select *,ID

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

Hah!  Can't fool me that easily.

That is because it gets selected twice.  ID is already a property.

1..10| select ID
1..10| select ID,ID

select : The property cannot be processed because the property "ID" already exists.
At line:1 char:8
+ 1..10| select ID,ID
+        ~~~~~~~~~~~~

¯\(ツ)_/¯


Thursday, December 4, 2014 3:25 PM

And if the user does have a value in the Pager property,

get-aduser -filter * -properties pager | 
    Select *,pager 

is going to result in 'Pager' getting selected twice.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 3:34 PM

Maybe if the first record contains a pager then it would be redundant hence: @{N='Pager';E={if($_.pager){$_.pager}else{''}}

I guess your method would compensate. 

The errors don't seem to do anything but put red on the screen on the good ones but they still print although thepproperty order gets screwed up.

¯\(ツ)_/¯


Thursday, December 4, 2014 3:51 PM

The property order is going to be determined by the first object through the pipeline, so it produce a different order depending on whether the first one has a Pager value or not.

But the only way around that is to explicitly select all the properties in the order you want them if you're going to add the additional Pager property for the objects that don't have one.

It is a non-terminating error, and the the ErrorAcion setting is just there to suppress the error message. The objects will be created the same with or without it.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 5:49 PM

The objects will be created in a different order except if you add a non-erring calculated property.  This forces the order to be explicit.  You can also use an explicit list and get no errors.  You cannot use a wildcard and get errors or bad things can happen.

¯\(ツ)_/¯


Thursday, December 4, 2014 6:04 PM

What bad things can happen?

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 9:22 PM

Schizophrenic data.

¯\(ツ)_/¯


Thursday, December 4, 2014 9:34 PM

They'll all have the same set of properties, and Powershell doesn't care what order they're in.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 9:44 PM

It does if properties are missing on an object.

With the wildcard the pager shows last on all objects that do not have a pager defined but on the ones where it is designed the pager is the third or fourth item.  If you make assumptions about order it will have an issue.  If you explicitly select the order with no wild cards then the order will be imposed even if the attribute does not exist and it wil not throw an error.

This is not earth shattering but it is useful at times. In most case -ea 0 wil work but keep the exceptions in mind.  That way it won't byte you bee-hind.

¯\(ツ)_/¯


Thursday, December 4, 2014 9:54 PM

I understand.

 But none of the objects will be missing any properties.  Objects that have a Pager property will retain it, and objects that are missing that property will get it added, with a null value. They will all have exactly the same properties, albeit some of them may be ordered differently. 

I also understand this could be an issue if you make assumptions about the order the properties will be in, but I can't think of any scenario where you would do that.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, December 4, 2014 10:28 PM

And you know what they say about "ass u me"

¯\(ツ)_/¯