我编写了一个脚本来更新 Azure AD 中的用户联系信息。我使用的 CSV 是从我们本地 AD 导出的。我找到了一些示例作为起点,这就是我破解的...
Start-Transcript "transcript.log"
# Connect to AzureAD
Connect-AzureAD
# Get CSV content
$CSVrecords = Import-Csv userexport.csv -Delimiter ","
# Create arrays for skipped and failed users
$SkippedUsers = @()
$FailedUsers = @()
# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
$upn = $CSVrecord.samaccountname + "@daytonrogers.com"
$user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"
if ($user) {
$command = "Set-AzureADUser -ObjectID $($user.objectid) "
if ($CSVrecord.title) {$command = "$command -jobtitle '$($CSVrecord.title)'"}
if ($CSVrecord.department) {$command = "$command -department '$($CSVrecord.department)'"}
if ($CSVrecord.office) {$command = "$command -PhysicalDeliveryOfficeName '$($CSVrecord.office)'"}
if ($CSVrecord.officephone) {$command = "$command -TelephoneNumber '$($CSVrecord.officephone)'"}
if ($CSVrecord.fax) {$command = "$command -FacsimileTelephoneNumber '$($CSVrecord.fax)'"}
if ($CSVrecord.mobilephone) {$command = "$command -Mobile '$($CSVrecord.mobilephone)'"}
if ($CSVrecord.streetaddress) {$command = "$command -streetaddress '$($CSVrecord.streetaddress)'"}
if ($CSVrecord.city) {$command = "$command -city '$($CSVrecord.city)'"}
if ($CSVrecord.state) {$command = "$command -state '$($CSVrecord.state)'"}
if ($CSVrecord.postalcode) {$command = "$command -postalcode '$($CSVrecord.postalcode)'"}
Write-Information $command
try{
$command
} catch {
$FailedUsers += $upn
Write-Warning "$upn user found, but FAILED to update."
}
}
else {
Write-Warning "$upn not found, skipped"
$SkippedUsers += $upn
}
}
Stop-Transcript
它运行并构建命令就好了。但是,没有一个用户得到更新。如果我从 transcript.log 文件复制/过去命令,它就可以工作。如果我从 PowerShell 命令行运行脚本,它就不起作用。
我在这里想念什么?