Solved

change home drives for .csv users

Posted on 2016-07-25
9
110 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 85

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 85

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 82

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 85

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 85

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

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