Author Posts

October 27, 2016 at 5:55 pm

Hello everyone, hopefully I can describe this correctly, as I'm new to PowerShell and learning on the run here.

So I have a script, that pulls from a .csv file. The file in its first column is usernames(User_Name). The second column is IP address(Framed_IP_Address).

So the script should populate the Notes field in their AD account, Telephone tab, with an IP address. Here's the script:

Import-module ActiveDirectory
$userlist=Import-Csv C:\PS\newIP2.csv
foreach ($user in $userlist) {write-host "Set-ADUser -Identity $($user.User_Name) -Replace @{info=`"$($user.Framed_IP_Address)`"}"}

When its run, it will show on the console:

Set-ADUser -Identity ExampleUserName -Replace @{info="xxx.xxx.xxx.xxx"}
Set-ADUser -Identity ExampleUserName -Replace @{info="xxx.xxx.xxx.xxx"}

So the output looks correct.... but the Notes field isn't populated. Now whats weird is, if I run each of the above commands on their own, just copy/paste and run each individually it will work. The IP will end up in the Notes field.

Any ideas?

Thank you!

October 27, 2016 at 6:41 pm

Your not running the command. Your just telling PowerShell to write "Set-ADUser -Identity ExampleUserName -Replace @{info="xxx.xxx.xxx.xxx"}"
Try something like:

Import-module ActiveDirectory
 $userlist=Import-Csv C:\PS\newIP2.csv
 foreach ($user in $userlist) {write-host "Starting to replace info" 
"Set-ADUser -Identity $($user.User_Name) -Replace @{info=`"$($user.Framed_IP_Address)`"}"}

What version of PS are you running?

October 27, 2016 at 8:05 pm

Version 5 from my client, but I've also tried running from a DC running version 4.

Thanks for the tip, tried it. Still not showing up in the AD account Notes field....

The output on the console looks like this now:

Starting to replace info
Set-ADUser -Identity ExampleName -Replace @{info="xxx.xxx.xxx.xx"}
Starting to replace info
Set-ADUser -Identity ExampleName -Replace @{info="xxx.xxx.xxx.xx"}

Ok, so I'm just telling Powershell to write the command.... how do I get it to execute the command?

Also I took out the write-host. I'm at:

Import-module ActiveDirectory
 $userlist=Import-Csv C:\Scripts\newIP2.csv
 foreach ($user in $userlist) 
 {"Set-ADUser -Identity $($user.User_Name) -Replace @{info=`"$($user.Framed_IP_Address)`"}"}

October 27, 2016 at 8:18 pm

I tested the script and it worked.

$userlist = Import-Csv C:\Test\testinfo.csv
foreach ($user in $userlist){
Write-Host "Setting info"
Set-ADUser -Identity $user.User_name -Replace @{info = $user.Framed_IP_Address}
}

October 27, 2016 at 8:22 pm

Remove the quotes around your command,they are turning it into a string.

Alternatively, you could replace white-hot in the first iteration of your code with invoke-expression, I suppose. Whichever you find easier.

October 27, 2016 at 10:45 pm

Thank you Wilfredo Perez and James Sudbury! Working now.