从Excel创建AD用户

时间:2016-04-11 01:03:57

标签: excel powershell active-directory

我正在尝试使用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.这些是登录名,通常在分配之前设置为禁用。

现在行动部分:

  1. 阅读excel并将第1列和第1列合并第2列,然后检查AD是否存在此用户(检查是否与显示名称匹配而不是SamAccountName。读取(2.1))对位置OU(在第3列中提供)。 不需要在整个AD中对其他OU中的用户进行递归搜索。
  2. 如果以上情况正常,则从“UnUsed”OU中选取最小的可用数字,例如在这种情况下,它将是510700。
    1. 更改此用户510700的名字,姓氏,职务名称,但登录名称与510700保持一致(用户在“未使用”OU中)。
    2. 更改显示名称,因为在这种情况下将成为“Andrew Smiles(510700)”
    3. 现在将此用户510700移至第3列(位置OU)中提供的OU并启用该帐户。
    4. 为上述所有程序撰写日志报告。
  3. ---------感谢您的关注,我能够自己解决这个问题-----如果有人有兴趣,这是代码问题-----------

    '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']);
      };
    

1 个答案:

答案 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()