Jump to content
Software FX Community

Function Output Piped to a PowerGadget using -refresh


Recommended Posts

Hey guys, I'v just got the trial of PowerGadgets to see what I can do with it and have my first question.

 I am trying to monitor hard drive space on a remote server that is firewalled except for port 80. What I am doing is running a batch file on the server every minute that dumps the result of a "dir" into a text file on the web folder so I can read it remotly. As an example I wrote this quick function to give you an idea:

 function GetBytesFree(){  $Url = "http://www.somewebsite.com/dir.txt"    $WebClient = New-Object System.Net.WebClient

    $Random = New-Object system.random   $DirResult = $WebClient.DownloadString($Url+"?t="+$Random.nextdouble())  $BytesFree = $DirResult | find "bytes free"  $BytesFree = $BytesFree.split(" ")[17]  return [double]$BytesFree}

PS C:\> GetBytesFree55153283072

Running this from the powershell console returns a double with the bytes free. It is correct and works fine. Now if i do a:

PS C:\>  GetBytesFree | out-gauge -type digital -floating

I get a nice digital display with the number in it, this works fine. But what I want to be able to do is update this every minute. However when I try to do a:

PS C:\>  GetBytesFree | out-gauge -type digital -floating -refresh 0:1:0

I get the output the first time, but when 1 minute passes I get the following error from the gauge:

Failed to connect to Data "The term 'GetBytesFree' is not recognised as a cmdlet, function, operable program, or script file. Verify the term and try again.' 

I realise that this is probalby not a PowerGadgets problem, but can anyone suggest a solution? Is it possible to put the function somewhere so it is exposed to the PowerGadget?

Edit:

After hunting around a bit more on the forums I found a post that mentioned when the -refresh is triggered a new instance of the Gadget with it's own runspace is created.  I will have a play around and see what I can work out.

 

Thanks in advance.

Link to post
Share on other sites

So along the same lines as Ivan...  Drop these lines in a .ps1 script

$Url = "http://www.somewebsite.com/dir.txt"    $WebClient = New-Object System.Net.WebClient

    $Random = New-Object system.random   $DirResult = $WebClient.DownloadString($Url+"?t="+$Random.nextdouble())  $BytesFree = $DirResult | find "bytes free"  $BytesFree = $BytesFree.split(" ")[17] [double]$BytesFree

Then pipe that .ps1 to out-gauge.  If you decide to leave the original code, and put all of that in a .ps1 script, make sure to invoke the function at the end of the .ps1.  Either put the script in your path, or pass the full path to out-gauge.

Link to post
Share on other sites

Thanks for the help,

Those solutions did work however I got the powershell console flashfor a quick second every time the refresh happened. In the mean time I had aplay around with the sidebar packaging tool and just pasted my code into thatas the data source. This actually worked phenomenally well and gave me a sidebar gadget to boot.

*stares dreamily at his new sidebar gadget* 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...