Hi, I am trying to create a script for my company. I am hoping someone can take a look and let me know if I am doing something wrong or point me in the right direction. This is important for my company.<\/p>\n
The idea to this script is:<\/p>\n
Any help will be greatly appreciated. Below is the script I have tried to put together. It has not fully been tested.<\/p>\n
#Store<\/span> the data from bulkAD.csv in the $bulkAD variable for Full Time Employees #Loop<\/span> through each row containing user details in the CSV file #Read<\/span> user data from each field in each row and assign the data to a variable as below<\/p>\n $Firstname = $User.firstname #Check<\/span> to see if the user already exists in AD and is a rehire {<\/p>\n Set-ADUser }<\/p>\n #Check<\/span> to see if the user already exists in AD { if ($Check) {$NewSam = “$Sam$i”} New-ADUser #Add<\/span> security groups and distributions Add-ADGroupMember -Identity $Group -Members $SAM<\/p>\n }<\/p>\n #Create<\/span> home directory folder<\/p>\n New-Item $HomeDir -ItemType Directory<\/p>\n $IdentityReference = $Username<\/p>\n #Set<\/span> Rules for Drives<\/p>\n $FileSystemAccessRights = [System.Security.AccessControl.FileSystemRights]”Modify”<\/p>\n $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]”ContainerInherit\", [System.Security.AccessControl.InheritanceFlags]\"ObjectInherit”<\/p>\n $PropagationFlags = [System.Security.AccessControl.PropagationFlags]”None”<\/p>\n $AccessControl = [System.Security.AccessControl.AccessControlType]”Allow”<\/p>\n #Build<\/span> Access Rule from parameters<\/p>\n $AccessRule = NEW-OBJECT System.Security.AccessControl.FileSystemAccessRule -argumentlist $IdentityReference,$FileSystemAccessRights,$InheritanceFlags,$PropagationFlags,$AccessControl<\/p>\n #Get<\/span> current Access Rule from Home Folder for User<\/p>\n $HomeFolderACL=GET-ACL -path $HomeDir<\/p>\n Start-Sleep -s 10<\/p>\n $HomeFolderACL.AddAccessRule($AccessRule)<\/p>\n SET-ACL –path $HomeDir -AclObject $HomeFolderACL<\/p>\n #Create<\/span> Scans folder in Home Directory folder #Create<\/span> _Requested Access folder in Home Directory folder }<\/p>\n else if($Found)<\/p>\n {<\/p>\n #User<\/span> does not exist then proceed to create the new user account<\/p>\n #Account<\/span> will be created in the OU provided by the $OU variable read from the CSV file #Add<\/span> security groups and distributions Add-ADGroupMember -Identity $Group -Members $SAM<\/p>\n }<\/p>\n #Create<\/span> home directory folder<\/p>\n New-Item $HomeDir -ItemType Directory<\/p>\n $IdentityReference = $Username<\/p>\n #Set<\/span> Rules for Drives<\/p>\n $FileSystemAccessRights = [System.Security.AccessControl.FileSystemRights]”Modify”<\/p>\n $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]”ContainerInherit\", [System.Security.AccessControl.InheritanceFlags]\"ObjectInherit”<\/p>\n $PropagationFlags = [System.Security.AccessControl.PropagationFlags]”None”<\/p>\n $AccessControl = [System.Security.AccessControl.AccessControlType]”Allow”<\/p>\n #Build<\/span> Access Rule from parameters<\/p>\n $AccessRule = NEW-OBJECT System.Security.AccessControl.FileSystemAccessRule -argumentlist $IdentityReference,$FileSystemAccessRights,$InheritanceFlags,$PropagationFlags,$AccessControl<\/p>\n #Get<\/span> current Access Rule from Home Folder for User<\/p>\n $HomeFolderACL=GET-ACL -path $HomeDir<\/p>\n Start-Sleep -s 10<\/p>\n $HomeFolderACL.AddAccessRule($AccessRule)<\/p>\n SET-ACL –path $HomeDir -AclObject $HomeFolderACL<\/p>\n #Create<\/span> Scans folder in Home Directory folder #Create<\/span> _Requested Access folder in Home Directory folder }<\/p>\n }<\/p>","upvoteCount":3,"answerCount":10,"datePublished":"2017-09-04T11:09:10.000Z","author":{"@type":"Person","name":"syncore","url":"https://community.spiceworks.com/u/syncore"},"acceptedAnswer":{"@type":"Answer","text":" Hmm, deleting users after a certain time period would generally be better. You’ll slowly use up those available SamAccountNames if nothing else. Actually those and sensible email addresses.<\/p>\n Added error handling when enabling an existing user. So the re-hire option is simple enough.<\/p>\n The continue<\/em> causes the flow to jump to the top of the loop and get the next user.<\/p>\n Next step is to create a new user account. There are plenty of exampel on spiceworks. Bit of searching as it comes up very regularly.<\/p>\n I should add that most of the AD cmdlets like New-ADUser and Set-ADUser have a -WhatIf<\/strong> parameter. You can use that for testing as it will tell you what it’s going to do without actually doing it.<\/p>","upvoteCount":1,"datePublished":"2017-09-04T18:31:04.000Z","url":"https://community.spiceworks.com/t/script-guidance/603729/8","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},"suggestedAnswer":[{"@type":"Answer","text":" Hi, I am trying to create a script for my company. I am hoping someone can take a look and let me know if I am doing something wrong or point me in the right direction. This is important for my company.<\/p>\n The idea to this script is:<\/p>\n Any help will be greatly appreciated. Below is the script I have tried to put together. It has not fully been tested.<\/p>\n #Store<\/span> the data from bulkAD.csv in the $bulkAD variable for Full Time Employees #Loop<\/span> through each row containing user details in the CSV file #Read<\/span> user data from each field in each row and assign the data to a variable as below<\/p>\n $Firstname = $User.firstname #Check<\/span> to see if the user already exists in AD and is a rehire {<\/p>\n Set-ADUser }<\/p>\n #Check<\/span> to see if the user already exists in AD { if ($Check) {$NewSam = “$Sam$i”} New-ADUser #Add<\/span> security groups and distributions Add-ADGroupMember -Identity $Group -Members $SAM<\/p>\n }<\/p>\n #Create<\/span> home directory folder<\/p>\n New-Item $HomeDir -ItemType Directory<\/p>\n $IdentityReference = $Username<\/p>\n #Set<\/span> Rules for Drives<\/p>\n $FileSystemAccessRights = [System.Security.AccessControl.FileSystemRights]”Modify”<\/p>\n $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]”ContainerInherit\", [System.Security.AccessControl.InheritanceFlags]\"ObjectInherit”<\/p>\n $PropagationFlags = [System.Security.AccessControl.PropagationFlags]”None”<\/p>\n $AccessControl = [System.Security.AccessControl.AccessControlType]”Allow”<\/p>\n #Build<\/span> Access Rule from parameters<\/p>\n $AccessRule = NEW-OBJECT System.Security.AccessControl.FileSystemAccessRule -argumentlist $IdentityReference,$FileSystemAccessRights,$InheritanceFlags,$PropagationFlags,$AccessControl<\/p>\n #Get<\/span> current Access Rule from Home Folder for User<\/p>\n $HomeFolderACL=GET-ACL -path $HomeDir<\/p>\n Start-Sleep -s 10<\/p>\n $HomeFolderACL.AddAccessRule($AccessRule)<\/p>\n SET-ACL –path $HomeDir -AclObject $HomeFolderACL<\/p>\n #Create<\/span> Scans folder in Home Directory folder #Create<\/span> _Requested Access folder in Home Directory folder }<\/p>\n else if($Found)<\/p>\n {<\/p>\n #User<\/span> does not exist then proceed to create the new user account<\/p>\n #Account<\/span> will be created in the OU provided by the $OU variable read from the CSV file #Add<\/span> security groups and distributions Add-ADGroupMember -Identity $Group -Members $SAM<\/p>\n }<\/p>\n #Create<\/span> home directory folder<\/p>\n New-Item $HomeDir -ItemType Directory<\/p>\n $IdentityReference = $Username<\/p>\n #Set<\/span> Rules for Drives<\/p>\n $FileSystemAccessRights = [System.Security.AccessControl.FileSystemRights]”Modify”<\/p>\n $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]”ContainerInherit\", [System.Security.AccessControl.InheritanceFlags]\"ObjectInherit”<\/p>\n $PropagationFlags = [System.Security.AccessControl.PropagationFlags]”None”<\/p>\n $AccessControl = [System.Security.AccessControl.AccessControlType]”Allow”<\/p>\n #Build<\/span> Access Rule from parameters<\/p>\n $AccessRule = NEW-OBJECT System.Security.AccessControl.FileSystemAccessRule -argumentlist $IdentityReference,$FileSystemAccessRights,$InheritanceFlags,$PropagationFlags,$AccessControl<\/p>\n #Get<\/span> current Access Rule from Home Folder for User<\/p>\n $HomeFolderACL=GET-ACL -path $HomeDir<\/p>\n Start-Sleep -s 10<\/p>\n $HomeFolderACL.AddAccessRule($AccessRule)<\/p>\n SET-ACL –path $HomeDir -AclObject $HomeFolderACL<\/p>\n #Create<\/span> Scans folder in Home Directory folder #Create<\/span> _Requested Access folder in Home Directory folder }<\/p>\n }<\/p>","upvoteCount":3,"datePublished":"2017-09-04T11:09:10.000Z","url":"https://community.spiceworks.com/t/script-guidance/603729/1","author":{"@type":"Person","name":"syncore","url":"https://community.spiceworks.com/u/syncore"}},{"@type":"Answer","text":" To start, I suggest you read this: PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a><\/p>\n Pay particularly close attention to the 6th bullet point after the picture. It’s really unreasonable to think people will drop their own job and completely debug your huge script–and I’m not saying this in a mean tone, just instructional.<\/p>\n I would suggest taking a portion of the script and asking a specific question that you are having a problem with. You’ll have a lot better luck getting answers that way. Also, don’t forget to use the code button </> to post your code with.<\/p>","upvoteCount":1,"datePublished":"2017-09-04T12:10:14.000Z","url":"https://community.spiceworks.com/t/script-guidance/603729/2","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":" If we have a new hire from HR, I want the system to check to see if the SamAccountName is taken and if it is then I want it to add increment numbers. (i.e. SCore is taken so it will become SCore1)If the SamAccountName is not taken the script will create the new user.One other thing if the SamAccountName is taken it is possible to be a rehire. I want it to check that it is not a rehire. If it is I want it to re-enable the users account.<\/em><\/p>\n I don’t think 3 things are possible<\/p>\n Use code format to paste code<\/p>\n Thank you, JitenSh! My apologies for not posting in the correct format, I am still new to this site. Will not happen again.<\/p>\n So, there is no way to incorporate the third option, even if I have a column on my CSV that says Rehire? Maybe something like this and I set parameters for the rehire.<\/p>\n Really don’t have a AD to check, may be something like. Also Tagging Neally and gungnir<\/p>\n @alexw<\/a> @gungnir<\/a><\/p>","upvoteCount":0,"datePublished":"2017-09-04T14:31:43.000Z","url":"https://community.spiceworks.com/t/script-guidance/603729/5","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"
\n$bulkAD = Import-csv “C:\\Users\\Administrator\\Documents\\bulkAD.csv”<\/p>\n
\nforeach ($User in $bulkAD)
\n{<\/p>\n
\n$Lastname = $User.lastname
\n$SAM = $User.firstname.Substring(0,1) + $User.lastname
\n$Proxydomain = “@adtest.com<\/span>”
\n$Username = $SAM + $Proxydomain
\n$Password = ‘Welcome2017’
\n$OU = “OU=Associates,OU=MainTest,DC=adtest,DC=Com”
\n$Description = $User.Description
\n$Email = $User.firstname + $User.lastname + $Proxydomain
\n$HomeDir = $User.HomeDirectory + '' + $SAM
\n$Mgr = “CN=” + $User.manager + $OU
\n$Job = $User.JobTitle
\n$Dept = $User.department
\n$Company = $User.company
\n$StreetAddress = $User.Streetaddress
\n$City = $User.city
\n$State = $User.state
\n$PostCode = $User.postcode
\n$Country = ‘US’
\n$ProxyAddress1 = “SMTP:” + $Firstname + ‘.’ + $Lastname + “@adtest.com<\/span>”
\n$ProxyAddress2 = “smtp:” + $SAM + “@adtest.com<\/span>”
\n$Scans = $HomeDir + '' + ‘Scans’
\n$ReqAccess = $HomeDir + '' + ‘_Requested Access’
\n$Expiredate = Get-Date
\n$status = $User.Status
\n$NewSam = $SAM
\n$Check = (Get-ADUser -F {SamAccountName -eq $SAM})
\n$Found = $False<\/p>\n
\nif (Get-ADUser -F {SamAccountName -eq $SAM -and $Status -eq “rehire”})<\/p>\n-Enabled $True<\/code>
\n-Description “$Description” -Path $OU<\/code>
\n-ChangePasswordAtLogon $true -AccountPassword (convertto-securestring $Password -AsPlainText -Force )<\/code>
\n-Manager “$Mgr” -Department \"$Dept\"<\/code>
\n-Title “$Job” -Company \"$Company\"<\/code>
\n-StreetAddress $Streetaddress -State $State<\/code>
\n-PostalCode $PostCode `
\n-Country “US”<\/p>\n
\nelse if<\/p>\n
\nfor ($i=1; $i -le 60; $i ++) {<\/p>\n
\nElse {$Found = $True;Break}
\n}
\nif ($Found -eq $True)
\n{<\/p>\n -SamAccountName $NewSam<\/code>
\n-UserPrincipalName “$Username” -Name \"$Firstname $Lastname\"<\/code>
\n-GivenName $Firstname -Surname $Lastname<\/code>
\n-Enabled $True -DisplayName \"$Lastname, $Firstname\"<\/code>
\n-Description “$Description” -Path $OU<\/code>
\n-EmailAddress $Email -ChangePasswordAtLogon $true<\/code>
\n-AccountPassword (convertto-securestring $Password -AsPlainText -Force ) -HomeDirectory \"$HomeDir\"<\/code>
\n-HomeDrive “H:” -Manager \"$Mgr\"<\/code>
\n-Department “$Dept” -Title \"$Job\"<\/code>
\n-Company “$Company” -StreetAddress $Streetaddress<\/code>
\n-State $State -PostalCode $PostCode<\/code>
\n-Country “US” -Add @{Proxyaddresses=$ProxyAddress1}<\/code>
\n-Add @{Proxyaddresses=$ProxyAddress2}<\/p>\n
\nforeach ($Group in ($User.Group -split ‘;’))
\n{<\/p>\n
\nNew-Item $Scans -ItemType Directory<\/p>\n
\nNew-Item $ReqAccess -ItemType Directory<\/p>\n
\nNew-ADUser -SamAccountName $SAM<\/code>
\n-UserPrincipalName “$Username” -Name \"$Firstname $Lastname\"<\/code>
\n-GivenName $Firstname -Surname $Lastname<\/code>
\n-Enabled $True -DisplayName \"$Lastname, $Firstname\"<\/code>
\n-Description “$Description” -Path $OU<\/code>
\n-EmailAddress $Email -ChangePasswordAtLogon $true<\/code>
\n-AccountPassword (convertto-securestring $Password -AsPlainText -Force ) -HomeDirectory \"$HomeDir\"<\/code>
\n-HomeDrive “H:” -Manager \"$Mgr\"<\/code>
\n-Department “$Dept” -Title \"$Job\"<\/code>
\n-Company “$Company” -StreetAddress $Streetaddress<\/code>
\n-State $State -PostalCode $PostCode<\/code>
\n-Country “US” -Add @{Proxyaddresses=$ProxyAddress1}<\/code>
\n-Add @{Proxyaddresses=$ProxyAddress2}<\/p>\n
\nforeach ($Group in ($User.Group -split ‘;’))
\n{<\/p>\n
\nNew-Item $Scans -ItemType Directory<\/p>\n
\nNew-Item $ReqAccess -ItemType Directory<\/p>\n # Check to see if the user already exists in AD and is a rehire\n if ($Status -eq \"rehire\")\n {\n try \n {\n Set-ADUser -Identity $SAM `\n -Enabled $True `\n -ErrorAction Stop `\n ....\n }\n catch \n {\n Write-Warning \"Failed to re-enable this account: $SAM\"\n }\n\n continue\n }\n\n<\/code><\/pre>\n
\n
\n$bulkAD = Import-csv “C:\\Users\\Administrator\\Documents\\bulkAD.csv”<\/p>\n
\nforeach ($User in $bulkAD)
\n{<\/p>\n
\n$Lastname = $User.lastname
\n$SAM = $User.firstname.Substring(0,1) + $User.lastname
\n$Proxydomain = “@adtest.com<\/span>”
\n$Username = $SAM + $Proxydomain
\n$Password = ‘Welcome2017’
\n$OU = “OU=Associates,OU=MainTest,DC=adtest,DC=Com”
\n$Description = $User.Description
\n$Email = $User.firstname + $User.lastname + $Proxydomain
\n$HomeDir = $User.HomeDirectory + '' + $SAM
\n$Mgr = “CN=” + $User.manager + $OU
\n$Job = $User.JobTitle
\n$Dept = $User.department
\n$Company = $User.company
\n$StreetAddress = $User.Streetaddress
\n$City = $User.city
\n$State = $User.state
\n$PostCode = $User.postcode
\n$Country = ‘US’
\n$ProxyAddress1 = “SMTP:” + $Firstname + ‘.’ + $Lastname + “@adtest.com<\/span>”
\n$ProxyAddress2 = “smtp:” + $SAM + “@adtest.com<\/span>”
\n$Scans = $HomeDir + '' + ‘Scans’
\n$ReqAccess = $HomeDir + '' + ‘_Requested Access’
\n$Expiredate = Get-Date
\n$status = $User.Status
\n$NewSam = $SAM
\n$Check = (Get-ADUser -F {SamAccountName -eq $SAM})
\n$Found = $False<\/p>\n
\nif (Get-ADUser -F {SamAccountName -eq $SAM -and $Status -eq “rehire”})<\/p>\n-Enabled $True<\/code>
\n-Description “$Description” -Path $OU<\/code>
\n-ChangePasswordAtLogon $true -AccountPassword (convertto-securestring $Password -AsPlainText -Force )<\/code>
\n-Manager “$Mgr” -Department \"$Dept\"<\/code>
\n-Title “$Job” -Company \"$Company\"<\/code>
\n-StreetAddress $Streetaddress -State $State<\/code>
\n-PostalCode $PostCode `
\n-Country “US”<\/p>\n
\nelse if<\/p>\n
\nfor ($i=1; $i -le 60; $i ++) {<\/p>\n
\nElse {$Found = $True;Break}
\n}
\nif ($Found -eq $True)
\n{<\/p>\n -SamAccountName $NewSam<\/code>
\n-UserPrincipalName “$Username” -Name \"$Firstname $Lastname\"<\/code>
\n-GivenName $Firstname -Surname $Lastname<\/code>
\n-Enabled $True -DisplayName \"$Lastname, $Firstname\"<\/code>
\n-Description “$Description” -Path $OU<\/code>
\n-EmailAddress $Email -ChangePasswordAtLogon $true<\/code>
\n-AccountPassword (convertto-securestring $Password -AsPlainText -Force ) -HomeDirectory \"$HomeDir\"<\/code>
\n-HomeDrive “H:” -Manager \"$Mgr\"<\/code>
\n-Department “$Dept” -Title \"$Job\"<\/code>
\n-Company “$Company” -StreetAddress $Streetaddress<\/code>
\n-State $State -PostalCode $PostCode<\/code>
\n-Country “US” -Add @{Proxyaddresses=$ProxyAddress1}<\/code>
\n-Add @{Proxyaddresses=$ProxyAddress2}<\/p>\n
\nforeach ($Group in ($User.Group -split ‘;’))
\n{<\/p>\n
\nNew-Item $Scans -ItemType Directory<\/p>\n
\nNew-Item $ReqAccess -ItemType Directory<\/p>\n
\nNew-ADUser -SamAccountName $SAM<\/code>
\n-UserPrincipalName “$Username” -Name \"$Firstname $Lastname\"<\/code>
\n-GivenName $Firstname -Surname $Lastname<\/code>
\n-Enabled $True -DisplayName \"$Lastname, $Firstname\"<\/code>
\n-Description “$Description” -Path $OU<\/code>
\n-EmailAddress $Email -ChangePasswordAtLogon $true<\/code>
\n-AccountPassword (convertto-securestring $Password -AsPlainText -Force ) -HomeDirectory \"$HomeDir\"<\/code>
\n-HomeDrive “H:” -Manager \"$Mgr\"<\/code>
\n-Department “$Dept” -Title \"$Job\"<\/code>
\n-Company “$Company” -StreetAddress $Streetaddress<\/code>
\n-State $State -PostalCode $PostCode<\/code>
\n-Country “US” -Add @{Proxyaddresses=$ProxyAddress1}<\/code>
\n-Add @{Proxyaddresses=$ProxyAddress2}<\/p>\n
\nforeach ($Group in ($User.Group -split ‘;’))
\n{<\/p>\n
\nNew-Item $Scans -ItemType Directory<\/p>\n
\nNew-Item $ReqAccess -ItemType Directory<\/p>\ntry {\n if (!(get-aduser -Filter {samaccountname -eq \"$SAM\"})){\n $Parameters = @{\n 'SamAccountName' = $Sam\n 'UserPrincipalName' = $UPN \n 'Name' = $Fullname\n \n}\n\nNew-ADUser @Parameters\n\n$a=1;\nelse\n{\n$sam=\"Core$a++\"\n\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-04T12:41:23.000Z","url":"https://community.spiceworks.com/t/script-guidance/603729/3","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"
try {\nif (!(get-aduser -Filter {samaccountname -eq \"$SAM\" -and $User.Status -eq \"Rehire\"})){\n\t\n$RehireParameters = @{\n'Enabled' $True \n'Description' = \"$Description\" \n'ChangePasswordAtLogon' = $true \n'AccountPassword' = (convertto-securestring $Password -AsPlainText -Force )\n}\n\t\n$NewHireParameters = @{\n'SamAccountName' = $Sam\n'UserPrincipalName' = $UPN \n'Name' = $Fullname\n \n}\n\nSet-ADUser @$RehireParameters\n\nelse\n\nNew-ADUser @$NewHireParameters\n\n$a=1;\nelse\n{\n$sam=\"Core$a++\"\n\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-04T13:32:30.000Z","url":"https://community.spiceworks.com/t/script-guidance/603729/4","author":{"@type":"Person","name":"syncore","url":"https://community.spiceworks.com/u/syncore"}},{"@type":"Answer","text":"
$Users=import-csv c:\\users.csv\n\n```\nforeach ($user in $Users) {\n$stx=$user.status\n try {\n $ADUser = Get-ADUser -Identity $user -ErrorAction Stop\n }\n catch {\n if ($_ -like \"*Cannot find an object with identity: '$user'*\") {\n New-aduser\n }\n else {\n \"An error occurred: $_\"\n }\n continue\n }\n if ($_ -like \"*User '$($ADUser.SamAccountName)' exists.*\" -and $stx -eq \"Rehire\"){\n # rehire parameters\n}\n}\n```\n\n<\/code><\/pre>\n