我正在尝试运行一个导入 CSV 文件的 Powershell 脚本,然后循环遍历并更新 AzureAD 中的用户雇用日期。
# Connect to Azure AD
Connect-AzureAD
# Path to the CSV file containing user data
$csvPath = "C:\Users\#######\Downloads\20231218_hire_date.csv"
# Import CSV data
$userData = Import-Csv -Path $csvPath
# Iterate through each user in the CSV and update their hire date
foreach ($row in $userData) {
# Get the user by UPN (User Principal Name)
$userPrincipalName = $row.'UserPrincipalName'
$targetUser = Get-AzureADUser -Filter "userPrincipalName eq '$userPrincipalName'"
# Check if the user is found
if ($targetUser) {
# Update the user's hire date attribute
$hireDate = $row.'EmployeeHireDate'
Set-AzureADUser -ObjectId $targetUser.ObjectId -ExtensionProperty EmployeeHireDate $hireDate
Write-Host "Updated hire date for user $($user.UserPrincipalName) to $($user.EmployeeHireDate)"
} else {
Write-Host "User with UPN $($user.UserPrincipalName) not found."
}
}
我认为一切都设置正确,至少在我看来是这样,但我遇到了与数据类型有关的错误。
CSV 文件中的日期是否需要转换,或者脚本可以转换已包含的内容吗?
您遇到的错误可能是因为您传递的日期值不被接受。此外,验证名为
EmployeeHireDate的
ExtensionProperty
。
如果 EmployeeHireDate 作为属性存在,您可以更新该属性的日期值。如果不可用,请使用以下命令创建该属性。
$MyApp = (New-AzureADApplication -DisplayName "Employee join Date").ObjectId
New-AzureADServicePrincipal -AppId (Get-AzureADApplication -SearchString "Employee join Date").AppId
New-AzureADApplicationExtensionProperty -ObjectId $MyApp -Name "EmployeejoinDate" -DataType "String" -TargetObjects "User"
如果您尝试使用用户的创建日期属性更新
EmployeejoinDate
值,则可以使用以下命令。
$userjoindate = $targetUser.ExtensionProperty.createdDateTime
$dateFormat = "MM/dd/yyyy h:mm:ss tt"
$dateObject = [DateTime]::ParseExact($userjoindate, $dateFormat, [System.Globalization.CultureInfo]::InvariantCulture)
这是更新扩展属性值的 PowerShell 代码。
$csvPath = "/home/mindtree/venkat/exportUsers_2023-12-19.csv"
# Import CSV data
$userData = Import-Csv -Path $csvPath
# Iterate through each user in the CSV and update their hire date
foreach ($row in $userData) {
# Get the user by UPN (User Principal Name)
$userPrincipalName = $row.'UserPrincipalName'
$targetUser = Get-AzureADUser -Filter "userPrincipalName eq '$userPrincipalName'"
# Check if the user is found
if ($targetUser) {
# i update value from user creation date
$userjoindate = $targetUser.ExtensionProperty.createdDateTime
$dateFormat = "MM/dd/yyyy h:mm:ss tt"
$dateObject = [DateTime]::ParseExact($userjoindate, $dateFormat, [System.Globalization.CultureInfo]::InvariantCulture)
#For updating random date value
$dateObject = Get-Date "12/19/2023"
$dateString = $dateObject.ToString('MM-dd-yyyy')
----------------------------------------------
# Update the user's hire date attribute
Set-AzureADUserExtension -ObjectId $targetUser.ObjectId -ExtensionName "extension_c4874bb532d2436b80ea2c5da29b5a47_EmployeejoinDate" -ExtensionValue $dateObject
Write-Host "Updated hire date for user $($targetUser.UserPrincipalName)"
} else {
Write-Host "User with UPN $userPrincipalName not found."
}
}
执行代码后,用户的扩展属性值更新成功。
参考:创建新的扩展属性