Solved

change home drives for .csv users

Posted on 2016-07-25
9
69 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 83

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 83

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
 

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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 78

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 83

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 83

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now