header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Skip blank cell when doing a Get-QADUser from .csv
Last Post 19 Jul 2010 10:57 AM by Aaron. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
AaronUser is Offline
New Member
New Member
Posts:3
Avatar

--
16 Jul 2010 02:06 PM
    Hello,

    I am very new to powershell and I am having an issue when trying to use the get Get-QADUser command to Querey  AD for a users full name (DisplayName).  The querey is based off  a list of usernames from an imported .csv file that contains a few blank cell's in the Column where the username should be.  If I manually filter out these blank cell's I am able to get the querey just fine. Is there a way I can have the script just ignore all the blank cell's and move on to the next cell without erroring out? Additionally I would like to have the results of the querey update the FullUserName column with the users full name.  I know powershell can do all this for me, I just need to figure out how to get it done.

    Thank you,

    Aaron

    Imported .CSV that contains usernames and other fields

    GroupName Name AssignedUserName FullUserName
    Test-XenDesktop-Group Test1 testuser1
    Test-XenDesktop-Group Test2 testuser2
    Test-XenDesktop-Group Test3

    Test-XenDesktop-Group Test4

    Test-XenDesktop-Group Test5 testuser3
    Prod-XenDesktop-Group Prod1

    Prod-XenDesktop-Group Prod2 produser1
    Prod-XenDesktop-Group Prod3 produser2
    Prod-XenDesktop-Group Prod4

    Prod-XenDesktop-Group Prod5 produser3

    Here is my code:

    Add-PSSnapin Quest.ActiveRoles.ADManagement

    $Usernames = Import-Csv E:\scripts\Usernames.csv

    $Usernames | foreach-object{
       
        $DisplayName = Get-QADUser -identity $_.AssignedUserName

        $DisplayName | Select-Object DisplayName
    }






    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    16 Jul 2010 05:06 PM

    Use the Where-Object cmdlet:

    Add-PSSnapin Quest.ActiveRoles.ADManagement $Usernames = Import-Csv E:\scripts\Usernames.csv $Usernames | where-object { $_.AssignedUserName } | foreach-object{ $DisplayName = Get-QADUser -identity $_.AssignedUserName $DisplayName | Select-Object DisplayName }
    AaronUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    19 Jul 2010 10:57 AM
    Thanks, that helped out quite a bit and it skipped the empty cell's as intended. However, I did have another question that I hope someone could help me out with.

    Is there a way to have it display (not assigned) under the output for the blank columns? I was looking for a way to do this in-line with the other results so the output would be something like:

    GroupName Name AssignedUserName FullUserName
    Test-XenDesktop-Group Test1 testuser1 User, Test1
    Test-XenDesktop-Group Test2 testuser2 User, Test2
    Test-XenDesktop-Group Test3 (not assigned) (not assigned)
    Test-XenDesktop-Group Test4 (not assigned) (not assigned)
    Test-XenDesktop-Group Test5 testuser3 User, Test3
    Prod-XenDesktop-Group Prod1 (not assigned) (not assigned)
    Prod-XenDesktop-Group Prod2 produser1 User, Prod1
    Prod-XenDesktop-Group Prod3 produser2 User, Prod2
    Prod-XenDesktop-Group Prod4 (not assigned) (not assigned)
    Prod-XenDesktop-Group Prod5 produser3 User, Prod3


    I tried using an if statement but I know my logic is incorrect and way off the mark.

    Code:

    import-csv E:\Scripts\output-XenDesktops-new.csv | where-object { $_.AssignedUserName } | foreach-object {

    $user = $_.AssignedUserName

    $DisplayName = if($user)
    {
    Get-QADUser -identity $user | Select-Object DisplayName
    }
    else
    {
    $_.AssignedUserName | Select-Object @{n='FullUserName';e={"$_ (not assigned)"}}
    }
    $DisplayName
    }
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer