This is a script I'm writing to use an existing user as a "model" user account in creating a new user. It creates the user account but immediately fails to be able to perform a get-qaduser action on the newly created account - thus all the other pieces of my script below the account creation fails.
Interestingly, if I type each of these commands in this script line by line into the powershell interface it works perfectly. It's only when I run this as a .ps1 script that it fails.
I've replaced the names of my corporate network with DOMAIN for security purposes. I've also added a note on where the script fails marked with #>>>>>>>
# =================================================== # # This PowerShell script creates a new user by # copying the pertinent fields from another # "model" user. It then mailbox-enables the new # account. # # Written by: David Smith # Last modified: 12/07/2007 # Notes: # # ===================================================
### Setup the log file function writetolog([string]$stringtowrite="*") { $datetime = (Get-Date).datetime Add-Content $logfile "$datetime : $stringtowrite" } if (Test-Path "copyuser.log") {Write-Host "Log file exists.";$logfile = "copyuser.log"} Else {Write-Host "Creating log file copyuser.log";$logfile = New-Item "copyuser.log" -Type file} writetolog "===== Beginning copyuser.ps1 ====="
### Add the Quest ActiveRoles ADManagement Snap-in if it isn't already Write-Host "Checking ActiveRoles Snapin" writetolog "Checking ActiveRoles Snapin" if (Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) {Write-Host "Added ActiveRoles Snapin";writetolog "Added ActiveRoles Snapin"} Else {Write-Host "Snapin present.";writetolog "Snapin present."} ### Set the variables $newFname = Read-Host -prompt "Enter NEW USER's First Name" # The new user's First name $newLname = Read-Host -prompt "Enter NEW USER's Last Name" # The new user's Last name $newSAMAccount = Read-Host -prompt "Enter NEW USER's Username" # The new user's requested username $newpassword = Read-Host -prompt "Enter NEW USER's Password" # The new user's requested password $modelSAMAccount = Read-Host -prompt "Enter MODEL USER's UserID" # The user id of the existing user after which to model this new user
$newFullname = "$newlname, $newfname" $modelLogonname = "DOMAIN\$modelSAMAccount" $newLogonname = "DOMAIN\$newSAMAccount" $myEmailAddr = (gc env:username) + "@DOMAIN.com" $fileserver = "SDALFILE2" writetolog "First name: $newFname" writetolog "Last name: $newLname" writetolog "SAMAccount: $newSAMAccount" writetolog "Password: $newpassword" writetolog "Model SAMAccount: $modelSAMAccount" writetolog "Model Account: $modelLogonname" writetolog "New Logon Name: $newlogonname" ### Grab the user object to model after
Write-Host "Getting the model user object." $objModeluser = Get-QADUser -Service $modelLogonname writetolog "objModeluser = $objModeluser"
### Create and enable the new user and set the password Write-Host "Creating the new user object" $objNewuser = New-QADUser -ParentContainer $objModeluser.ParentContainer -Name $newFullname -FirstName $newFname -LastName $newLname -SamAccountName $newSAMAccount -Description $objModeluser.Description -UserPrincipalName "$newSAMAccount@DOMAIN.us" -DisplayName $newFullName | enable-qaduser Write-Host $objNewuser writetolog "$objNewuser.LogonName created"
### Wait for AD to catch up Write-Host "Waiting for Active Directory..." Start-Sleep -Seconds 5 #$objNewuser = '' $objNewuser = Get-QADUser $newLogonname #set-qaduser $newLogonname -UserPassword $newpassword
### Join the new user to the same groups and report on which ones failed
Write-Host "Adding to groups" $objModeluser.MemberOf | ForEach-Object {Add-QADGroupMember $_ $newLogonname} if (Test-Path "SharedGroups.txt") {Write-Host "SharedGroups.txt exists. Removing." writetolog "SharedGroups.txt file exists. Removing." Remove-Item "SharedGroups.txt" } Write-Host "Creating SharedGroups.txt" writetolog "Creating SharedGroups.txt" $SharedFile = New-Item "SharedGroups.txt" -Type file if (Test-Path "MissingGroups.txt") {Write-Host "MissingGroups.txt exists. Removing." writetolog "MissingGroups.txt file exists. Removing." Remove-Item "MissingGroups.txt" } Write-Host "Creating MissingGroups.txt" writetolog "Creating MissingGroups.txt" $MissingFile = New-Item "MissingGroups.txt" -Type file $modelGroups = $objModeluser.MemberOf | Get-QADGroup | ForEach-Object {$_.name} $newGroups = $objNewuser.MemberOf | Get-QADGroup | ForEach-Object {$_.name} Foreach ($Item in $modelGroups) {If ($newGroups -contains $Item) {Add-Content $SharedFile $Item writetolog "Added to $Item" } Else {Add-Content $MissingFile $Item writetolog "Could not add to $Item" } }
### Begin creating the shared network folder # Create the folder Write-Host "Creating home folder" $homefolder = New-Item -Path "\\$fileserver\Users$" -Name $newSAMAccount -type directory writetolog "Created $homefolder" # Set the ACL permissions # see http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1204480&SiteID=1 # this explains why we need to set three rules for one user Write-Host "Setting ACL on the home folder." $AccessRule1 = New-Object System.Security.AccessControl.FileSystemAccessRule($newSAMAccount,"Modify",2,2,"Allow") $AccessRule2 = New-Object System.Security.AccessControl.FileSystemAccessRule($newSAMAccount,"Modify",1,2,"Allow") $AccessRule3 = New-Object System.Security.AccessControl.FileSystemAccessRule($newSAMAccount,"Modify","Allow") $acl = Get-Acl $homefolder $acl.AddAccessRule($AccessRule1) $acl.AddAccessRule($AccessRule2) $acl.AddAccessRule($AccessRule3) Set-Acl $homefolder $acl -ErrorAction Stop writetolog "Set $acl" #Create the Share if ($fileserver) { $Win32ShareClass = [wmiclass]"\\$fileserver\root\CIMv2:Win32_Share" } else { $Win32ShareClass = [wmiclass]"Win32_Share" } Write-Host "Creating the share." $Win32ShareClass.Create("D:\Users\$newSAMAccount","$newSAMAccount$",0,$null,"") writetolog "Created the share $sharename" #Set the share as the user's home directory and configure the logon script path $homeDirectory = "\\$fileserver\$newSAMAccount$" $homeDrive = "L:" $scriptPath = $objModeluser.scriptPath Write-Host "Setting homeDrive" Set-QADUser $objNewuser -ObjectAttributes @{homeDrive=$homeDrive} writetolog "Set homeDrive as $homeDrive" Write-Host "Setting homeDirectory" Set-QADUser $objNewuser -ObjectAttributes @{homeDirectory=$homedirectory} writetolog "Set homeDirectory as $homeDirectory" Write-Host "Setting scriptPath" Set-QADUser $objNewuser -ObjectAttributes @{scriptPath=$scriptPath} writetolog "Set scriptPath as $scriptPath"
### Begin establishing the mailbox settings Write-Host "Establishing mailbox settings." $ldapquery = "LDAP://" + $objNewuser.DN $newmbx = [ADSI]$ldapquery $newmbx.mailNickname = $newLogonName $newmbx.msExchHomeServerName = $objModeluser.msExchHomeServerName $newmbx.homeMDB = $objModeluser.homeMDB writetolog "ldapquery = $ldapquery" writetolog "mailNickname = $newmbx.mailNickname" writetolog "msExchHomeServerName = $newmbx.msExchHomeServerName" $newmbx.setinfo() writetolog "Established mailbox settings."
writetolog "===== Ending copyuser.ps1 ====="
|