3

Let me start with I am very new to powershell and programming for that matter. I have a powershell script that takes some arguments and that outputs a value. The result of the script is going to be something like 9/10 where 9 would be the number active out of the total amount of nodes. I want to assign the output to a variable so I can then call another script based on the value.

This is what I have tried, but it does not work:

$active = (./MyScript.ps1 lb uid **** site)  

I have also tried the following which seems to assign the variable an empty string

$active = (./MyScript.ps1 lb uid **** site | out-string)

In both cases they run and give me the value immediately instead of assigning it to the variable. When I call the variable, I get no data.

4
  • 1
    Show the script code, remember that Powershell trows objects in any statement except assignments and increments, so to be able to help you we(community) need to see what are you trowing Commented Oct 24, 2011 at 19:06
  • 1
    My guess would be that you are using Write-Host in the script. Commented Oct 24, 2011 at 19:11
  • I stumbled across another post that seems to jive with voodoomsr's comment. I will grab the script and post it. EBGreen You are also correct the output is generated by this line in the script Write-Host "$avail_count/$total_count" Commented Oct 24, 2011 at 19:21
  • I found the solution based on both your comments. The script was using Write-Host. After doing some searching I found this link. blog.usepowershell.com/2011/04/… In essence, if you use Write-Host it will simply write it to the console and you will not be able to operate on it. If you need to be able to do anything with the data it returns you should use Write-Output. Thanks for your help Commented Oct 24, 2011 at 19:40

4 Answers 4

6

I would embrace PowerShell's object-oriented nature and rather than output a string like "9/10", create an object with properties like NumActiveNodes and TotalNodes e.g. in your script output like so:

new-object psobject -Property @{NumActiveNodes = 9; TotalNodes = 10}

Of course, substitute in the dynamic values for num active and total nodes. Note that uncaptured objects will automatically appear on your script's output. Then, if this is your scripts only output, you can do this:

$obj = .\MyScript.ps1
$obj.NumActiveNodes
9
$obj.TotalNodes
10

It will make it nicer for those consuming the output of your script. In fact the output is somewhat self-documenting e.g.:

C:\PS> .\MyScript.ps1

NumActiveNodes        TotalNodes
--------------        ----------
             9        10

P.S. When did StackOverflow start sucking so badly at formatting PowerShell script?

Sign up to request clarification or add additional context in comments.

1 Comment

That is a really good thought. It isn't actually my script. It was written by the vendor in an unofficial capacity. I have been making small adjustments to it now to suit my needs. Will likely make those changes as I get better acquainted with powershell.
5

If you don't want to change the script ( and assuming only that $avail_count/$total_count line is written by the script), you can do:

$var= powershell .\MyScript.ps1

Or just drop the write-host and have just $avail_count/$total_count

and then do:

$var = .\MyScript.ps1

Comments

0

you could just do a $global:foobar in your script and it will persist after the script is closed

Comments

0

I know, the question is a bit older, but it might help someone to find the right answer.

I had the similar problem with executing PS script with another PS script and saving the output into variable, here are 2 VERY good answers:

  1. Mathias
  2. mklement0

Hope it helps!

Please up-vote them if so, because they are really good!

Comments

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.