【发布时间】:2019-10-29 00:24:29
【问题描述】:
我正在尝试为我们在工作中必须完成的一些日常任务构建一个 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开发工具的好工具。
【问题讨论】:
-
那不是winforms,那是wpf
-
不要使用
$args作为变量名,因为它是PowerShell中的automatic variable。 -
我强烈建议不要在 PowerShell 中构建 UI。仅仅因为你可以做某事并不意味着你应该。
-
它不完全是图形化 PoSh。我只是想通过单击从 GUI 窗口获取 .ps1 可执行文件。
-
我们还没有收到您的来信。我的回答解决了您的问题吗?如果是这样,请点击左侧的✓ 考虑accepting。这将帮助其他有类似问题的人更轻松地找到它。
标签: wpf powershell user-interface