Dennis Van der Gragt
Dennis Van der Gragt

Reputation: 11

Powershell external program as variable

I just started with powershell. And created a script to monitor my server. But, I also need to monitor my harddisks and raid configuration from my server. This only can be done with software from HP.

So what I want:
I have a command line utility from HP, which I can use to monitor the array and disks. when I start this utility, I get this:

PS C:\Program Files (x86)\Compaq\Hpacucli\Bin> .\hpacucli.exe controller slot=3 physicaldrive "1I:0:1 (port 1I:box 0:bay  1, 750 GB)" show status | out-host 
physicaldrive 1I:0:1 (port 1I:box 0:bay 1, 750 GB): OK

I just want this output as a variable to use it further in my script.

How can this be done.

I tried:

$disk1 = "c:\program files (x86)\Compaq\hpacucli\bin\hpacucli.exe controller slot=3 physicaldrive "1I:0:1 (port 1I:box 0:bay  1, 750 GB)" show status | out-host"

But this was not working

So I just want to have physicaldrive 1I:0:1 (port 1I:box 0:bay 1, 750 GB): OK as variable.

I hope someone can help me. So I can learn to use powershell further.

Thanks allready. dennis

Upvotes: 1

Views: 780

Answers (1)

Keith Hill
Keith Hill

Reputation: 201602

Get rid of the out-host cmdlet as it is consuming the exe's output e.g.:

$disk1 = .\hpacucli.exe controller slot=3 physicaldrive "1I:0:1 (port 1I:box 0:bay 1, 750 GB)" show status

With PowerShell, keep in mind that anytime you invoke a pipeline and there is output, it will go to the output stream where it can be captured by a variable (like above) or get displayed to the screen if the output is not captured. When you pipe to Out-Host, it bypasses normal output and displays the input "directly" to the screen. That's why you can't capture the data to a variable.

In a function, this is a bit different than traditional languages because every statement that produces output that isn't captured to a variable (or redirected to $null) will produce output for that function. This can be surprising to many first time PowerShell users when their functions return more than they expected. Look at the accepted answer to this SO question for more details.

Upvotes: 3

Related Questions