ShowUI and VisiFire

Jul 7, 2011 at 12:57 AM
Edited Jul 7, 2011 at 12:59 AM

I downloaded VisiFire 3.6.8 and ShowUI 1.1 and can't figure out how to create graphs as described in articles such as this.  The code using PowerBoots and VisiFire works but I was wondering if it was possible to accomplish the same with ShowUI.

Coordinator
Jul 7, 2011 at 8:13 AM

Now that it's released, I have a few blog posts about Add-UIModule to post, but basically, download visifire, put the two WPF dlls into a folder somewhere where you want to keep them (you might consider: C:\Users\UserName\Documents\WindowsPowerShell\Modules\Visifire\ since we're about to create the module in that folder anyway).

I've writen a show-ui version of that one function, just to help out, but basically, you need to do generate a module from Visifire (I recommend also creating one for WPFToolkit, while you're at it, but you don't need it for this). In the future, when you're using this ... you need to import ShowUI and the Visifire module you're creating.

 

Import-Module ShowUI
Add-UIModule -path .\Libraries\WPFToolkit.dll -Name WPFToolKit -AsCmdlet
Add-UIModule -path .\Libraries\WPFVisifire.Charts.dll -name Visifire -AsCmdlet

Import-Module ShowUI, Visifire

function New-DiskSpace {
param([Parameter(ValueFromPipeline=$true)]$InputObject)
begin {
    $chart = Chart -MinHeight 100 -MinWidth 200 -Theme Theme2 -View3D `
          -BorderBrush Gray -CornerRadius 5 -BorderThickness 0.5 `
          -AnimationEnabled:$false -Series {
            DataSeries -RenderAs Pie -LegendText UsedSpace -LabelEnabled $true
            DataSeries -RenderAs Pie -LegendText FreeSpace -LabelEnabled $true #  -RadiusX 5 -RadiusY 5
        }
}
process {
    foreach ($disk in $InputObject) {
        [double]$free = [System.Math]::Round(($disk.FreeSpace/1GB),2)
        [int]$pFree = ([double]$disk.FreeSpace/[double]$disk.Size) * 100

        [double]$used = [System.Math]::Round((([double]$disk.Size - [double]$disk.FreeSpace)/1GB),2)
        [int]$pUsed = (([double]$disk.Size - [double]$disk.FreeSpace)/[double]$disk.Size) * 100

        $dp1 = DataPoint -AxisXLabel $disk.Name -YValue $used -LabelText "$pUsed%" -
        $chart.Series[0].DataPoints.Add($dp1)

        $dp2 = DataPoint -YValue $free -LabelText "$pFree%" 
        $chart.Series[1].DataPoints.Add($dp2)
    }
}
end {
    Grid -ControlName "DiskSpace Chart" { $chart } -show
}
}


# Here's an example
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" | New-DiskSpace

 

