Using output from Foreach Loop for Variables

Hey if anyone can help it would be gratefully appreciated.

I am trying to pass the outputs of file into Update-Ownership command below. to date if will not accept the values.

I have added the write-output only for verification that it is reading the file correctly, which it is as it outputs the right data.

I would prefer to just upload or copy and paste server and user list but this is stage 1. standalone command also works fine. Do I need to create a second for loop ? I have tried to pass outputs to different variables also.

Any Help much appreciated.

$Database = import-csv Database.txt
 foreach ($Data in $Database)
 {
 $servername = $Data.server
 $assignuser = $Data.user

 write-output $servername
 write-output $assignuser

 Update-UserOwnership -Machine_id (Get-DesktopVM -Name "$servername").machine_id -Sid (Get-User -Name "$assignuser").sid
 }

July 27th, 2015 10:33am

Are you getting an error? If so, what is it?

I'd skip the foreach loop and use a ForEach-Object loop instead, but that's just my personal preference.

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 10:39am

Hello,

can you assign the machineID and UserID to some variables and check if the data is retrieved?

$machineID = (Get-DesktopVM -Name "$servername").machine_id 
$userID = (Get-User -Name "$assignuser").sid
$machineID
$userID
Could you post error message that you are getting.

Regards,

July 28th, 2015 5:16am

Hi,

is this what you are looking for?

$Database = import-csv Database.csv -Delimiter ';'
 foreach ($Data in $Database)
 {
 Update-UserOwnership -Machine_id (Get-DesktopVM -Name "$data.Server").machine_id -Sid (Get-User -Name "$data.User").sid`
 | ConvertTo-Csv -Delimiter ';' -NoTypeInformation | out-file database2.csv -Append
 }

IF CSV is not good for you and you want to put it into variable, use $database2 = foreach (here is the rest of the comma

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 7:58am

You cannot put quotes around these: -Name "$data.Server".  It will break them.  The quotes are not needed.

July 29th, 2015 8:10am

import-csv Database.txt  :-)
Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 8:14am

Ok - enough with the wild guesses.  If the post was clear I am sure there would be no guessing.

What is it that is not working.  Why are you writing so much code that is prone to failure wwhen you are not sure how to write Powershell to beginwith. Start simple.

This would be an easier start:

$Database = import-csv Database.txt
foreach($Data in $Database){
  $id=(Get-DesktopVM -Name $Data.server).machine_id 
  $usersid=(Get-User -Name  $Data.user).sid
  Update-UserOwnership -Machine_id $id -Sid $usersid
}

This would be easier to debug and avoids all of the failures of adding quotes around everything that looks scary.  If there are errors then you will have to trap for them and see what is bad in your file.

P.S.  - adding semi-colons and changing file extensions will not solve this issue as OP already has printed the individual file values and states they are correct.  Adding quotes and moving variables around will not fix this.

July 29th, 2015 8:26am

You cannot put quotes around these: -Name "$data.Server".  It will break them.  The quotes are not needed.

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 8:39am

You cannot put quotes around these: -Name "$data.Server".  It will break them.  The quotes are not needed.

July 29th, 2015 8:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics