PowerShellで人員配置の変更を自動化する

画像 この記事は、1Cで行われた人事異動に応じて人を作成、移動、および解雇するための操作の自動化に専念しています。



スクリプトの原則は、1Cからダウンロードされたユーザーデータを解析し、このデータをさらに使用してスクリプト変数に割り当てることです。



当社では、1Cでのユーザーの確立後に、データを含むCSVファイルが作成されます。以下に例を示します。

文書タイプ userName フィオ 部門 部門
雇う USERtester1 イワノフイワンイワノビッチ ディスペンサー 処分および書類事務局
動いている USERtester2 ペトロフ・イヴァン・イワノビッチ 拡張マネージャー 信用保険部
解雇 USERtester3 シドロフ・イヴァン・イワノビッチ 拡張マネージャー 信用保険部


スクリプトには、操作のタイプに応じて3つの関数(usrcreate、usrmove、usrdelete)が含まれています。



採用



部門の役職と名前に基づいてユーザーテンプレートを探しています。必要な属性とグループを取得し、それらを新しいユーザーに書き込むと同時に、ホームフォルダーを作成して接続し、郵送先住所も添付します。 レポートは、ユーザー名、フルネーム、および追加されたグループのリストとともにメールに送信されます。



人事の動き



新しい役職と従業員の新しい部門のユーザーテンプレートを見つけ、そこから新しいグループとOUアドレスのリストを取得し、見つかった後、ローミングユーザーから古いグループを削除して新しいグループに追加し、同時に新しい部門に対応するOUに転送します(そのような部門がある場合) AD)、および新しい位置にそれぞれ変更します。 新しい役職、部門、グループを含むレポートがメールに送信されます。



解雇



メールボックスを切断し、すべてのグループからユーザーを削除し、無効なユーザーのフォルダーに移動して、アカウントを切断します。 ユーザーが切断され、解雇されたというレポートがメールに送信されます。



スクリプトにはいくつかのチェックが含まれています。



ExchangeおよびActive Directoryスナップインを実行する必要があることを確認します



このチェックでは、見つからない場合に処理されるデータの検索が行われ、スナップインが読み込まれないため、約7秒の時間が節約されます。



変数で処理されるデータを確認する$ getstaff | $ movestaff | $ delstaff



ユーザーの作成、移動、削除の機能を実行するため。



作成されたユーザーの存在の確認



重複したユーザー作成タスクを確認します。



ユーザーネットワークフォルダーを確認する



フォルダーの存在エラーを回避するには。



位置と部門の検証



ユーザーがすでに新しい位置に移動している場合に、重複した転送要求を確認します。



ユーザー解雇チェック



ユーザーが既に解雇されている場合に、解雇の申請の重複をチェックする。



スクリプト変数の説明:
$ smtpserv メール送信(サーバー)
$ smtpfrom 送料(差出人)
$ smtpto 送料(To)
$ opertype ユーザー操作の種類
$ usrname SamAccountName
$ FIO 姓名パトロニミック
$ Curdate day.month.year形式の現在の日付
$ del $ Delstaff配列要素
$デルスタッフ 終了のためのユーザーデータ
$部門 部門
$ dir 1CがCSVをアンロードする場所
ゲット $ Getstaff配列要素
$ getstaff 雇用のためのユーザーデータ
$グループ レポートのコピーされたグループのリスト
$移動 $ Movestaff配列要素
$移動スタッフ フレーム移動のユーザーデータ
$ newusername
$ newusersurname
$ポジション 役職
$ sdel 区別された$ del
$ sget 識別$ get
$ smove 特別な$の動き
$シャブロン SamAccountNameユーザーテンプレート
$ spisokf 1Cからのアップロードディレクトリ内のファイルのリスト
$本日 今日の人事異動を含むファイル
$ usrcopyfrom ユーザーテンプレート
$ usrexist 存在を確認するためのユーザーログイン
$ usrpath ADのユーザーの場所
$ usrprop テンプレートユーザープロパティ
$ usrdomain あなたのドメイン
$セッション Exchange管理コンソールとのセッション
$ usrshare NASのユーザーフォルダーアドレス
$パス ユーザー仮パスワード
$会社 会社名
$ usrstate 会社の所在地
$ usrCity 市区町村
$ usrStreetAddress ストリートハウスビル
$ curuserpath ADの現在のローミングユーザーの場所
$ newuserpath ADのローミングユーザーターゲット
$ usrmoveddep 転送順序の重複を確認する現在のユーザー部門
$ usrmovedtit 転送順序の重複をチェックするためのユーザーの現在の位置


