无法使用WPF执行Powershell GUI

时间:2019-06-14 11:06:04

标签: wpf powershell user-interface

我正在尝试为我们在工作中要做的一些日常任务构建一个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开发工具。

1 个答案:

答案 0 :(得分:0)

也许最好在窗口中使用等宽字体的多行文本框来显示结果。另外,由于要运行的代码非常小,因此我将其放在函数中,然后单击以下按钮运行它:

std::vector<std::string> ids {"a", "b", "c"};
std::vector<Student> students;

students.reserve(ids.size());
std::transform(ids.begin(), ids.end(), std::back_inserter(students),
    [](auto& /* const std::string& */ id) {
        return Student(id);
    });

以上内容将导致出现这样的窗口:

enter image description here

希望有帮助