Get-QADUser

Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


Get-QADUser

This question is answered

Hi Guys.


I have knocked up a quick script as I need to retrieve some info from Active Directory. The issue is that I need to obtain the secretary info. This works however it displays the CN= of the secretary when really I would like to just retrieve the SamAccountName of the Secretary or SN, GivenName.


Also I some how need the extension number of the secretary as well.


Can anyone help? 



Get-QADUser -IncludeAllProperties | Select-Object SamAccountName , Office , Department , GivenName , SN , Title , HomePhone , Mobile , Mail , Secretary | Out-GridView

 

Thanks in advance

Verified Answer
  • If you have PowerShell 3, then replace "New-Object PSObject -Property " with "[Ordered]":

    $Report += [Ordered]


    @{

    If not, add a Select-Object clause before the Export-Csv command with the items in the correct order:

    $Report | Select-Object Account, Office, Department, 'First Name', 'Last Name',
        Title,'Home Phone', 'Cell Phone', eMail, Secretary, 'Secretary Phone' |
            Export-Csv <full-path-to-csv-file> -NoTypeInformation






All Replies
  • Something like this?

    foreach ($user in (Get-QADUser -SizeLimit 0 -IncludeAllProperties)) {
        $Secretary = Get-QADUser $user.Secretary
        $User | Select-Object SamAccountName, Office, Department, GivenName, SN, Title, HomePhone, Mobile, Mail,
            @{Name='Secretary';Expression={"$($Secretary.GivenName) $($Secretary.SN)"}},
            @{Name='Secretary Phone';Expression={$Secretary.telephoneNumber}} | Out-GridView
    }




  • Thanks Art.


    Using | Out-gridview sends the script a bit mad. However removing that makes the script work. However, For the users who do not have a secretary you get an error



    Get-QADUser: Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argument that is not null or empty and try the command again.


    Anyway of surpressing this?



  • Also i need to export-csv which does not quite work and i think its because of the error above.
  • Just noticed that this is not working as designed.

    I just checked my details and the script has given me a secretary. (i wish!)

    Any ideas?
  • $Report = foreach ($user in (Get-QADUser -SizeLimit 0 -IncludeAllProperties)) {
        Try {
            $Secretary = Get-QADUser $user.Secretary -ErrorAction Stop
            $sName = "$($Secretary.GivenName) $($Secretary.SN)"
            $sPhone = $Secretary.telephoneNumber
        }
        Catch {
            $sName = ''
            $sPhone = ''
        }
        $User | Select-Object SamAccountName, Office, Department, GivenName, SN, Title, HomePhone, Mobile, Mail,
            @{Name='Secretary';Expression={$sName}},
            @{Name='Secretary Phone';Expression={$sPhone}}
    $Report | Export-Csv <full-path-to-csv-file>
    $Report | Out-GridView

  • Thanks Art

    So using Get-GridView makes the script open a million GridView windows and then the server falls over as it chews through all 8GB of RAM. What i can see from some of the million windows that are open is that the data is correct, for example people who do not have a secretary still dont and those who do have the correct one.

    If i comment out $Report | Out-GridView and just work with the CSV for now i get

    Export-CSV : cannot bind argument to parameter 'InputObject' because it is null.

    Any ideas? think we are close on this

    Thanks in advance
  • Hmmm. The Export-Csv error says that the $Report object is empty, but if the Out-GridView works, then something is funny. One thing to try would be adding a bit of debugging after the data collection and before processing it.

    "Found $(($Report | Where {-not [string]::IsNullOrEmpty($_.Secretary)}).Count) users (out of $($Report.Count)) with a secretary."

    If the $Report object is faulty, then we'll need to change the coding around a bit. This is somewhat more verbose, but more explicitly builds the report object.

    $Report = @()
    foreach ($user in (Get-QADUser -SizeLimit 0 -IncludeAllProperties)) {
        Try {
            $Secretary = Get-QADUser $user.Secretary -ErrorAction Stop
            $sName = "$($Secretary.GivenName) $($Secretary.SN)"
            $sPhone = $Secretary.telephoneNumber
        }
        Catch {
            $sName = ''
            $sPhone = ''
        }
        $Report += New-Object PSObject -Property @{
            'Account' = $user.SamAccountName
            'Office' = $user.Office
            'Department' = $user.Department
            'First Name' = $user.GivenName
            'Last Name' = $user.SN
            'Title' = $user.Title
            'Home Phone' = $user.HomePhone
            'Cell Phone' = $user.Mobile
            'eMail' = $user.Mail
            'Secretary' = $sName
            'Secretary Phone' = $sPhone
        }
    }

    "Found $(($Report | Where {-not [string]::IsNullOrEmpty($_.Secretary)}).Count) users (out of $($Report.Count)) with a secretary."
    $Report
    | Export-Csv <full-path-to-csv-file> -NoTypeInformation












  • Top Man Art.


    This now displays the correct information. However, the information is all over the place ,


    I have email as the first colum then last name , first name , secretarty phone etc.


    Is there a way to sort this? no big issue if we cant

  • If you have PowerShell 3, then replace "New-Object PSObject -Property " with "[Ordered]":

    $Report += [Ordered]


    @{

    If not, add a Select-Object clause before the Export-Csv command with the items in the correct order:

    $Report | Select-Object Account, Office, Department, 'First Name', 'Last Name',
        Title,'Home Phone', 'Cell Phone', eMail, Secretary, 'Secretary Phone' |
            Export-Csv <full-path-to-csv-file> -NoTypeInformation






  • Thank you Art. You have been most helpful and i have really learnt as we have been going along. Thank you for that.