A couple of notes:

  1. The function I wrote there is slightly convoluted because I wanted to let you pipe in the output the way you did with the old one, and I wanted it to still be able to (by just commenting out the -Show) use this as a "ShowUI Control"...
  2. I recommend -AsCmdlet on the Add-UIModule. because it generates a dll like the one that we create by default for ShowUI, which loads a little faster, but ...
  3. You can't generate -AsCmdlet for WPFToolkit if you're running PowerShell in .Net 4 (WPF Toolkit isn't required in that case), so make sure you haven't messed with your PowerShell.exe.config before you generate that module, or just skip it, you only need the visifire module.
  4. That particular diskspace function requires that you pass in the WMI Win32_LogicalDisk object
Jul 7, 2011 at 7:44 PM
Edited Jul 8, 2011 at 10:14 PM

 

The code you provided doesn't work for me.  I have included what I have in my test.ps1, Visifire.psd1 and Visifire.psm1 file plus I 
noticed the reference to a VisifireCommands.dll in the psd1 file but the dll does not exists.

#--------------------------------------------------
# test.ps1
#--------------------------------------------------
Import-Module ShowUI
Add-UIModule -path ".\VisiFire\WPFToolkit.dll" -Name WPFToolKit #-AsCmdlet
Add-UIModule -path ".\VisiFire\WPFVisifire.Charts.dll" -name Visifire -AsCmdlet

Import-Module ShowUI, Visifire

function New-DiskSpace {
	param([Parameter(ValueFromPipeline=$true)]$InputObject)
	begin {
    	$chart = Chart -MinHeight 100 -MinWidth 200 -Theme Theme2 -View3D `
        				-BorderBrush Gray -CornerRadius 5 -BorderThickness 0.5 `
          				-AnimationEnabled:$false -Series {
            				DataSeries -RenderAs Pie -LegendText UsedSpace -LabelEnabled $true
            				DataSeries -RenderAs Pie -LegendText FreeSpace -LabelEnabled $true #  -RadiusX 5 -RadiusY 5
        				}
	}

	process {
    	foreach ($disk in $InputObject) {
        	[double]$free = [System.Math]::Round(($disk.FreeSpace/1GB),2)
        	[int]$pFree = ([double]$disk.FreeSpace/[double]$disk.Size) * 100

	        [double]$used = [System.Math]::Round((([double]$disk.Size - [double]$disk.FreeSpace)/1GB),2)
    	    [int]$pUsed = (([double]$disk.Size - [double]$disk.FreeSpace)/[double]$disk.Size) * 100

	        $dp1 = DataPoint -AxisXLabel $disk.Name -YValue $used -LabelText "$pUsed%" -
	        $chart.Series[0].DataPoints.Add($dp1)

	        $dp2 = DataPoint -YValue $free -LabelText "$pFree%" 
	        $chart.Series[1].DataPoints.Add($dp2)
    	}
	}
	end {
    	Grid -ControlName "DiskSpace Chart" { $chart } -show
	}
}

Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" | New-DiskSpace

#--------------------------------------------------
# Visifire.psd1 #-------------------------------------------------- @{ ModuleVersion = '1.0' RequiredModules = 'ShowUI' RequiredAssemblies = 'C:\Users\youngs\Documents\WindowsPowerShell\Modules\VisiFire\WPFVisifire.Charts.dll' ModuleToProcess = 'C:\Users\youngs\Documents\WindowsPowerShell\Modules\Visifire\Visifire.psm1' GUID = 'b8a0d887-41fb-409a-a57b-5c2a2f36de0e' NestedModules = 'C:\Users\youngs\Documents\WindowsPowerShell\Modules\Visifire\VisifireCommands.dll' CmdletsToExport = @('New-Chart','New-Axis','New-DataPoint','New-DataSeries','New-CustomAxisLabel','New-TrendLine','New-ToolTip',
'New-CustomAxisLabels','New-AxisLabels','New-Title','New-ZoomBar','New-Legend','New-ChartGrid','New-Ticks','New-PlotArea ' ) AliasesToExport = @( 'Chart','Axis','DataPoint','DataSeries','CustomAxisLabel','TrendLine','ToolTip','CustomAxisLabels','AxisLabels',
'Title'
,'ZoomBar','Legend','ChartGrid','Ticks','PlotArea' ) }

#----------------------------------------------
# Visifure.psm1
#----------------------------------------------
Set-Alias -Name Chart -Value New-Chart 
Set-Alias -Name Axis -Value New-Axis 
Set-Alias -Name DataPoint -Value New-DataPoint 
Set-Alias -Name DataSeries -Value New-DataSeries 
Set-Alias -Name CustomAxisLabel -Value New-CustomAxisLabel 
Set-Alias -Name TrendLine -Value New-TrendLine 
Set-Alias -Name ToolTip -Value New-ToolTip 
Set-Alias -Name CustomAxisLabels -Value New-CustomAxisLabels 
Set-Alias -Name AxisLabels -Value New-AxisLabels 
Set-Alias -Name Title -Value New-Title 
Set-Alias -Name ZoomBar -Value New-ZoomBar 
Set-Alias -Name Legend -Value New-Legend 
Set-Alias -Name ChartGrid -Value New-ChartGrid 
Set-Alias -Name Ticks -Value New-Ticks 
Set-Alias -Name PlotArea -Value New-PlotArea 
Export-ModuleMember -Cmdlet * -Function * -Alias *

 

Here is the errors I get:

Add-Type : (0) : Metadata file 'WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35.dll' could not be
found

(1) : namespace AutoGenerateCmdlets1564175944

