0

I am working on a GUI in powershell. I need help writing a foreach loop that looks at the information in a DataGrid and creates 2 variables based on the input in the 2 columns of each row.

something like

for each row
  v1 = column1
  v2 = column2

  do something

then repeat. 

Actual code

Function RenameComputers{
foreach ($item in $DataGrid.Items){
        $OName = $item.OldName
        $NName = $item.NewName

        write-host "$OName and $NName"
        netdom renamecomputer $OName /newName:$NName /uD:$Username /passwordD:$Password /force /reboot
    }
    }
1
  • 3
    Sounds like a great plan, your pseudo code makes sense - what have you actually tried? Commented Feb 6, 2017 at 16:13

1 Answer 1

1
foreach ($item in $dataGrid.Items) {
    $oldname = $item.oldname
    $newname = $item.newname

    //Do stuff, you don't even need to create those variables you can just call $item.attribute
}

Then you can call the "columns" with $item.oldname or $item.newname etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I hadn't tried much. This is really just a learning experience for so i am looking for examples and how to interact with different objects. I have some experience with Java. So i understand the basics of programming/scripting and its logic. This is capturing the information the way i need it to, but doesn't appear to be running the command i need it to. I am going to copy the full function into the original post. I can't seem to get the comments to format correctly. If you have any ideas or suggestions that would be great! Thanks again. You have already been a big help.
I don't think write-host will ever work. It would be easier to append an existing file or create a text box and write to the text box. I believe it is because the console is in a different run space?
the write host command is just there for testing purposes. i used it to see if the function was grabbing the names from the data table correctly. The netdom command is what is not working. I have tried using Invoke-Expression as well with no luck. After the Write-Host command there is no further output or errors reported by the console and the machine never renames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.