Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

What's wrong with this code? Home directory manipulation

$
0
0

So what I want to do here is create new home folders for each user listed in a csv file, copy the permissions from their old directory to the new one and set their home directory to this New folder:


 
the format of the csv file is as follows:
Username
norma
norm

Here is the code:

#import the active directory module so that the script can use it.
import-module activedirectory
# create an object/variable that stores the usernames listed in a csv file.
$users = Import-csv "c:\UdriveUsers.csv"
# Create a new folder in the target path
# Get the Access Control List, (ACL), from the users previous home directory
# ... apply it
foreach ($user in $users)
 {
  New-Item -path "\\NewServer\NewPath\$users" -itemtype directory
  $acl = get-acl "\\OldServer\OldPath\users\$users"
  Set-acl "\\NewServer\NewPath\$users" $acl
  set-aduser -identity "$users" -homedirectory "\\NewServer\NewPath\$users" -homedrive u:
 }

Here is the error:

New-Item : Item with specified name \\NewServer\NewPath\  already exists.
At C:\scripts\PowerShell\SetHomeDrives.ps1:13 char:3+         New-Item -path "\\NewServer\NewPath\$users" -itemtype directory+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : ResourceExists: (\\NewServer\NewPath\ :String) [New-Item], IOException+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

If I change the following line of code:
$users = Import-csv "c:\UdriveUsers.csv"
To
$users = get-content "c:\UdriveUsers.txt"

then I get the following error(s):

get-acl : Cannot find path '\\OldServer\OldPath\users\norma norm' because it does not exist.
At C:\scripts\PowerShell\SetHomeDrives.ps1:12 char:10+         $acl = get-acl "\\OldServer\OldPath\users\$users"+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException+ FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.
At C:\scripts\PowerShell\SetHomeDrives.ps1:13 char:50+         Set-acl "\\NewServer\NewPath\$users" $acl+                                                        ~~~~+ CategoryInfo          : InvalidData: (:) [Set-Acl], ParameterBindingValidationException+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclCommand
set-aduser : Cannot find an object with identity: 'norma norm' under: 'DC=fpp,DC=wucon,DC=wustl,DC=edu'.
At C:\scripts\PowerShell\SetHomeDrives.ps1:14 char:3+         set-aduser -identity "$users" -homedirectory "\\NewServer\NewPath\$u ...+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : ObjectNotFound: (norma norm:ADUser) [Set-ADUser], ADIdentityNotFoundException+ FullyQualifiedErrorId : Cannot find an object with identity: 'norma norm' under: 'DC=fpp,DC=wucon,DC=wustl,DC=edu'.,Microsoft.ActiveDirectory.Management.Commands.SetADUser
New-Item : Item with specified name \\NewServer\NewPath\norma norm already exists.
At C:\scripts\PowerShell\SetHomeDrives.ps1:11 char:3+         New-Item -path "\\NewServer\NewPath\$users" -itemtype directory+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : ResourceExists: (\\NewServer\NewPath\norma norm:String) [New-Item], IOException+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
get-acl : Cannot find path '\\OldServer\OldPath\users\norma norm' because it does not exist.
At C:\scripts\PowerShell\SetHomeDrives.ps1:12 char:10+         $acl = get-acl "\\OldServer\OldPath\users\$users"+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException+ FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.
At C:\scripts\PowerShell\SetHomeDrives.ps1:13 char:50+         Set-acl "\\NewServer\NewPath\$users" $acl+                                                        ~~~~+ CategoryInfo          : InvalidData: (:) [Set-Acl], ParameterBindingValidationException+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclCommand
set-aduser : Cannot find an object with identity: 'norma norm' under: 'DC=fpp,DC=wucon,DC=wustl,DC=edu'.
At C:\scripts\PowerShell\SetHomeDrives.ps1:14 char:3+         set-aduser -identity "$users" -homedirectory "\\NewServer\NewPath\$u ...+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : ObjectNotFound: (norma norm:ADUser) [Set-ADUser], ADIdentityNotFoundException+ FullyQualifiedErrorId : Cannot find an object with identity: 'norma norm' under: 'DC=fpp,DC=wucon,DC=wustl,DC=edu'.,Microsoft.ActiveDirectory.Management.Commands.SetADUser


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>