Hello,<\/p>\n
So right off the bat I have basically zero experience with powershell either ran running scripts with it that have already been written and tested. I am using a script that grabs student info from a .csv and adds to a OU in AD. here is the script:<\/p>\n
----------------------------------------------------------Beginning------------------------------------------------------------------<\/p>\n
#Import<\/span> the PowerShell module containing AD cmdlets write-host “Start Process” write-host “Reading the CSV file…”<\/p>\n #Loop<\/span> through all items in the CSV items #Check<\/span> if the User exists If ($ADuser -eq $Null) if ($user.Program -eq “AC”) if ($user.Program -eq “INSTR”) if ($user.Program -eq “PTECH”) if ($user.Program -eq “DRAFT”) if ($user.Program -eq “ELECTRIC”) if ($user.Program -eq “ELECTRON”) if ($user.Program -eq “IT”) if ($user.Program -eq “MC”) if ($user.Program -eq “OA”) if ($user.Program -eq “MA”) if ($user.Program -eq “CNST-MGT”) New-ADUser -Name $saMAccountName Add-ADGroupMember -Identity Students -Members $user.userName<\/p>\n #Set<\/span> Password to never expire #Check<\/span> if the User exists if ($ADuser -ne $Null) else else if ($user.Program -eq ‘IT’) catch else }<\/p>\n if ($user.Program -eq ‘OA’) catch else }<\/p>\n if ($user.Program -eq ‘DRAFT’) catch else }<\/p>\n if ($user.Program -eq ‘MC’) catch else }<\/p>\n $homeDir = \"\\martin-fs1\\Users\" if ( (Test-Path “$homeDir$sso”) -eq $false) if ( (Test-Path “$homeDir$sso”) -eq $true) else else catch write-host “-----------------------------------------------------------------” ------------------------------------------------------------End-----------------------------------------------------------------------<\/p>\n The error I receive is this:<\/p>\n ----------------------------------------------------------Beginning------------------------------------------------------------------<\/p>\n Error: InvalidOperation: (CN=cdeaton01655…lassroom,DC=com:String) [New-ADUser], ADInvalidOperationException ------------------------------------------------------------End-----------------------------------------------------------------------<\/p>\n Any and all help greatly appreciated<\/p>","upvoteCount":3,"answerCount":14,"datePublished":"2020-02-20T16:38:54.000Z","author":{"@type":"Person","name":"jeremybrown12","url":"https://community.spiceworks.com/u/jeremybrown12"},"suggestedAnswer":[{"@type":"Answer","text":" Hello,<\/p>\n So right off the bat I have basically zero experience with powershell either ran running scripts with it that have already been written and tested. I am using a script that grabs student info from a .csv and adds to a OU in AD. here is the script:<\/p>\n ----------------------------------------------------------Beginning------------------------------------------------------------------<\/p>\n #Import<\/span> the PowerShell module containing AD cmdlets write-host “Start Process” write-host “Reading the CSV file…”<\/p>\n #Loop<\/span> through all items in the CSV items #Check<\/span> if the User exists If ($ADuser -eq $Null) if ($user.Program -eq “AC”) if ($user.Program -eq “INSTR”) if ($user.Program -eq “PTECH”) if ($user.Program -eq “DRAFT”) if ($user.Program -eq “ELECTRIC”) if ($user.Program -eq “ELECTRON”) if ($user.Program -eq “IT”) if ($user.Program -eq “MC”) if ($user.Program -eq “OA”) if ($user.Program -eq “MA”) if ($user.Program -eq “CNST-MGT”) New-ADUser -Name $saMAccountName Add-ADGroupMember -Identity Students -Members $user.userName<\/p>\n #Set<\/span> Password to never expire #Check<\/span> if the User exists if ($ADuser -ne $Null) else else if ($user.Program -eq ‘IT’) catch else }<\/p>\n if ($user.Program -eq ‘OA’) catch else }<\/p>\n if ($user.Program -eq ‘DRAFT’) catch else }<\/p>\n if ($user.Program -eq ‘MC’) catch else }<\/p>\n $homeDir = \"\\martin-fs1\\Users\" if ( (Test-Path “$homeDir$sso”) -eq $false) if ( (Test-Path “$homeDir$sso”) -eq $true) else else catch write-host “-----------------------------------------------------------------” ------------------------------------------------------------End-----------------------------------------------------------------------<\/p>\n The error I receive is this:<\/p>\n ----------------------------------------------------------Beginning------------------------------------------------------------------<\/p>\n Error: InvalidOperation: (CN=cdeaton01655…lassroom,DC=com:String) [New-ADUser], ADInvalidOperationException ------------------------------------------------------------End-----------------------------------------------------------------------<\/p>\n Any and all help greatly appreciated<\/p>","upvoteCount":3,"datePublished":"2020-02-20T16:38:54.000Z","url":"https://community.spiceworks.com/t/receiving-error-from-my-ad-import-powershell-script/752184/1","author":{"@type":"Person","name":"jeremybrown12","url":"https://community.spiceworks.com/u/jeremybrown12"}},{"@type":"Answer","text":" Is this the full OU path? Is Students a part of an OU?<\/p>\n $path = ‘OU=Students,DC=iticlassroom,DC=com’<\/p>\n Also try using splatting instead of back tics in the New-ADUser block.<\/p>","upvoteCount":0,"datePublished":"2020-02-20T17:17:12.000Z","url":"https://community.spiceworks.com/t/receiving-error-from-my-ad-import-powershell-script/752184/2","author":{"@type":"Person","name":"iansmith17","url":"https://community.spiceworks.com/u/iansmith17"}},{"@type":"Answer","text":" Please use the </> button to post your script in a format that is easier to read than plain text.<\/p>\n Also, please read this article about guidelines for posting:<\/p>\n
\nImport-Module ActiveDirectory<\/p>\n
\nwrite-host “-------------------------------------”
\ntry
\n{
\n#Read<\/span> the CSV file
\n$csvPath = “C:\\AD_StudentImport\\AD_StudentImport.csv”
\n$csvData = import-csv $csvPath<\/p>\n
\nForEach ($user In $csvData)
\n{
\n$saMAccountName = $user.username<\/p>\n
\n$ADuser = Get-ADUser -LDAPFilter “(sAMAccountName=$saMAccountName)”<\/p>\n
\n{
\n#Create<\/span> user using New-ADUser cmdlet
\n$path = ‘OU=Students,DC=iticlassroom,DC=com’
\n$userPrincipalName = $user.userName + “@iticlassroom.com<\/span>”
\n$FullName = $user.firstname + \" \" + $user.lastname
\n$ScriptPath = “default” + $user.Program + “.bat”
\n$HomeDirectory = \"\\martin-fs1\\users\" + $user.userName<\/p>\n
\n{
\n$program = “Air Conditioning”
\n}<\/p>\n
\n{
\n$program = “Instrumentation”
\n}<\/p>\n
\n{
\n$program = “Process Technology”
\n}<\/p>\n
\n{
\n$program = “Drafting And Design”
\n}<\/p>\n
\n{
\n$program = “Instrumentation”
\n}<\/p>\n
\n{
\n$program = “Instrumentation”
\n}<\/p>\n
\n{
\n$description = “Process Technology”
\n}<\/p>\n
\n{
\n$description = “Drafting And Design”
\n}<\/p>\n
\n{
\n$program = “Process Technology”
\n}<\/p>\n
\n{
\n$program = “Drafting And Design”
\n}<\/p>\n
\n{
\n$program = “Construction Management”
\n}<\/p>\n-SamAccountName $saMAccountName<\/code>
\n-UserPrincipalName $userPrincipalName -GivenName $user.firstname<\/code>
\n-Surname $user.lastname -Description $user.stunum<\/code>
\n-DisplayName $FullName -EmailAddress $user.EmailLogin<\/code>
\n-AccountPassword (ConvertTo-SecureString $user.password -AsPlainText -Force) -Path $path<\/code>
\n-ScriptPath $ScriptPath -HomeDirectory $HomeDirectory<\/code>
\n-HomeDrive “H:” -Enabled $true<\/code>
\n-OtherAttributes @{‘cvProgram’=$program;‘cvStuNum’=$user.stunum;‘cvSyStudentID’=$user.systudentid}<\/p>\n<\/a>Adding User to Group<\/h1>\n
\nset-aduser -identity $saMAccountName -PasswordNeverExpires $true<\/p>\n
\n$ADuser = Get-ADUser -LDAPFilter “(sAMAccountName=$saMAccountName)”<\/p>\n
\n{
\nwrite-host \"- \" $user.userName “|Account Created & Added to Students Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “WTF!!!” $user.userName -ForegroundColor red
\n}
\n}<\/p>\n
\n{
\nwrite-host \"- \" $user.userName “|Account Exists” -ForegroundColor yellow
\n}<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n$sso = $user.userName<\/p>\n<\/a>If the folder for the user does not exist, make a new one and set the correct permissions.<\/h1>\n
\n{
\n$NewFolder = New-Item -Path $homeDir -Name $sso -ItemType “Directory”
\n$Rights = [System.Security.AccessControl.FileSystemRights]“FullControl,Modify,ReadAndExecute,ListDirectory,Read,Write”
\n$InheritanceFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
\n$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
\n$objType =[System.Security.AccessControl.AccessControlType]::Allow
\n$objUser = New-Object System.Security.Principal.NTAccount “itimartin.com<\/a>$sso”
\n$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
\n($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType)
\n$ACL = Get-Acl -Path $NewFolder
\n$ACL.AddAccessRule($objACE)
\nSet-ACL -Path $NewFolder.FullName -AclObject $ACL<\/p>\n
\n{
\nwrite-host “Directory” “$homeDir$sso” “Created” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “WTF!!!” “$homeDir$sso” -ForegroundColor red
\n}
\n}<\/p>\n
\n{
\nwrite-host “Directory” “$homeDir$sso” “Already Exists” -ForegroundColor yellow
\n}
\n}
\n}<\/p>\n
\n{
\nwrite-host \"Error: \" $($.CategoryInfo) -ForegroundColor red
\nwrite-host \"Message: \" $($<\/em>.Exception.Message) -ForegroundColor red
\n}<\/p>\n
\nwrite-host “End Process”<\/p>\n
\nMessage: The parameter is incorrect<\/p>\n
\nImport-Module ActiveDirectory<\/p>\n
\nwrite-host “-------------------------------------”
\ntry
\n{
\n#Read<\/span> the CSV file
\n$csvPath = “C:\\AD_StudentImport\\AD_StudentImport.csv”
\n$csvData = import-csv $csvPath<\/p>\n
\nForEach ($user In $csvData)
\n{
\n$saMAccountName = $user.username<\/p>\n
\n$ADuser = Get-ADUser -LDAPFilter “(sAMAccountName=$saMAccountName)”<\/p>\n
\n{
\n#Create<\/span> user using New-ADUser cmdlet
\n$path = ‘OU=Students,DC=iticlassroom,DC=com’
\n$userPrincipalName = $user.userName + “@iticlassroom.com<\/span>”
\n$FullName = $user.firstname + \" \" + $user.lastname
\n$ScriptPath = “default” + $user.Program + “.bat”
\n$HomeDirectory = \"\\martin-fs1\\users\" + $user.userName<\/p>\n
\n{
\n$program = “Air Conditioning”
\n}<\/p>\n
\n{
\n$program = “Instrumentation”
\n}<\/p>\n
\n{
\n$program = “Process Technology”
\n}<\/p>\n
\n{
\n$program = “Drafting And Design”
\n}<\/p>\n
\n{
\n$program = “Instrumentation”
\n}<\/p>\n
\n{
\n$program = “Instrumentation”
\n}<\/p>\n
\n{
\n$description = “Process Technology”
\n}<\/p>\n
\n{
\n$description = “Drafting And Design”
\n}<\/p>\n
\n{
\n$program = “Process Technology”
\n}<\/p>\n
\n{
\n$program = “Drafting And Design”
\n}<\/p>\n
\n{
\n$program = “Construction Management”
\n}<\/p>\n-SamAccountName $saMAccountName<\/code>
\n-UserPrincipalName $userPrincipalName -GivenName $user.firstname<\/code>
\n-Surname $user.lastname -Description $user.stunum<\/code>
\n-DisplayName $FullName -EmailAddress $user.EmailLogin<\/code>
\n-AccountPassword (ConvertTo-SecureString $user.password -AsPlainText -Force) -Path $path<\/code>
\n-ScriptPath $ScriptPath -HomeDirectory $HomeDirectory<\/code>
\n-HomeDrive “H:” -Enabled $true<\/code>
\n-OtherAttributes @{‘cvProgram’=$program;‘cvStuNum’=$user.stunum;‘cvSyStudentID’=$user.systudentid}<\/p>\n<\/a>Adding User to Group<\/h1>\n
\nset-aduser -identity $saMAccountName -PasswordNeverExpires $true<\/p>\n
\n$ADuser = Get-ADUser -LDAPFilter “(sAMAccountName=$saMAccountName)”<\/p>\n
\n{
\nwrite-host \"- \" $user.userName “|Account Created & Added to Students Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “WTF!!!” $user.userName -ForegroundColor red
\n}
\n}<\/p>\n
\n{
\nwrite-host \"- \" $user.userName “|Account Exists” -ForegroundColor yellow
\n}<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n{
\ntry
\n{
\nAdd-ADGroupMember -Identity AdminRights -Member $user.userName
\nwrite-host “Student Added to AdminRights Group” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “Student Already a member of AdminRights Group” -ForegroundColor yellow
\n}
\n}<\/p>\n
\n{<\/p>\n
\n$sso = $user.userName<\/p>\n<\/a>If the folder for the user does not exist, make a new one and set the correct permissions.<\/h1>\n
\n{
\n$NewFolder = New-Item -Path $homeDir -Name $sso -ItemType “Directory”
\n$Rights = [System.Security.AccessControl.FileSystemRights]“FullControl,Modify,ReadAndExecute,ListDirectory,Read,Write”
\n$InheritanceFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
\n$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
\n$objType =[System.Security.AccessControl.AccessControlType]::Allow
\n$objUser = New-Object System.Security.Principal.NTAccount “itimartin.com<\/a>$sso”
\n$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
\n($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType)
\n$ACL = Get-Acl -Path $NewFolder
\n$ACL.AddAccessRule($objACE)
\nSet-ACL -Path $NewFolder.FullName -AclObject $ACL<\/p>\n
\n{
\nwrite-host “Directory” “$homeDir$sso” “Created” -ForegroundColor green
\n}<\/p>\n
\n{
\nwrite-host “WTF!!!” “$homeDir$sso” -ForegroundColor red
\n}
\n}<\/p>\n
\n{
\nwrite-host “Directory” “$homeDir$sso” “Already Exists” -ForegroundColor yellow
\n}
\n}
\n}<\/p>\n
\n{
\nwrite-host \"Error: \" $($.CategoryInfo) -ForegroundColor red
\nwrite-host \"Message: \" $($<\/em>.Exception.Message) -ForegroundColor red
\n}<\/p>\n
\nwrite-host “End Process”<\/p>\n
\nMessage: The parameter is incorrect<\/p>\n