Solved

change home drives for .csv users

Posted on 2016-07-25
9
96 Views
Last Modified: 2016-07-27
Dear Experts,

I have this below powershell code working for me and I am trying to change it to get user details from csv file instead but could not get it working.

(working code)

import-module activedirectory        

 $targetou= "OU=Test,OU=Users,OU=myOU,DC=myOU,DC=mydc,DC=local"
 Get-ADUser -Filter * -SearchBase $targetou | Foreach-Object{
 $sam = $_.SamAccountName
     Set-ADuser -Identity $_ -HomeDrive "Y:" -HomeDirectory "\\server\share\HOME\$sam"
     }
 "

 I am trying to get this working slightly different and get user list from csv file instead and change their home drive path in AD. Could you please help me with below code? as expected, it is not working. Could you please modify this or give another one that you used?

( broken code)
 $UserList = Import-Csv -Path C:\mydata\userlist.csv

 foreach ($User in $UserList) {
 
 $Account = Get-ADUser -LDAPFilter ('(displayname={0})'-f. $User.DisplayName);
  $HomeDirectory = '\\server\share\HOME'+-f $Account.SamAccountName;
    Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory  -HomeDrive -Y;
     }
0
Comment
Question by:kuzum
[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
  • 4
  • 4
9 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41727510
Try it like this:
$UserList = Import-Csv -Path C:\Temp\userlist.csv
ForEach ($User in $UserList) {
	If ($Account = Get-ADUser -LDAPFilter ("(displayname=$($User.DisplayName))")) {
		$HomeDirectory = "\\server\share\HOME\$($Account.SamAccountName)"
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive -Y
	}
}

Open in new window

0
 

Author Comment

by:kuzum
ID: 41728473
Hi Obda

received this error?

Get-ADUser : The search filter cannot be recognized
At line:4 char:27
+     If ($Account = Get-ADUser <<<<  -LDAPFilter ("(displayname=$($User.DisplayName))")) {
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 

____________________________________________________________________________________________________________________________________________________________________________________________________________________
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41728497
Then your csv probably isn't a csv, so $User.DisplayName is $Null.
This will list the name it's currently processing:
$UserList = Import-Csv -Path C:\Temp\userlist.csv
ForEach ($User in $UserList) {
	"Processing '$($User.DisplayName)'" | Write-Host -Fore Yellow
	If ($Account = Get-ADUser -LDAPFilter ("(displayname=$($User.DisplayName))")) {
		$HomeDirectory = "\\server\share\HOME\$($Account.SamAccountName)"
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive -Y
	}
} 

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:kuzum
ID: 41728554
I guess I'm missing something in the code?

Unexpected token 'User' in expression or statement.
At line:6 char:17

Unexpected token '.DisplayName' in expression or statement.
At line:6 char:22

Missing closing '}' in statement block.
At line:6 char:34

Missing closing '}' in statement block.
At line:6 char:34

Unexpected token ')' in expression or statement.
At line:6 char:34
0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 41728698
$UserList = Import-Csv -Path C:\mydata\userlist.csv
 foreach ($User in $UserList) {
  $Account = Get-ADUser -LDAPFilter ('(displayname={0})'-f. $User.DisplayName);
 $HomeDirectory = '\\server\share\HOME'+-f $Account.SamAccountName;
    Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory  -HomeDrive -Y;
     }

Open in new window


Is userlist.csv in fact a valid comma separated value file?
i.e.
colum1,colum2,colum3,DisplayName,anothercolumn,yetanothercolumn
,,,"don.brown",,
,,,"adam.west",,
The very minimum it would be
DisplayName
don.brown
adam.west

Line 1 is the header, lines 2+ are the data.
if you have just a list of names use get-content vice import-csv, and modify your script i.e.
in the get-aduser line replace  $user.DisplayName with just $user
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41728813
Use this, this fixes the error with the home drive (the '-' in front was incorrect).
* Do not change anything below line 3.
* Use a real csv file with a header line and a column "DisplayName".
$HomeRoot = "\\server\share\HOME"
$UserList = Import-Csv -Path C:\Temp\userlist.csv
ForEach ($User in $UserList) {
	If ($Account = Get-ADUser -LDAPFilter "(displayname=$($User.DisplayName))") {
		$HomeDirectory = Join-Path -Path $HomeRoot -ChildPath $Account.SamAccountName
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive Y
	}
}

Open in new window

0
 

Author Comment

by:kuzum
ID: 41729236
thanks for your responses guys.

David, "parse errors detected" did not actually run the code.
user list is as follows;   ( no gabs no display name headers..)

don.brown
adam.west


OBda,  I guess I am doing something wrong if you guys tested your codes? thanks

Get-ADUser : The search filter cannot be recognized
At line:4 char:27
+     If ($Account = Get-ADUser <<<<  -LDAPFilter "(displayname=$($User.DisplayName))") {
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41729242
As I already said in https:#a41728497 and https:#a41728813: your file is not a csv file as expected by Powershell. A csv file has a header to identify the columns; how else is Powershell supposed to know the property names, in this case the "Displayname" that you're using?
So, either you add a line "DisplayName" as the first line in your input file to turn it into a real (single-column) csv,
or you tell Import-Csv the header name(s) to use using the Header argument:
$HomeRoot = "\\server\share\HOME"
$UserList = Import-Csv -Path C:\Temp\userlist.csv -Header DisplayName
ForEach ($User in $UserList) {
	If ($Account = Get-ADUser -LDAPFilter "(displayname=$($User.DisplayName))") {
		$HomeDirectory = Join-Path -Path $HomeRoot -ChildPath $Account.SamAccountName
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive Y
	}
}

Open in new window

Or you just use the flat file you have (note that I changed the file extension to indicate clearly that it's not a csv), which means the loop variable will be a simple string.
$HomeRoot = "\\server\share\HOME"
$UserList = Get-Content -Path C:\Temp\userlist.txt
ForEach ($User in $UserList) {
	If ($Account = Get-ADUser -LDAPFilter "(displayname=$($User))") {
		$HomeDirectory = Join-Path -Path $HomeRoot -ChildPath $Account.SamAccountName
		"Processing $($User) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive Y
	}
}

Open in new window

0
 

Author Comment

by:kuzum
ID: 41729264
David, oBdA

apologies, my fault. I did not read your comment properly.   David added -Header Displayname to your code and did not work. I appreciate your time and I would like to access both of yours solutions if you can corret my fault on yours please?

this worked for me. I will post one  more question please for a similar thing.
$HomeRoot = "\\server\share\HOME"
$UserList = Import-Csv -Path C:\Temp\userlist.csv -Header DisplayName
ForEach ($User in $UserList) {
      If ($Account = Get-ADUser -LDAPFilter "(displayname=$($User.DisplayName))") {
            $HomeDirectory = Join-Path -Path $HomeRoot -ChildPath $Account.SamAccountName
            "Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
            Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive Y
      }
}
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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