powershell - 一个相当简单的脚本,但看不到这里有什么问题
问题描述
我编写了一个相当简单的脚本,通过 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
}
}
解决方案
推荐阅读
- oracle - 如何在 sql 中不使用 union、union all 或 minus 函数从 sql 中的表中查找最后 500 条记录。?
- sql - 在 SQL 动态视图中是可能的吗?
- reactjs - 如何在 Input 组件中使用 Formik
- c++ - 从 C++ 中的 .txt 文件中获取变量值
- python - 发生某种情况时停止抓取下一页
- css - 如何在动画中“跳跃”单个变换值?
- java - 刷新 JPanel 以显示 JList 中添加的组件
- php - DropZoneJS 库将多个图像上传到 PHP MySQL 数据库单行,用逗号(,)分隔值
- go - `range` 返回的迭代值的数据类型是什么?
- python - 在文本文件中垂直打印 Python 列表