Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in /var/www/powershellpower.com/public_html/page_parts/page.php on line 78

Warning: Use of undefined constant page_id - assumed 'page_id' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 92

Warning: Use of undefined constant synopsis - assumed 'synopsis' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 93

How to inject new values into a variable

In this tutorial we will show you how to take any values from other sources, variables and inject them into a new Powershell object or variable.

3
Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in /var/www/powershellpower.com/public_html/page_parts/page.php on line 156

Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Create an empty array variable

First we need to create an empty variable, we will use this later to create our new Powershell object


$people = @()



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Create an empty hash table

We also need an empty hash table. Notice for an array we use ( ) and for a hash table we use { }


$person = @{}



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Create headers with data

Next is the important bit. We create the headers for our new Powershell object by putting a dot at the end of our variable followed by what we would like to call the header and finally giving it some data. In my example I am simply adding some text but you can take the data from any source you like.


$person.firstname = "Reelfunky"
$person.surname = "Solutions"



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Transfer data in a powershell object

Now we move the data into a new Powershell object. We will be injecting the data from our hash table into this object and then injecting the data from there into the array we made in step one. The reason we do this is so that we can reuse the hash table and new object to collect more data and add this into the final object as much as we need to.


$newperson = New-Object PSobject -Property $person



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Populate the final variable

The final part is to inject everything we have into the array we made in step one. Please note Powershell will have automatically converted this from an array to a Powershell object for us. To append an object in Powershell we use the += function.


$people += $newperson



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
The full script

### Create an empty variable ###
$people = @()

### Create an empty hash table###
$person = @{}

### Create the values that will be injected into the new powershell object ###
$person.firstname = "Reelfunky"
$person.surname = "Solutions"

### Create a new powershell object ###
$newperson = New-Object PSobject -Property $person


### Create an empty variable ###
$people = @()
### Create an empty hash table###
$person = @{}
### Create the values that will be injected into the new powershell object ###
$person.firstname = "Reelfunky"
$person.surname = "Solutions"
### Create a new powershell object ###
$newperson = New-Object PSobject -Property $person


Comments

Notice: Undefined variable: commentno in /var/www/powershellpower.com/public_html/page_parts/page.php on line 335
// Collect comments ''
Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in /var/www/powershellpower.com/public_html/page_parts/comments.php:9 Stack trace: #0 /var/www/powershellpower.com/public_html/page_parts/page.php(343): include() #1 /var/www/powershellpower.com/public_html/pages/how_to_inject_new_values_into_a_variable.php(2): include('/var/www/powers...') #2 {main} thrown in /var/www/powershellpower.com/public_html/page_parts/comments.php on line 9