Hello All, and thanks if advance for any help.
I am working on this script to import users and need some help to figure out the remaining hurdles.
I will take one hurdle at a time.
In the script I am trying set the HomeDirectory so that the home folder name will match the users assigned username as the script is run and folder created.
Currently I just have %username% as a place holder till the needed changes are in place.
pre
$users = Import-Csv -Delimiter “,” -Path “.\importtest2.csv”
$pass = ConvertTo-SecureString -String “P@ssw0rd” -AsPlainText -Force
$ou = “OU=aaa,OU=bbb,DC=ccc,DC=windhamsd,DC=org”
foreach ($user in $users)
{
#define a New-ADUser
$hash = @{
Name = “$($user.firstname) $($user.middlename) $($user.lastname)”
Displayname = “$($user.firstname) $($user.middlename) $($user.lastname)”
Path = $ou
Surname = $user.lastname
GivenName = $user.firstname
Initials = $user.middlename
Samaccountname = “$($user.firstname[0])$($user.middlename[0])$($user.lastname)”
UserPrincipalName = “$($user.firstname[0])$($user.middlename[0])$($user.lastname)@windhamsd.org”
Email = $user.emailaddr
Office = $user.schoolid
Description = $user.description
Homedrive = “H:”
HomeDirectory = (‘\servername.ccc.windhamsd.org\sharedfolder%username%’)
AccountPassword = $pass
Enabled = $True
ChangePasswordAtLogon = $True
}
New-ADUser @hash -PassThru
}
/pre
Sean,
PowerShell won’t resolve %username% for you but you can use the SamAccountName property if you add the HomeDirectory property after the Hashtable has been defined.
Example:
$hash = @{
Name = "$($user.firstname) $($user.middlename) $($user.lastname)"
Displayname = "$($user.firstname) $($user.middlename) $($user.lastname)"
Path = $ou
Surname = $user.lastname
GivenName = $user.firstname
Initials = $user.middlename
SamAccountName = "$($user.firstname[0])$($user.middlename[0])$($user.lastname)"
UserPrincipalName = "$($user.firstname[0])$($user.middlename[0])$($user.lastname)@windhamsd.org"
Email = $user.emailaddr
Office = $user.schoolid
Description = $user.description
Homedrive = "H:"
AccountPassword = $pass
Enabled = $True
ChangePasswordAtLogon = $True
}
$hash.HomeDirectory = ('\\servername.ccc.windhamsd.org\sharedfolder\{0}' -f $hash.SamAccountName)
Hello Daniel and Thank You for the information.
The %username% entry I had in the script was meant as a placeholder to remind me that I needed to get that option functioning.
I altered the script as you wrote and the test accounts show the proper path to the user home folder.
What does the {0} do in the line you provided?
Unlike when a user is manually added, the home directory is not created on the share.
I am researching now to see if there is some more code that needs to be included so that the user home folder is actually created.
To create the home directory I placed
“MkDir $hash.HomeDirectory”
under the line that Daniel informed me about.
The created home directories inherited the proper permissions I had set for the location.
Would anyone be able to point me at information for review that would enable the script to assign the users being imported to a Security Group in nested OUs?
I believe in some form I need to get the following line into the script
“Add-ADGroupMember -Identity (Samaccount of user) -Members (Samaccount of Security Group)”
Would anyone be able to point me in the correct direction?
Thanks in advance.
Hi!
This should work:
Add-ADGroupMember -Identity "nameOfGroup" -Members "nameOfUser"
And here are some information about Add-ADGroupMember:
[url]http://technet.microsoft.com/en-us/library/ee617210.aspx[/url]
Hello Alexander, and Thank You for the information.
Here is the script I put together with some notes at the top.
Perhaps others will find it useful and I am sure be able to improve on it.
pre
#This script will import users from a .csv
#create AD user accounts in the specified OU
#create the username, first initial, middle initial if present, and last name
#populate the user email field if present in the .csv
#the profile tab will set the home folder to “H”
#the homedirectory will assign to the specified path and append the username as the final location
#the user home directory will be created
#the user account is assigned to a security group
$users = Import-Csv -Delimiter “,” -Path “.\importtest2.csv”
$pass = ConvertTo-SecureString -String “P@ssw0rd” -AsPlainText -Force
$ou = “OU=aaa,OU=bbb,DC=ccc,DC=windhamsd,DC=org”
$secgroup = “SecurityGroupName”
foreach ($user in $users)
{
#define a New-ADUser
$hash = @{
Name = “$($user.firstname) $($user.middlename) $($user.lastname)”
Displayname = “$($user.firstname) $($user.middlename) $($user.lastname)”
Path = $ou
Surname = $user.lastname
GivenName = $user.firstname
Initials = $user.middlename
Samaccountname = “$($user.firstname[0])$($user.middlename[0])$($user.lastname)”
UserPrincipalName = “$($user.firstname[0])$($user.middlename[0])$($user.lastname)@windhamsd.org”
Email = $user.emailaddr
Office = $user.schoolid
Description = $user.description
Homedrive = “H:”
AccountPassword = $pass
Enabled = $True
ChangePasswordAtLogon = $True
}
HomeDirectory = (‘\servername.ccc.windhamsd.org\sharedfolder{0}’ -f $hash.Samaccountname)
MkDir $hash.HomeDirectory
New-ADUser @hash -PassThru
Add-ADGroupMember -Identity $secgroup -Members $hash.Samaccountname
}
/pre
Sean,
-f is the PowerShell format operator. {0} defines the first value to be inserted into the string. In your case the Samaccountname to complete the home directory path.
Please try below simple example:
$name = 'Sean'
$day = 'Friday'
'Hello {0}! Are you available next {1} at 12pm?' -f $name, $day
The format operator is based on the .NET format method. See more examples here: http://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx#Format_Brief
Best,
Daniel