0

我编写了一个脚本来更新 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 命令行运行脚本,它就不起作用。

我在这里想念什么?

4

1 回答 1

0

Creating command strings and then executing them is going to lead to bad practices I think. You will have to resort to Invoke-Expression. That is something we want to avoid. I would build a hash table with your parameters and use splatting. From that, you can build your command string to send to the information stream. See below for an example.

if ($user) {
    $command = "Set-AzureADUser"
    $params = @{}
    $params.ObjectID = $user.objectid
    if ($CSVrecord.title) {$params.jobtitle = $CSVrecord.title}
    if ($CSVrecord.department) {$params.department = $CSVrecord.department}
    if ($CSVrecord.office) {$params.PhysicalDeliveryOfficeName = $CSVrecord.office}
    if ($CSVrecord.officephone) {$params.TelephoneNumber = $CSVrecord.officephone}
    if ($CSVrecord.fax) {$params.FacsimileTelephoneNumber = $CSVrecord.fax}
    if ($CSVrecord.mobilephone) {$params.Mobile = $CSVrecord.mobilephone}
    if ($CSVrecord.streetaddress) {$params.streetaddress = $CSVrecord.streetaddress}
    if ($CSVrecord.city) {$params.city = $CSVrecord.city}
    if ($CSVrecord.state) {$params.state = $CSVrecord.state}
    if ($CSVrecord.postalcode) {$params.postalcode = $CSVrecord.postalcode}
    Write-Information "$command $($params.GetEnumerator() |% {"-{0} '{1}'" -f $_.Key,$_.Value})"
    & $command @params
}
于 2020-09-11T21:35:34.203 回答