首页 > 解决方案 > 一个相当简单的脚本,但看不到这里有什么问题

问题描述

我编写了一个相当简单的脚本,通过 UserPrinsipalName 简单地检索用户。它失败并出现以下错误:

Get-AdUser : Property: 'UserPrincipalName' not found in object of type: 'System.Management.Automation.PSCustomObject'.
At C:\Temp\changeUPN.ps1:8 char:16
+ ... serObject = Get-AdUser -Filter {UserPrincipalName -eq $Obj.UserPrinci ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
$Csv = Read-Host "Enter Path to the Csv file"
$sps = Import-csv -Path $Csv
Foreach ($Obj in $Sps) {
    Write-Host "Splitting UPN $($Obj.UserPrincipalName) to retreive domain information" -ForegroundColor Green
    $Domain = ($Obj.UserPrincipalName -split "@")[1]
    Write-Host "$($Obj.UserPrincipalName) is located in $($Domain)" -ForegroundColor Green
    Write-Host "Getting AD Object" -ForegroundColor Green
    $UserObject = Get-AdUser -Filter {UserPrincipalName -eq "$Obj.UserPrincipalName"} -Server $Domain
    If ($UserObject) {
        Write-Host "UserObject $($UserObject.DistinguishedName) Retrieved" -ForegroundColor Green
        Write-Host "Changing UPN" -ForegroundColor Green
        $NewUpn = $Obj.UserPrincipalName -Replace ("$Domain", "Contoso.com")
        Write-Host "New UserPrincipalName = $($NewUpn)" -ForegroundColor Green
        $UserObject | Set-AdUser -UserPrincipalName $NewUpn
    }
    Else {
        Write-Host "Could not retrieve $($Obj.UserPrincipalName) from $($Domain)" -ForegroundColor Yellow
    }
}

标签: powershell

解决方案


推荐阅读