Solved

change home drives for .csv users

Posted on 2016-07-25
9
85 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 80

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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