
This article focuses on automating the creation, relocation and firing of people in accordance with the personnel changes made in 1C.
The principle of the script is to parse unloaded user data from 1C and assign this data to script variables with further use.
In our company, after the user's entry in 1C, a CSV file is created with the data, an example below:
document type | userName | Fio | Department | Department |
---|
recruitment | USERtester1 | Ivanov Ivan Ivanovich | Disponent | Division of disposition and paperwork |
moving | USERtester2 | Petrov Ivan Ivanovich | Renewal Manager | Credit and Insurance Department |
dismissal | USERtester3 | Sidorov Ivan Ivanovich | Renewal Manager | Credit and Insurance Department |
The script contains three functions (usrcreate; usrmove; usrdelete) in accordance with the types of operations:
Recruitment
We are looking for a user template based on the position and department name, after finding the necessary attributes and groups, we write them to the new user, at the same time a home folder is created and connected, and the mailing address is associated. A report is sent to the mail with the user name, full name and a list of groups to which it was added.
')
Frame moving
We find the user template for the new position and the new department of the employee, from which we get the new list of groups and the OU address, after finding the old groups from the user being moved and adding it to the new ones, simultaneously transfer it to the OU corresponding to the new department (if there is such a division in AD), and change posts for new ones respectively. A report with a new post, department and groups is sent to the post office.
Dismissal
Disable the mailbox, remove the user from all groups, move it to the folder for disabled users and disable its account. A report is sent to the mail that the user is disconnected and dismissed.
The script includes a number of checks:
Check for need to start Exchange and Active Directory snap-ins
In this test, data is searched for processing if they are not found. The snap-ins will not be loaded, which will save approximately 7 seconds of time.
Check for existence of data for processing in variables $ getstaff | $ movestaff | $ delstaff
To run the functions of creating, moving and deleting users.
Check on the existence of the created user
To check for duplicate tasks to create a user.
Check on the existence of the user's network folder
To bypass the folder existence error.
Position and department check
To check for duplication of the transfer request when the user has already been moved to a new position.
Check for user termination
To check for duplication of applications for dismissal, when the user has already been dismissed.
Description of script variables:$ smtpserv | Mailing (Server) |
---|
$ smtpfrom | Mailing (From whom) |
---|
$ smtpto | Mailing (To) |
---|
$ opertype | Type of user operation |
---|
$ usrname | SamAccountName |
---|
$ Fio | Full Name |
---|
$ curdate | Current date in day.month.year format |
---|
$ del | $ Delstaff array element |
---|
$ delstaff | User details for dismissal |
---|
$ department | Department |
---|
$ dir | Place where 1C unloads CSV |
---|
$ get | Element of the array $ getstaff |
---|
$ getstaff | User data for recruitment |
---|
$ groups | List of copied groups for report |
---|
$ move | The element of the array $ movestaff |
---|
$ movestaff | User data for personnel movement |
---|
$ newusername | Name |
---|
$ newusersurname | Surname |
---|
$ Position | Position |
---|
$ sdel | Distributed $ del |
---|
$ sget | Public $ get |
---|
$ smove | Shared $ move |
---|
$ shablon | SamAccountName user template |
---|
$ spisokf | List of files in the download directory from 1C |
---|
$ todayf | The file which contains the personnel changes for today |
---|
$ usrcopyfrom | User template |
---|
$ usrexist | User login to check for existence |
---|
$ usrpath | User location in AD |
---|
$ usrprop | Template User Properties |
---|
$ usrdomain | Your domain |
---|
$ Session | Session with Exchange Management Console |
---|
$ usrshare | Address of the user folder on the network storage |
---|
$ pass | Temporary user password |
---|
$ company | Company name |
---|
$ usrstate | Company location |
---|
$ usrCity | City |
---|
$ usrStreetAddress | Street House Corps |
---|
$ curuserpath | The current location of the user being moved to AD |
---|
$ newuserpath | Target location of user being moved to AD |
---|
$ usrmoveddep | Current user department to check for duplicate move order |
---|
$ usrmovedtit | The current user position to check for duplicate move order |
---|
Elements in $ get | $ move | $ del arrays
[0] | Type of operation: recruitment / relocation / dismissal |
---|
[one] | SamAccountName |
---|
[2] | Full name |
---|
[3] | Position |
---|
[four] | Department |
---|
The script itself:
# $smtpserv = "post.domain.local" $smtpfrom = "AUTOUSERCHANGER@domain.local" $smtpto = "sysadmin@domain.local" $encoding = [System.Text.Encoding]::UTF8 $smtpBody = "" # 1 $dir= "\\fileserver\1C\" # $usrdomain="domain.local" # csv $spisokf=Get-Childitem -File -Path $dir*.csv | Select-Object -ExpandProperty Name # $curdate=Get-Date -Format dd.MM.yyyy $todayf=$spisokf | Select-String $curdate # $getstaff= Get-Content $dir$todayf |Select-String " " $movestaff= Get-Content $dir$todayf |Select-String "" $delstaff= Get-Content $dir$todayf |Select-String "" # EXCHANGE ActiveDirectory if ($getstaff -ne $null -or $movestaff -ne $null -or $delstaff -ne $null) { $Session=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://post.domain.local/PowerShell/ -Authentication Kerberos Import-PSSession $Session -AllowClobber | out-null Import-Module ActiveDirectory } else { Write-Host " " } # function usrcreate () { # ForEach ($get in $getstaff) { # $sget=$get -split (";") $opertype = $sget[0] $usrname=$sget[1] $FIO=$sget[2] $Position=$sget[3] $department=$sget[4] # $usrexist=Get-ADUser -filter {(SamAccountName -eq $usrname)} | Select-Object -ExpandProperty SamAccountName if ($usrexist -eq $null) { # $usrshare="\\FILESERVER\Users\$usrname" # $shablon=Get-ADUser -filter { (physicalDeliveryOfficeName -like $department) -and (title -like $Position) -and (Enabled -eq $true) -and (SamAccountName -ne $usrname)} | Select-Object -ExpandProperty SamAccountName $usrcopyfrom=$shablon[1] #-Path $usrprop= Get-ADUser -Identity $usrcopyfrom | Select-Object -ExpandProperty DistinguishedName $usrpath= ($usrprop -split