6

I am trying to create a script that receives a list of computers and for-each computer I need to create a process, after running the process, I have many variables that y need to append to a an object

How can I append to the object, the returned info of the pcs?

#input variable that the foreach need to process
$Machines = "pc1,pc2,pc3,pc4,pc5,pc6,pc7,pc8,pc9,pc0"


#create empty object
$pcNull
$MachineNull
$usersNull    
$object= New-Object Object
$object | Add-Member NoteProperty propiedad $MachineNull
$object | Add-Member NoteProperty users $usersNull
$object | Add-Member NoteProperty computer $pcNull
$object | Add-Member NoteProperty error $false


foreach ($Machine in $Machines  )
{
 #where i make a process foreach computer and there are variables that are returned 
 {
 }
 ###############################
 #where i am trying to append to the created object the returned variables

 #append to propiedad property
 $object.propiedad = $object.propiedad = $MachineNull 
 $object
 #append to users property
 $object.users = $object.users = $false
 $object
 #append to computers property
 $object.computer = $object.computer = $Machine
 $object
}


$object
6
  • Can you actually include the code instead of pasting a picture. You already know how to make an object from the looks of it. Create each object inside the loop. Commented Nov 2, 2015 at 17:49
  • Stop linking to external files, paste the code inside the text box, select it and press the code formatting button ({ }) Commented Nov 2, 2015 at 17:57
  • I have uploaded it to one drive OK i will format the code Commented Nov 2, 2015 at 17:57
  • @kimopryvt Much better :) Are you sure you want to append values to the same object? Why not create multiple objects, eg. one for each computer? Commented Nov 2, 2015 at 18:10
  • I will use the object that is created to create a html report, when i am creating the html its easier to select the object like $object.computers and format it with the proper color or in the right <tr> o <td>. Commented Nov 2, 2015 at 18:16

1 Answer 1

6

You're probably better off creating multiple objects, one per computer, inside the foreach loop.

# Loop through machines, assign all output to $Objects variable
$Objects = foreach ($Machine in $Machines)
{
    #where i make a process foreach computer and there are variables that are returned 
    {
    }
    ###############################
    #where i am trying to append to the created object the returned variables

    # Define the properties that the object should have in a hashtable
    $ObjectProperties = @{
        # Assuming you've assigned something to $Propriedad, $Users and $ErrorState above
        Propiedad = $Propriedad
        Users     = $Users
        Computer  = $Machine
        Error     = $ErrorState
    }

    # Now create an object. 
    # When we just drop it in the pipeline like this, it gets assigned to $Objects
    New-Object psobject -Property $ObjectProperties
}

Now you can create HTML from your objects with ConvertTo-Html:

$Objects | ConvertTo-Html -As Table -Head "<title>Kimo's report</title>"

If you want to test it, you'll need to change $Machines to:

"pc1","pc2","pc3","pc4","pc5","pc6","pc7","pc8","pc9","pc0"

If you want to save this as a .ps1 script file and be able to pass the computer names as arguments, add a param() block at the top:

param([string[]]$Machines)

Now, if you save the script as "KimosReporter.ps1", you can run it against any computer like this:

PS C:\>.\KimosReporter.ps1 -Machines "pc1","pc6","pc9"
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, I was just writing a very similar answer. Simpler than creating an empty array beforehand, I've learned to do $Objects = ForEach($x in $y){...} and just output the object within the ForEach loop, or if you want other output as well you can do [array]$Objects = New-Object psobject -property $ObjectProperties and still skip creating an empty array first.
@TheMadTechnician Yes, $Objects = foreach($x in $y){...} is my own goto pattern, but I've heard a couple of newcomers complain that it doesn't read as easy as adding to an array inline. Only way to fix that I guess :D
Many thanks it works perfect :') I've been trying since Sunday to Do This :')
If this answer has resolved your problem please mark it as accepted (on the left side of the answer is a checkmark that you can click to do that). Mathias does strong work, and deserves credit for providing a good answer.

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.