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.
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.
XML Config Solution (Recommended)
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 theSelectSingleNodecall with the actual key name from your config's red box. - If your AD user has no
othertelephonevalues,$phoneNumberswill be an empty string—add a check likeif ([string]::IsNullOrEmpty($phoneNumbers)) { ... }if you want to handle that case.
内容的提问来源于stack exchange,提问作者ItsPhil

