从Excel创建广告用户
我正试图通过PowerShell脚本实现这一点,希望有人能帮助我实现这一点 我有一张excel表格,其中包含第1列(名字)、第2列(姓氏)、第3列(位置是广告中的OU)、第4列(角色是广告中的职务) ---------感谢大家的关注,我能够自己解决这一切------这是代码,以防有人感兴趣-----------从Excel创建广告用户,excel,powershell,active-directory,Excel,Powershell,Active Directory,我正试图通过PowerShell脚本实现这一点,希望有人能帮助我实现这一点 我有一张excel表格,其中包含第1列(名字)、第2列(姓氏)、第3列(位置是广告中的OU)、第4列(角色是广告中的职务) ---------感谢大家的关注,我能够自己解决这一切------这是代码,以防有人感兴趣----------- 这不是一个代码编写服务。试着自己写脚本,如果你遇到了一个特定的问题,请回来。请把你的问题包括在这些细节中-评论不是发布代码的好方法。 FirstName LastName Loc
这不是一个代码编写服务。试着自己写脚本,如果你遇到了一个特定的问题,请回来。请把你的问题包括在这些细节中-评论不是发布代码的好方法。 FirstName LastName Location(OU) Role(JobTitle) Andrew Smiles Perth ISS
$file = "C:\Temp\Book1.xlsx"
$sheetName = "Sheet1"
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
$rowMax = ($sheet.UsedRange.Rows).count
$rowFName,$colFName = 1,1
$rowLName,$colLName = 1,3
$rowLocation,$colLocation = 1,6
$rowRole,$colRole = 1,7
$rowTotal = $rowMax-1
Write-Output ("Total Number of Records in the EXCEL Sheet are: "+$rowTotal ) >> "C:\Temp\Output.txt"
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=Unused Users,OU=MYOU,DC=MYDC,DC=MYDOMAIN,DC=COM,DC=au" -server MYAD -ResultSetSize 10000 |
Select-Object Name | Sort Name | Out-File C:\Temp\UnUsedUsersList.txt
$content = Get-Content C:\Temp\UnUsedUsersList.txt
$content | Foreach {$_.TrimEnd()} | Set-Content C:\Temp\UnUsedUsersList.txt
[int]$Skip = 3
for ($i=1; $i -le $rowMax-1; $i++)
{
$FName = $sheet.Cells.Item($rowFName+$i,$colFName).text #Get first Column i.e First Name
$LName = $sheet.Cells.Item($rowLName+$i,$colLName).text #Get 3rd Column i.e Last Name
$Name = "$FName "+$LName #Combine the 2 Columns to complete Full Name
$OULocation = $sheet.Cells.Item($rowLocation+$i,$colLocation).text #Get OU Column of the user
$Role = $sheet.Cells.Item($rowRole+$i,$colRole).text #Get Title Column
$UserID = Get-Content "C:\Temp\UnUsedUsersList.txt" | select -skip $Skip | select -First 1
Write-Output ("User Account: " +$Name + " in OU: " +$OULocation + " will be assigned to: " +$UserID + " having Title as: " +$Role ) >> "C:\Temp\Output.txt"
$ADObject = Get-ADUser -Filter {(givenname -eq $FName) -and (sn -eq $LName)} -SearchBase "OU=$OULocation,OU=MYOU,DC=MYDC,DC=MYDOMAIN,DC=COM,DC=au" -server MYAD -ResultSetSize 10000
if ($ADObject)
{
Write-Output ($Name + " EXIST in OU: " +$OULocation) >> "C:\Temp\Output.txt"
}
else
{
$DisplayName = "$Name ($UserID)"
Write-Output ($Name + " DOES NOT exist in OU: " +$OULocation) >> "C:\Temp\Output.txt"
Get-ADUser $UserID | Set-ADAccountPassword $UserID -reset -newpassword (ConvertTo-SecureString 'welcome01' -AsPlainText -Force) |
Set-ADUser -Replace @{GivenName="$FName";DisplayName="$DisplayName";SN="$LName";} -Title $Role -PhysicalDeliveryOfficeName $OULocation -ChangePasswordAtLogon $true -Enabled $true |
Move-ADObject -TargetPath "OU=$OULocation,OU=MYOU,DC=MYDC,DC=MYDOMAIN,DC=COM,DC=au" -server MYAD
}
$Skip++
}
$objExcel.quit()
$file = "C:\Temp\Book1.xlsx"
$sheetName = "Sheet1"
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
$rowMax = ($sheet.UsedRange.Rows).count
$rowFName,$colFName = 1,1
$rowLName,$colLName = 1,3
$rowLocation,$colLocation = 1,6
$rowRole,$colRole = 1,7
$rowTotal = $rowMax-1
Write-Output ("Total Number of Records in the EXCEL Sheet are: "+$rowTotal ) >> "C:\Temp\Output.txt"
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=Unused Users,OU=MYOU,DC=MYDC,DC=MYDOMAIN,DC=COM,DC=au" -server MYAD -ResultSetSize 10000 |
Select-Object Name | Sort Name | Out-File C:\Temp\UnUsedUsersList.txt
$content = Get-Content C:\Temp\UnUsedUsersList.txt
$content | Foreach {$_.TrimEnd()} | Set-Content C:\Temp\UnUsedUsersList.txt
[int]$Skip = 3
for ($i=1; $i -le $rowMax-1; $i++)
{
$FName = $sheet.Cells.Item($rowFName+$i,$colFName).text #Get first Column i.e First Name
$LName = $sheet.Cells.Item($rowLName+$i,$colLName).text #Get 3rd Column i.e Last Name
$Name = "$FName "+$LName #Combine the 2 Columns to complete Full Name
$OULocation = $sheet.Cells.Item($rowLocation+$i,$colLocation).text #Get OU Column of the user
$Role = $sheet.Cells.Item($rowRole+$i,$colRole).text #Get Title Column
$UserID = Get-Content "C:\Temp\UnUsedUsersList.txt" | select -skip $Skip | select -First 1
Write-Output ("User Account: " +$Name + " in OU: " +$OULocation + " will be assigned to: " +$UserID + " having Title as: " +$Role ) >> "C:\Temp\Output.txt"
$ADObject = Get-ADUser -Filter {(givenname -eq $FName) -and (sn -eq $LName)} -SearchBase "OU=$OULocation,OU=MYOU,DC=MYDC,DC=MYDOMAIN,DC=COM,DC=au" -server MYAD -ResultSetSize 10000
if ($ADObject)
{
Write-Output ($Name + " EXIST in OU: " +$OULocation) >> "C:\Temp\Output.txt"
}
else
{
$DisplayName = "$Name ($UserID)"
Write-Output ($Name + " DOES NOT exist in OU: " +$OULocation) >> "C:\Temp\Output.txt"
Get-ADUser $UserID | Set-ADAccountPassword $UserID -reset -newpassword (ConvertTo-SecureString 'welcome01' -AsPlainText -Force) |
Set-ADUser -Replace @{GivenName="$FName";DisplayName="$DisplayName";SN="$LName";} -Title $Role -PhysicalDeliveryOfficeName $OULocation -ChangePasswordAtLogon $true -Enabled $true |
Move-ADObject -TargetPath "OU=$OULocation,OU=MYOU,DC=MYDC,DC=MYDOMAIN,DC=COM,DC=au" -server MYAD
}
$Skip++
}
$objExcel.quit()