You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell新手求助:用AD扩展属性值替换配置文件指定内容

Hey there! Let's work through fixing your PowerShell script step by step since you're new to this—no worries, we'll get it sorted out.

Fixing Your Script to Update Config with AD User's othertelephone

Let's tackle each of your three issues one by one:

1. Extract just the othertelephone value (not an object)

Your current AD query returns a custom object with a property name and value. Instead, we can directly grab the phone number array, join it with semicolons, and store it as a plain string:

# Get the AD user's othertelephone values, join into a semicolon-separated string
$phoneNumbers = (Get-ADUser -Identity $env:username -Properties othertelephone -Server generic.com.au).othertelephone -join ";"

This gives you a simple string like "555-1234;555-6789" instead of an object with extra property metadata.

2. & 3. Precise config replacement (no manual old value needed)

Since you're working with an XML config file (your variable is named $XML), using XML parsing is far more reliable than plain text replacement—it avoids breaking the XML structure and lets you target exactly the right setting.

Here's how to safely update the specific setting:

# Define your config file paths
$originalConfig = "C:\Users\Test.User04\Documents\test.config"
$newConfig = "C:\Users\Test.User04\Documents\test2.config"

# Load the XML file into a document object
$xmlDoc = [xml](Get-Content $originalConfig)

# Target the exact setting (replace "TelephoneNumbers" with the actual key from your config's red box)
# Example: If your config has <add key="YourTargetKey" value="OLD_VALUE" />
$targetSetting = $xmlDoc.SelectSingleNode("//add[@key='TelephoneNumbers']")

# Update the value if we found the setting
if ($targetSetting) {
    $targetSetting.SetAttribute("value", $phoneNumbers)
    # Save the modified XML to the new file
    $xmlDoc.Save($newConfig)
    Write-Host "Config updated successfully! New value: $phoneNumbers"
} else {
    Write-Warning "Couldn't find the target setting in the config file—double-check the key name."
}

Plain Text Config Solution (If your config isn't XML)

If your config is a plain text file with lines like TelephoneNumbers=10586, use a regex to replace the value without specifying the old one:

(Get-Content $originalConfig) | ForEach-Object {
    # Replace everything after "TelephoneNumbers=" with our phone numbers
    $_ -replace '^(TelephoneNumbers=).*$', "`$1$phoneNumbers"
} | Set-Content $newConfig

The regex matches the entire line starting with your target key, keeps the key part, and replaces the old value with your phone number string.

Full Working Script (XML Version)

Putting it all together with error handling:

# Get AD user's othertelephone as semicolon-separated string
$phoneNumbers = (Get-ADUser -Identity $env:username -Properties othertelephone -Server generic.com.au).othertelephone -join ";"

# Config file paths
$originalConfig = "C:\Users\Test.User04\Documents\test.config"
$newConfig = "C:\Users\Test.User04\Documents\test2.config"

try {
    $xmlDoc = [xml](Get-Content $originalConfig -ErrorAction Stop)
    # Update "TelephoneNumbers" to match the exact key from your config's red box
    $targetSetting = $xmlDoc.SelectSingleNode("//add[@key='TelephoneNumbers']")

    if ($targetSetting) {
        $targetSetting.SetAttribute("value", $phoneNumbers)
        $xmlDoc.Save($newConfig)
        Write-Host "Success! Updated config with phone numbers: $phoneNumbers"
    } else {
        Write-Error "Target setting not found. Check the key name in your script matches the config."
    }
} catch {
    Write-Error "Error processing config file: $_"
}

Quick Notes:

  • Replace "TelephoneNumbers" in the SelectSingleNode call with the actual key name from your config's red box.
  • If your AD user has no othertelephone values, $phoneNumbers will be an empty string—add a check like if ([string]::IsNullOrEmpty($phoneNumbers)) { ... } if you want to handle that case.

内容的提问来源于stack exchange,提问作者ItsPhil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:02:55