繁体   English   中英

无法使用wpf执行powershell GUI

[英]Unable to execute powershell GUI using wpf

我正在尝试构建一个GUI(一个简单的单击按钮),用于我们在工作中必须完成的一些日常任务。 我开始使用内置于GUI中的磁盘空间检查.ps1脚本,如下所示

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

下面是我嵌入GUI的checkDiskSpaceOnMulti.ps1的代码

$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 
} 

当我点击GUI上的按钮时,我得到以下错误。 当使用PowerShell ISE时,checkDiskSpaceOnMulti.ps1可以正常工作。 只有在使用GUI脚本时才会出现问题。

.\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

还建议我使用PoSh GUI开发工具的好工具。

也许您最好使用窗口中使用等宽字体的多行文本框来显示结果。 此外,由于要运行的代码非常小,我会把它放在一个函数中并在按钮单击上运行它:

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

以上将导致如下窗口:

在此输入图像描述

希望有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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