【问题标题】:Skip Empty CSV cells for Set-ADUser script跳过 Set-ADUser 脚本的空 CSV 单元格
【发布时间】:2021-12-03 21:30:58
【问题描述】:

我一直在努力寻找修复脚本的方法。对于 CSV 中有空单元格的任何用户,我都会不断收到“替换”错误。

示例:我们有一些用户没有手机。该用户的手机单元格将为空白。

我得到的“替换”只发生在不是每个字段都有值的用户身上。

我已经链接了下面 csv 列的图像。

我看过一些关于解决此问题的帖子,但似乎没有一个与我的情况相符。任何帮助将不胜感激。

谢谢,

$TestUsers = Import-Csv -Path C:\Users\test.admin\Documents\ADDummyTestUsers.csv


foreach( $user in $testusers )
{ Set-ADUser -Identity $user.sAMAccountName `
 -Title $user.Title `
 -Description $user.Title `
 -OfficePhone $user.TelephoneNumber `
 -MobilePhone $user.Mobile `
 -Fax $user.facismileTelephoneNumber `
 -StreetAddress $user.streetAddress `
 -City $user.city `
 -State $user.st `
 -PostalCode $user.postalCode `
}

csv columns

【问题讨论】:

  • 完全跳过用户?您能否提供包含和不包含空白单元格的 CSV 样本?
  • 如果 any 值为空或 sAMAccountName 为空,您是否要跳过?
  • 什么是“替换”错误?可以发这样的错误信息吗?
  • 请分享示例 csv 以及您收到的错误消息。
  • 请以纯文本形式发布包含字段的csv

标签: powershell csv active-directory


【解决方案1】:

在这种情况下,您可以在Set-ADUser 上使用备用-Instance

$TestUsers = Import-Csv -Path C:\Users\test.admin\Documents\ADDummyTestUsers.csv

foreach( $user in $testusers )
{
    $ADUser = Get-ADUser -Identity $user.sAMAccountName

    $ADUser.Title = $user.Title 
    $ADUser.Description = $user.Title 
    $ADUser.OfficePhone = $user.TelephoneNumber 
    $ADUser.MobilePhone = $user.Mobile 
    $ADUser.Fax = $user.facismileTelephoneNumber 
    $ADUser.StreetAddress = $user.streetAddress 
    $ADUser.City = $user.city 
    $ADUser.State = $user.st 
    $ADUser.PostalCode = $user.postalCode 

    Set-ADUser -Instance $ADUser
}

【讨论】:

    【解决方案2】:

    OP 所指的“替换”错误类似于:

    Set-ADUser : 替换 At line:15 char:5

    • Set-ADUser -Instance $User
      
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      • CategoryInfo : InvalidOperation: (:) [Set-ADUser], ADInvalidOperationException
      • FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

    我建议在尝试设置之前简单地测试一个值。我已经在下面演示了这一点,同时也稍微改变了方法(使用实例)

    $TestUsers = Import-Csv -Path C:\tmp\file.csv
    
    $TestUsers | ForEach-Object {
        $User = $null
        $User = Get-ADUser -Identity $_.samaccountname -Properties description,telephonenumber,mobile,facsimiletelephoneNumber,streetaddress,city,st,postalcode
    
        if ('*' -like $_.description)              {$User.description              = $_.description}
        if ('*' -like $_.telephonenumber)          {$User.telephonenumber          = $_.telephonenumber}
        if ('*' -like $_.mobile)                   {$User.mobile                   = $_.mobile}
        if ('*' -like $_.facsimiletelephoneNumber) {$User.facsimiletelephoneNumber = $_.facsimiletelephoneNumber}
        if ('*' -like $_.streetaddress)            {$User.streetaddress            = $_.streetaddress}
        if ('*' -like $_.city)                     {$User.city                     = $_.city}
        if ('*' -like $_.st)                       {$User.st                       = $_.st}
        if ('*' -like $_.postalcode)               {$User.postalcode               = $_.postalcode}
    
        Set-ADUser -Instance $User
    }
    

    【讨论】:

      【解决方案3】:

      我将首先测试是否存在具有该 SamAccountName 的用户(如您所说,该字段在 CSV 文件中永远不会为空),如果是这样,则构建一个 Splatting Hashtable,其属性不为空或只有空格。
      通过将参数添加到 Set-ADUser cmdlet,您还可以摆脱代码中那些讨厌的反引号:

      $testusers = Import-Csv -Path 'C:\Users\test.admin\Documents\ADDummyTestUsers.csv'
      
      foreach ($user in $testusers) { 
          $ADUser = Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'" -ErrorAction SilentlyContinue
          if (!$ADUser) {
              Write-Warning "User $($user.sAMAccountName) does not exist"
              continue   # skip this one and proceed with the next
          }
          # create a hashtable for splatting
          $splat = @{}
          # only add fields that have content
          if (![string]::IsNullOrWhiteSpace($user.Title)) {
              $splat['Title'] = $user.Title
              $splat['Description'] = $user.Title
          }
          if (![string]::IsNullOrWhiteSpace($user.TelephoneNumber)) { $splat['OfficePhone'] = $user.TelephoneNumber }
          if (![string]::IsNullOrWhiteSpace($user.Mobile)) { $splat['MobilePhone'] = $user.Mobile }
          if (![string]::IsNullOrWhiteSpace($user.facismileTelephoneNumber)) { $splat['Fax'] = $user.facismileTelephoneNumber }
          if (![string]::IsNullOrWhiteSpace($user.streetAddress)) { $splat['StreetAddress'] = $user.streetAddress }
          if (![string]::IsNullOrWhiteSpace($user.city)) { $splat['City'] = $user.city }
          if (![string]::IsNullOrWhiteSpace($user.st)) { $splat['State'] = $user.st }
          if (![string]::IsNullOrWhiteSpace($user.postalCode)) { $splat['PostalCode'] = $user.postalCode }
      
          # now set the properties on the user
          $ADUser | Set-ADUser @splat
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 2021-03-16
        相关资源
        最近更新 更多