我正在尝试使用PowerShell脚本来实现这一点,并希望有人可以帮助我。
我有一张excel表,其中包含第1列(名字),第2列(姓氏),第3列(AD中的位置为OU),第4列(角色是AD中的职位)。
FirstName LastName Location(OU) Role(JobTitle) Andrew Smiles Perth ISS
在Active Directory中,我有一个包含用户的“UnUsed”OU,例如:510700,510701,510702,到519960.这些是登录名,通常在分配之前设置为禁用。
现在行动部分:
---------感谢您的关注,我能够自己解决这个问题-----如果有人有兴趣,这是代码问题-----------
'use strict';
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt);
// Define the configuration for all the tasks
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: {
src: [
'Gruntfile.js',
'app/scripts/{,*/}*.js'
]
}
},
copy: {
dist: {
cwd: 'app',
src: [ '**','!styles/**/*.css','!scripts/**/*.js' ],
dest: 'dist',
expand: true
},
fonts: {
files:[
{
//for bootstrap fonts
expand: true,
dot: true,
cwd: 'bower_components/bootstrap/dist',
src: ['fonts/*.*'],
dest: 'dist'
}, {
//for font-awesome
expand: true,
dot: true,
cwd: 'bower_components/font-awesome',
src: ['fonts/*.*'],
dest: 'dist'
}
]
}
},
clean: {
build:{
src: [ 'dist/']
}
}
});
grunt.registerTask('build', [
'clean',
'jshint',
'copy'
]);
grunt.registerTask('default',['build']);
};
答案 0 :(得分:0)
---------感谢您的关注,我能够自己解决这个问题-----如果有人有兴趣,这是代码问题-----------
$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()