简体   繁体   中英

Unable to execute powershell GUI using wpf

I am trying to build a GUI (a simple one click button) for some daily tasks we have to do at work. I started off with a disk space checking .ps1 script built into GUI as below

Add-Type -AssemblyName PresentationFramework

[xml]$XAMLWindow = '
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="Windows Management Tool" Height="450" Width="600" Background="Gray">
    <Grid>

        <Button Name="DiskSpace" Content="Check Available Disk Space" HorizontalAlignment="Left" Height="43" Margin="56,194,0,0" VerticalAlignment="Top" Width="181"/>


           </Grid>
</Window>

'

$Reader=(New-Object System.Xml.XmlNodeReader $XAMLWindow)
$Window=[Windows.Markup.XamlReader]::Load( $Reader )


$DiskSpace = $Window.FindName('DiskSpace')



$DiskSpace.Add_Click({
.\checkDiskSpaceOnMulti.ps1
})


$Window.ShowDialog() | Out-Null

Below is the code for checkDiskSpaceOnMulti.ps1 that I embedded into GUI

$file = get-Content C:\list.txt  

foreach ( $args in $file) { 
get-WmiObject win32_logicaldisk -ComputerName $args -Filter "Drivetype=3"  |  
ft SystemName,DeviceID,VolumeName,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize 
} 

and when I click the button on GUI, I get below error. checkDiskSpaceOnMulti.ps1 works perfectly as I need, when using from powershell ISE. Problem is only when using with the GUI script.

.\checkDiskSpaceOnMulti.ps1 : The term '.\checkDiskSpaceOnMulti.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At H:\Powershell\Powershell\Windows_Utility_Tool.ps1:54 char:1
+ .\checkDiskSpaceOnMulti.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\checkDiskSpaceOnMulti.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Also suggest me a good tool for PoSh GUI developing tool.

Perhaps you'd be better of using a multiline textbox using a monospaced font in the window to show the results. Also, since the code to run is quite small, I would put it inside a function and run that on the button click:

Add-Type -AssemblyName PresentationFramework

[xml]$XAMLWindow = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="Windows Management Tool" Height="450" Width="600" Background="Gray">

    <Grid Margin="0,10,0,0">

        <TextBox Name="ResultBox" Height="300" Width="500" TextWrapping="Wrap" 
                 AcceptsReturn="True" VerticalScrollBarVisibility="Auto" FontFamily="Consolas"
                 VerticalAlignment="Top" Margin="0,20,0,0"/>

        <Button Name="DiskSpace" Content="Check Available Disk Space" HorizontalAlignment="Left" 
                Height="43" Margin="40,330,0,0" VerticalAlignment="Top" Width="181"/>

    </Grid>
</Window>
'@

$Reader = New-Object System.Xml.XmlNodeReader $XAMLWindow
$Window = [Windows.Markup.XamlReader]::Load($Reader)

function checkDiskSpaceOnMulti {
    $textBox = $Window.FindName("ResultBox")
    $textBox.Clear()

    # read the file as string array and skip empty lines
    # UPDATE: force the result of Get-Content to be an array if only one computer is listed
    $file = @(Get-Content 'C:\list.txt') | Where-Object { $_ -match '\S' }

    # fill the textbox with the results
    $textBox.Text = foreach ($computer in $file) {
        # add a test to see if the computer can be reached
        if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
            Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer -Filter "Drivetype=3" |
            Format-Table SystemName, DeviceID, VolumeName,
                         @{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},
                         @{Label="Free Size" ;Expression={$_.Freespace / 1gb -as [int] }} -AutoSize | Out-String
        }
        else {
            "WARNING: Computer '$computer' is off-line or does not exist.`r`n"
        }
    }
}

$DiskSpace = $Window.FindName("DiskSpace")
$DiskSpace.Add_Click({ checkDiskSpaceOnMulti })

$Window.ShowDialog() | Out-Null

The above will result in a window like this:

在此输入图像描述

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM