How to read variables from file PowerShell

Hi i have a variable file: variable.ps1:

$Appserver1Box = "app11" $Webserver1Box = "web11" $Webserver2Box = "web22" $Webserver3Box = ""

From a a Powershell Program: PowershellProg.ps1 I am trying to get the above variables into my textboxes with:

get-content c:\variable.ps1

$Appserver1Box.Text = $Appserver1Box $Webserver1Box.Text = $Webserver1Box $Webserver2Box.Text = $Webserver2Box $Webserver3Box.Text = $Webserver3Box

However, all my textboxes get filled with: System.Windows.Forms.TextBox, Text: and comboboxes get filled with: System.Windows.Forms.ComboBox, Items.Count: 0

ok so i changed it to :

 .c:\variables.ps1 (instead of get-content c:\variables.ps1)

and can now get the variables by asking for $Appserver1Box: write-host "$Appserver1Box".

Thats fine. But when i try to insert them in my textboxes im presented with:

ERROR: Property 'Text' cannot be found on this object; make sure it exists and is settable.
testWcode8.pff (1461): ERROR: At Line: 1461 char: 1
ERROR: +     $Appserver1Box.Text = Appserver1Box
ERROR: +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : PropertyAssignmentException
ERROR:

any help will be appriciated!

br

Bjorn



July 16th, 2013 7:06am

You could dotsource the .ps1 file (like: . <filepath>.ps1).

But the major problem might be, that the names of your variables are identical to those of your TextBox objects. You should better choose different names for them.

Kind regards,

wizend

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2013 7:19am

Hi Wizend,
Thanks for your reply!
i just tried to dot source it, didnt help as a got an error:

ERROR: Property 'Text' cannot be found on this object; make sure it exists and is settable.
testWcode8.pff (1461): ERROR: At Line: 1461 char: 1
ERROR: +     $Appserver1Box.Text = Appserver1Box
ERROR: +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : PropertyAssignmentException
ERROR:

Maybe its the bad variable names as you say, ill try to change.

br

Bjorn

July 16th, 2013 7:42am

As I pointed out earlier, you mustn't use the same variable, e.g. '$Appserver1Box', for instantiating a TextBox object and at once for instantiating a string object. So, it comes that after dotsourcing your file the variable '$Appserver1Box', initially declaring a TextBox object, will get a different value and then containing a string value.

To solve your problem, you need to rename your string variables.

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2013 8:03am

Changing the variable name did the trick thank you!
July 16th, 2013 8:09am

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

Other recent topics Other recent topics