Solved

Powershell - Pulling a single users info from a .csv

Posted on 2016-11-03
5
27 Views
Last Modified: 2016-11-06
I am creating some AD and MSOL users from a CSV using powershell. The users get created fine using ‘import’ and then ‘for each’ commands.

----------------------------------------------------

$UserCSV = Import-Csv -Path "C:\Path.csv"
       
    foreach ($User in $UserCSV)            
    {            
        $FirstName = $User.Firstname        
        $LastName = $User.Lastname
        $UserName = $User.Username.ToUpper()
        $Password = $User.Password
               
    # Create User Commands          
    New-ADUser -Name $UserName etc etc
    }

--------------------------------------

But then once the users are created I want to sync them to MSOL, and as this tends to take a while check one of the users exists before moving on to the next lot of commands.

How can I pull just one of the users details from the .csv and set variables based on that single users info?
ie:
$Username = ‘The last user in the .csv username’
$emailaddress = ‘The last user in the .csv email address’
0
Comment
Question by:Pete
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 200 total points
ID: 41872472
Depends on how exactly you want to identify the user from the csv.
You can get the last object in the file like this:
$LastUser = $UserCSV | Select-Object -Last 1

Open in new window

0
 
LVL 13

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 100 total points
ID: 41872475
You can use the .Length property of your array to see how many items are in it (example):
$users = Import-CSV C:\test\user.csv

$howmany = $users.Length

Write-Host $howmany

Open in new window


Then you can use $users[$i] to call the item in the list.  Here is a loop that does just that:
$users = Import-CSV C:\test\user.csv

for ($i = 0; $i -lt $users.Length; $i++)
{
    Write-Host $users[$i].username    
}

Open in new window


(sample csv attached)
user.csv
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 200 total points
ID: 41872483
$UserCSV[0]  will give you the first item in variable.. $UserCSV[-1] will output last item.
Or
$UserCSV = (Import-Csv -Path "C:\Path.csv")[-1]

You can also use Select-Object
$UserCSV | Select -First 1
or
$UserCSV | Select -Last 1

Open in new window

0
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 200 total points
ID: 41872490
Come to think of it: in case you want to select just any old user from the csv, with a bit of comfort, you can do that like this:
$SelectedUser = $UserCsv | Out-GridView -Title 'Select a user from the CSV' -OutputMode Single
#  Do something with the selected user object ...
$SelectedUser

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 200 total points
ID: 41872543
But then once the users are created I want to sync them to MSOL, and as this tends to take a while check one of the users exists before moving on to the next lot of commands.
You can add a If condition to check if user status and then continue based on that.. Example..
New-ADUser -Name $UserName etc etc
#wait for 60 seconds..
Sleep 60
#Get UserPrincipalName from csv if you are using Get-MsolUser to check user status

$UserPrincipalName = $User.UserPrincipalName

#User check here - Use Get-MsolUser or Get-ADUser based on your requirement

$User = Try {Get-MsolUser -UserPrincipalName $UserPrincipalName}Catch{}

If ($User -ne $null){
 # Do some thing
}

Open in new window


Or you can add sleep until the user is created..
Example..
Do {Sleep 10;$User = Try {Get-MsolUser -UserPrincipalName $UserPrincipalName}Catch{}} Until ($User)

Open in new window

0

Featured Post

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this Micro Tutorial viewers will learn how they can get their files copied out from their unbootable system without need to use recovery services. As an example non-bootable Windows 2012R2 installation is used which has boot problems.
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question