Adding info to Notes field in AD account problem

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of TomS TomS 1 month ago.

  • Author
    Posts
  • #56275
    Profile photo of TomS
    TomS
    Participant

    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!

  • #56281
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    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?

  • #56293
    Profile photo of TomS
    TomS
    Participant

    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)`"}"}
  • #56296
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    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}
    }
    
  • #56299
    Profile photo of James Sudbury
    James Sudbury
    Participant

    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.

  • #56306
    Profile photo of TomS
    TomS
    Participant

    Thank you Wilfredo Perez and James Sudbury! Working now.

You must be logged in to reply to this topic.