$ get | $ move | $ del配列の要素

[0] 雇用/移動/解雇の種類
[1] SamAccountName
[2] 氏名
[3] 役職
[4] 部門




スクリプト自体:



#   $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 ',',2)[1] #-name $newusername=($FIO-split ' ')[1] #-Surname $newusersurname=($FIO-split ' ')[0] #-AccountPassword $pass="Qwerty01" #-company $company= Get-ADUser -Identity $usrcopyfrom -Properties Company | Select-Object -ExpandProperty company #-State $usrstate= Get-ADUser -Identity $usrcopyfrom -Properties State | Select-Object -ExpandProperty State #-City $usrCity= Get-ADUser -Identity $usrcopyfrom -Properties City | Select-Object -ExpandProperty City #-StreetAddress $usrStreetAddress= Get-ADUser -Identity $usrcopyfrom -Properties StreetAddress | Select-Object -ExpandProperty StreetAddress #  New-ADUser ` -name $FIO ` -AccountPassword (ConvertTo-SecureString $pass -AsPlainText -Force) ` -ChangePasswordAtLogon $true ` -Company $company ` -Department $department ` -Description $Position ` -DisplayName $FIO ` -Enabled $true ` -HomeDirectory $usrshare ` -HomeDrive "<b>   </b>" ` -Path $usrpath ` -PostalCode "<b>  </b>" ` -SamAccountName $usrname ` -UserPrincipalName "$usrname@$usrdomain" ` -State $usrstate ` -StreetAddress $usrStreetAddress ` -Surname $newusersurname ` -Title $Position ` -GivenName $newusername ` -City $usrCity ` -Office $department ` | out-null #   Start-Sleep -Seconds 10 #     $groups=Get-ADUser -Identity $usrcopyfrom -Properties memberof | Select-Object -ExpandProperty memberof|Add-ADGroupMember -Members $usrname -PassThru | Select-Object -ExpandProperty SamAccountName #      (       ) if(!(Test-Path -Path $usrshare )) { New-item $usrshare -type directory | out-null $acl = Get-Acl $usrshare $acl | Format-List $acl.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) $acl.SetAccessRuleProtection($true, $true) $rule = New-Object System.Security.AccessControl.FileSystemAccessRule ("$usrdomain\$usrname","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow") $acl.addAccessRule($rule) Set-Acl $usrshare $acl | out-null } else { $acl = Get-Acl $usrshare $acl | Format-List $acl.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) $acl.SetAccessRuleProtection($true, $true) $rule = New-Object System.Security.AccessControl.FileSystemAccessRule ("$usrdomain\$usrname","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow") $acl.addAccessRule($rule) Set-Acl $usrshare $acl | out-null } #   Enable-Mailbox "$usrname@$usrdomain" -Database "<b>   </b>" | out-null #    foreach ($group in $groups) {$smtpBody = $smtpBody +$group+"`n`n"} #    Send-MailMessage -From $smtpfrom -Subject $opertype -To $smtpto -Body " $FIO ($usrname) ""$Position""  ""$department"" .`n`n  :`n$smtpBody" -SmtpServer $smtpserv -Encoding $encoding #  $smtpBody="" } else { #    Send-MailMessage -From $smtpfrom -Subject $opertype -To $smtpto -Body " $FIO ($usrname)  ." -SmtpServer $smtpserv -Encoding $encoding } #     (Get-Content $dir$todayf) | Where-Object {$_ -notmatch $get} | Set-Content -Path $dir$todayf -Force } } #   if ($getstaff -ne $null) { #    #usrcreate #   Start-Sleep -Seconds 10 } else { Write-Host "   " } #  function usrmove () { ForEach ($move in $movestaff) { #  $smove=$move -split (";") $opertype = $smove[0] $usrname=$smove[1] $FIO=$smove[2] $Position=$smove[3] $department=$smove[4] #    $usrmoveddep =Get-ADUser -Identity $usrname -Properties Department |Select-Object -ExpandProperty Department $usrmovedtit =Get-ADUser -Identity $usrname -Properties title |Select-Object -ExpandProperty title if ($usrmoveddep -ne $department -or $usrmovedtit -ne $Position) { #    $shablon=Get-ADUser -filter { (physicalDeliveryOfficeName -like $department) -and (title -like $Position) -and (Enabled -eq $true) } | Select-Object -ExpandProperty SamAccountName $usrcopyfrom=$shablon[1] #     Set-ADUser -Identity $usrname -Department $department -Description $Position -Title $Position -Replace @{physicalDeliveryOfficeName = $department} #    $curuserpath= Get-ADUser -Identity $usrname |Select-Object -ExpandProperty DistinguishedName #    $usrprop= Get-ADUser -Identity $usrcopyfrom | Select-Object -ExpandProperty DistinguishedName $newusrpath= ($usrprop -split ',',2)[1] #  Move-ADObject -Identity $curuserpath -TargetPath $newusrpath #       ,   . $ConfirmPreference = "None" $ErrorActionPreference = "SilentlyContinue" #     ,  domain users Remove-ADPrincipalGroupMembership -Identity $usrname -MemberOf $(Get-ADPrincipalGroupMembership -Identity $usrname | Where-Object {$_.DistinguishedName -ne 'CN=Domain Users,CN=Users,DC=<b>domain</b>,DC=<b>local</b>'}) #     $groups=Get-ADUser -Identity $usrcopyfrom -Properties memberof | Select-Object -ExpandProperty memberof|Add-ADGroupMember -Members $usrname -PassThru | Select-Object -ExpandProperty SamAccountName #    foreach ($group in $groups) {$smtpBody = $smtpBody +$group+"`n`n"} #    Send-MailMessage -From $smtpfrom -Subject $opertype -To $smtpto -Body " $FIO ($usrname) ""$Position""  ""$department"" .`n`n  :`n$smtpBody" -SmtpServer $smtpserv -Encoding $encoding #  $smtpBody="" } else { #    Send-MailMessage -From $smtpfrom -Subject $opertype -To $smtpto -Body " $FIO ($usrname)    ""$Position""  ""$department""" -SmtpServer $smtpserv -Encoding $encoding } #     (Get-Content $dir$todayf) | Where-Object {$_ -notmatch $move} | Set-Content -Path $dir$todayf -Force } } #   if ($movestaff -ne $null) { #    #usrmove #   Start-Sleep -Seconds 10 } else { Write-Host "   " } # function usrdelete () { ForEach ($del in $delstaff) { #  $sdel=$del -split (";") $opertype = $sdel[0] $usrname=$sdel[1] $FIO=$sdel[2] $Position=$sdel[3] $department=$sdel[4] #    $usrdeleted =Get-ADUser -Identity $usrname | Where-Object {$_.Enabled -eq $false} if ($usrdeleted -eq $null) { #       ,   . $ConfirmPreference = "None" $ErrorActionPreference = "SilentlyContinue" #   Disable-Mailbox -Identity "$usrname@$usrdomain" -confirm:$false #     ,  domain users Remove-ADPrincipalGroupMembership -Identity $usrname -MemberOf $(Get-ADPrincipalGroupMembership -Identity $usrname | Where-Object {$_.DistinguishedName -ne 'CN=Domain Users,CN=Users,DC=<b>domain</b>,DC=<b>local</b>'}) #    $curuserpath = Get-ADUser -Identity $usrname |Select-Object -ExpandProperty DistinguishedName #      Move-ADObject -Identity $curuserpath -TargetPath "OU=USER_DISABLE,OU=TO_DELETE_USER_COMPUTER_GROUP_OU,DC=<b>domain</b>,DC=<b>local</b>" #   Disable-ADAccount $usrname #    Send-MailMessage -From $smtpfrom -Subject $opertype -To $smtpto -Body " $FIO ($usrname) ""$Position""  ""$department""   ." -SmtpServer $smtpserv -Encoding $encoding } else { #    Send-MailMessage -From $smtpfrom -Subject $opertype -To $smtpto -Body " $FIO ($usrname) ""$Position""  ""$department""   ." -SmtpServer $smtpserv -Encoding $encoding } #     (Get-Content $dir$todayf) | Where-Object {$_ -notmatch $del} | Set-Content -Path $dir$todayf -Force } } #   if ($delstaff -ne $null) { #    usrdelete #   Start-Sleep -Seconds 10 } else { Write-Host "   " }
      
      






All Articles