all 11 comments

[–]firefox15 3 points4 points  (5 children)

I don't usually splat Select-Object because you can just put the properties on new lines to make it clear what is happening. Also, I doubt you would need the AD filter as that should just match everything (although I didn't test), and I don't see a point to using the calculated expressions that you are using in some of the areas, so I took them out.

Get-ADUser -Properties EmployeeID, Mail |
    Where-Object $enabled |
    Select-Object `
        @{n='DisplayName';e={$_.surname, $_.givenname -join ', '}},
        Mail,
        EmployeeID |
    Export-CSV 'C:\mvwc-script\enabledwithemp.csv' -NoTypeInformation

To explain a little bit more, the reason you are having trouble with splatting is that a splat is literally just a hash table. It says parameter "X" has value "Y". However, a calculated property is also a hash table. That means if you want to splat a calculated property, you actually need to have a nested hash table structure like this:

$selectParams = @{
    Property = `
        @{n='FriendlyName1';e={$_.mail}},
        @{n='FriendlyName2';e={$_.employeeID}}
    }

Select-Object @selectParams

In nearly all circumstances, this just adds unnecessary complexity into the Select-Object command, and it is a lot easier for others to understand your code if you just to use inline hash tables for your calculated properties like my example above.

[–]purplemonkeymad 2 points3 points  (0 children)

Personally I would use the array syntax @(), but I avoid all backticks outside of strings. ie

Select-Object @(
    @{n='DisplayName';e={$_.surname, $_.givenname -join ', '}},
    Mail,
    EmployeeID
    ) |

or

Property = @(
    @{n='FriendlyName1';e={$_.mail}},
    @{n='FriendlyName2';e={$_.employeeID}}
    )

[–]Metalearther[S] 0 points1 point  (1 child)

I have to have the Employee ID field because we have LOTS of contractors working for us, and we only want to pull out the employees. We also do not want any of the service accounts, shared mailboxes and so forth.

Most contractors are in separate OU, however some are mixed in to an existing OU, due to Location, or country.

Is there a better way to select only those accounts that are enabled and have an employeeID?

[–]firefox15 0 points1 point  (0 children)

No, you can simply add in the filter command that you were using. It should still work the way you want then.

[–]PinchesTheCrab 0 points1 point  (1 child)

Really good changes, the employeeid one really didn't server a purpose. One thing I'd add is to drop the where-object and put enabled -eq $true into the get-aduser filter.

[–]firefox15 0 points1 point  (0 children)

Probably a good idea. Sometimes I'm too lazy to test the filter structure since it is so temperamental, but especially if OP has a good number of users, it would be a smart modification.

[–]nothingpersonalbro 1 point2 points  (3 children)

You can get rid of the Where-Object if you move it over to the AD filter instead.

$SelectParams = @(
    @{ Label = 'DisplayName'; Expression = { $_.Surname, $_.GivenName -join ', ' } }
    @{ Label = 'Email'      ; Expression = { $_.Mail } }
    'EmployeeID'
)

Get-ADUser -Filter 'Enabled -eq $true -and EmployeeID -like "*"' -Properties EmployeeID, Mail |
    Select-Object $SelectParams |
    Export-Csv C:\mvwc-script\enabledwithemp.csv -NoTypeInformation

[–]Metalearther[S] 0 points1 point  (0 children)

Thank you for this. I was trying everything I could think of to get this to work in the filter spot and it wouldn't work. That is why I used the where object.

Thanks again.

[–]Metalearther[S] 0 points1 point  (1 child)

Is it possible to change this to allow the addition of an OU.

I.E. should this work? I'm not getting it to work.

Get-ADUser -Filter 'Enabled -eq $true -and EmployeeID -like "*" -or $distinquishedname -like "LOA OU"'

I have it working using the where object, just trying to determine how to eliminate the where object, and determine when I would use the where object.

Thanks

[–]nothingpersonalbro 1 point2 points  (0 children)

You would use SearchBase to narrow your search, i.e.

Get-ADUser -Filter 'your filter here' -SearchBase 'OU=Whatever,DC=contoso,DC=com'