At C:\Users\youngs\Documents\WindowsPowerShell\Modules\ShowUI\CodeGenerator\Add-UiModule.ps1:254 char:21
+             Add-Type <<<<  @addTypeParameters
    + CategoryInfo          : InvalidData: (error CS0006: M...ld not be found:CompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
 
Add-Type : Cannot add type. There were compilation errors.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\ShowUI\CodeGenerator\Add-UiModule.ps1:254 char:21
+             Add-Type <<<<  @addTypeParameters
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
 
Import-Module : The module to process 'C:\Users\youngs\Documents\WindowsPowerShell\Modules\Visifire\VisifireCommands.dll', listed in
 field 'NestedModules' of module manifest 'C:\Users\youngs\Documents\WindowsPowerShell\Modules\Visifire\Visifire.psd1' was not proce
ssed because no valid module was found in any module directory.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\test.ps1:7 char:14
+ Import-Module <<<<  ShowUI, Visifire
    + CategoryInfo          : ResourceUnavailable: (Visifire:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleFileNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
 
The term 'Chart' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the na
me, or if a path was included, verify that the path is correct and try again.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\test.ps1:12 char:20
+         $chart = Chart <<<<  -MinHeight 100 -MinWidth 200 -Theme Theme2 -View3D `
    + CategoryInfo          : ObjectNotFound: (Chart:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'DataPoint' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of th
e name, or if a path was included, verify that the path is correct and try again.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\test.ps1:28 char:26
+             $dp1 = DataPoint <<<<  -AxisXLabel $disk.Name -YValue $used -LabelText "$pUsed%" -
    + CategoryInfo          : ObjectNotFound: (DataPoint:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Cannot index into a null array.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\test.ps1:29 char:24
+             $chart.Series[ <<<< 0].DataPoints.Add($dp1)
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
 
The term 'DataPoint' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of th
e name, or if a path was included, verify that the path is correct and try again.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\test.ps1:31 char:26
+             $dp2 = DataPoint <<<<  -YValue $free -LabelText "$pFree%"
    + CategoryInfo          : ObjectNotFound: (DataPoint:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Cannot index into a null array.
At C:\Users\youngs\Documents\WindowsPowerShell\Modules\test.ps1:32 char:24
+             $chart.Series[ <<<< 1].DataPoints.Add($dp2)
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
 
Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: Children of 'System.Windows.Controls.UIElementCollection' cannot be null. Object derived from UIElement expected."
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\WPK\Set-Property.ps1:74 char:81
+                                     $null = $inputObject."$($realItem.Name)".Add <<<< ($ri)                                      
                                                                
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 

Coordinator
Jul 8, 2011 at 4:33 AM
Edited Jul 8, 2011 at 4:37 AM

Well, ok, for the record: Nobody should put an Add-UIModule command into a script. :-)

That command should always be run in the console, by hand.  If you get errors generating the UI Module, the resulting module won't work, and nothing that depends on it is going to work. We should probably actually tweak things a bit to make sure we wipe out the files if we don't fully generate it.

Once it's generated, you can Import it in scripts without problems, but you want to make sure the generation works. 

From your error, I'm going to guess that something went wrong generating the Visifire module, not the WPFToolkit, but I can't figure out what exactly that might be...  to be honest, I tried it myself that way and it didn't work!  So then, I looked back over my list to see what I had done before and tried repeating it, and to my surprise, it actually worked:

 

Add-UIModule -Path .\Libraries\WPFToolkit.dll -Name WPFToolkit -AsCmdlet -Import
Add-UIModule -Path .\Libraries\WPFVisifire.Charts.dll -Name Visifire -AsCmdlet -Import

 

Basically, if you import the WPFToolkit module that you created before you try to generate the Visifire module, then it works.  When I was doing it piecemeal, I used the -Import parameter, but I didn't include it when I pasted it here because a) I didn't realize it mattered, and b) I didn't want anyone thinking that Add-UIModule is something you should do more than once ;-)

 

P.S.: if you can edit your post above and add some carriage returns in the CmdletsToExport and AliasesToExport lines, that would return the forum width to normal. I'm going to go see if there's a request in the CodePlex forums to fix the forums so they don't get so wide.