I think I'm doing ForEach wrong
Hello everyone! I could use a little help with a script I'm trying to set up, I think I'm doing my ForEach loop incorrectly, but Powershell doesn't spit out any errors so I'm not sure where to start looking.
I've got two things we want to do, working from the same CSV file:
-
Take the list of Users, and change their password to the one specified in the CSV file.
-
Take a list of computers, and run a remote command to change a registry key, using the password from the CSV file.
The CSV file itself is set up something like this (generic info):
| Computers | Users | Password |
|---|---|---|
| Computer1 | User1 | Password1 |
| Computer2 | User2 | Password2 |
For the first task, here's the script we've built so far:
For the second task (change registry key that stores the auto-login password), here's the script so far:
For both scripts, when they are run (in powershell as normal "admin", or as "domain admin") they just output the table from the CSV file, no error messages.
I suspect that I am doing forgetting something stupid in my ForEach loop, but I'm not quite sure what, it looks (to me) just like any other ForEach example I've seen.
For the second task, I'm starting to suspect that I might need to pass the variables ($Computer and $Password) to the "Remote Command" as an Argument? I'm not quite sure to accomplish that.
ANY ideas or direction is appreciated here!
Posted Apr 5, 2017, 3:54 PM UTC
For each loops are
foreach (<item> in <array>)you're calling arrays that don't exist:needs to be:
or something like that.
Yes, and OP--to expand on this, if you want to make your CSV in Excel, for instance, your column headers will be your properties. Example, columns are Size, Weight, Height, Color, and you import that such as $dogs = import-csv c:\doglist.csv. As you iterate through each dog in your list using the foreach loop (e.g. foreach($dog in $dogs), you'd reference the $dog.size, $dog.weight, etc.
Awesome. I think i understand what I was missing before. I was assuming that i just Import-CSV, and that Powershell for the rest of that "instance" would just refer back to it whenever I reference a variable.
So theoretically, I could bring in two CSV files, like this:
$CSV1 = Import-CSV C:\Temp\Test1.CSV
$CSV2 = Import-CSV C:\Temp\Test2.CSV
And then refer back to them each, pulling the Computer and Password from different sheets like this:
Invoke-Command -ComputerName $Computer.CSV1 -ScriptBlock {reg add "hklm\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d $Password.CSV2 /f}
Are you allowed to pull info from two sheets like that, or am I getting this wrong?
You have your variables and properties mixed up.
$CSV1/$CSV2are the variables, and.Computer/.Passwordare properties of those respective variables. It would actually read as$CSV1.Computerand$CSV2.Passwordif you were to go that route.In the case of what you are doing, it makes more sense to have 1 CSV file as you have 1 for 1 mapping of computer-user-password, whereas if you split them apart you need a way to tell the script what entry in $CSV1 matches up to a partner entry in $CSV2.
With the
foreachloop you are essentially telling the script to iterate through each entry of your $CSV variable and run whatever set of commands for each entry of that variable. Ideally, assuming your CSV has column headers forComputerNameandPasswordyour loop should look like this:Also a little bit more advanced but
$Using:is needed any time you are trying to useinvoke-commandand passing a variable to the remote session, because your variables don't exist on the remote server.$Using:tells it to pass the variable over to the remote session.Hmm, you're right, I had that backwards. It makes sense that you would list it out as $MainThing.SubThing when I think about it.
I'm currently using one CSV file to pull in whatever I need, I just wanted to make sure that you could potentially pull in more than one file. I wasn't sure if pulling in a second file would "replace" the previous one.
As long as you give it a different $Nickname, it should be fine right?
Correct, you can create an infinite number of variables so long as they each have their own unique name.