Created
April 15, 2024 09:23
-
-
Save YuukiNishikido/0926fa459b6497d5ecfaa44323e511fb to your computer and use it in GitHub Desktop.
読み込んだCSVの値に応じてActiveDirectory上にユーザーを作成するPowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 読み込むCSVのパスをセット | |
$csvPath = "C:\ADユーザー編集ツール\新規ユーザー作成\Source.csv" | |
# CSVファイルをUTF-8にエンコード(UTF-8にしないと日本語が文字化けする) | |
(Get-Content -Path $csvPath -Encoding default) | Set-content -Path $csvPath -Encoding UTF8 | |
# CSVファイルからデータを取り込み | |
$users = Import-Csv -Path $csvPath | |
# ADにユーザーを作成する | |
foreach ($user in $users) { | |
$flag=$user.flag | |
#CSVのフラグに応じてユーザーを作成するOUを変更 | |
IF($flag -eq 1){ | |
$Path = "OU=ユーザ,OU=契約社員,DC=hogehoge,DC=com" #ユーザーを作成するOUを指定(契約社員) | |
}else{ | |
$Path = "OU=ユーザ,OU=正社員,DC=hogehoge,DC=com" #ユーザーを作成するOUを指定(正社員) | |
} | |
$newUserParams = @{ | |
SamAccountName = $user.AccountName #ログオンアカウント | |
Name = $user.Name #ADツリー上の表示名 | |
displayName = $user.DisplayName #ユーザーログオン時の表示名 | |
UserPrincipalName = $user.AccountName + "@hogehoge.com" | |
AccountPassword = ConvertTo-SecureString $user.password -AsPlainText -Force | |
scriptPath = $user.script | |
ChangePasswordAtLogon = $True #初回ログイン時パスワード変更必要 | |
Enabled = $True | |
path = $Path #ユーザーを作成するOU | |
} | |
$newUser = New-ADUser @newUserParams -OtherAttributes @{info=$user.memo} #社員番号は電話のメモ欄 | |
# セキュリティグループをセット | |
$groupColumns = "group1","group2","group3","group4","group5" | |
foreach ($groupColumn in $groupColumns) { | |
$groupName = $user.$groupColumn | |
if (-not [string]::IsNullOrWhiteSpace($groupName)) { | |
Add-ADGroupMember -Identity $groupName -Members $user.AccountName | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment