PowerShell GUI 之使用visual studio创建GUI (2)

来源:互联网 发布:space矩阵分析 编辑:程序博客网 时间:2024/05/17 02:32

上一节提到的仅仅是powershell加载出来一个界面,并没有获取到任何信息,那么这节我们继续来完善我们的脚本。

我就先把所有脚本贴上来,里面涉及到一些基础知识再下一节再详解吧。

[xml]$XAML = @"<Window         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        xmlns:local="clr-namespace:powershell_GUI01"        Title="Diskquery" Height="350" Width="525">    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition/>        </Grid.ColumnDefinitions>        <Label Name="label" Content="查询磁盘空间" HorizontalAlignment="Center" Margin="195,20,84,0" VerticalAlignment="Top" Width="238" OpacityMask="Black" FontSize="18.667" Height="34" BorderThickness="0,0,10,0"/>        <Image Name="image" HorizontalAlignment="Left" Height="100" Margin="10,20,0,0" VerticalAlignment="Top" Width="100"/>        <Image Name="image1" HorizontalAlignment="Left" Height="100" Margin="25,20,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\melody\Desktop\55.png"/>        <Label Name="label1" Content="输入计算机名" HorizontalAlignment="Left" Margin="25,141,0,0" VerticalAlignment="Top"/>        <ListView Name="listView" HorizontalAlignment="Left" Height="159" Margin="171,59,0,0" VerticalAlignment="Top" Width="325">            <ListView.View>                <GridView>                    <GridViewColumn Width="80" DisplayMemberBinding ="{Binding 'Drive Letter'}" Header="Drive Letter"/>                    <GridViewColumn Width="80" DisplayMemberBinding ="{Binding 'Drive Label'}" Header="Drive Label"/>                    <GridViewColumn Width="80" DisplayMemberBinding ="{Binding Size(MB)}" Header="Size(MB)"/>                    <GridViewColumn Header="FreeSpace%" DisplayMemberBinding ="{Binding FreeSpace%}" Width="80"/>                </GridView>            </ListView.View>        </ListView>        <Button Name="button" Content="点击获取" HorizontalAlignment="Left" Margin="291,236,0,0" VerticalAlignment="Top" Width="82" Height="32" RenderTransformOrigin="0.413,1.522"/>        <TextBox Name="textBox" HorizontalAlignment="Left" Height="29" Margin="25,171,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="112"/>    </Grid></Window>"@$reader=(New-Object System.Xml.XmlNodeReader $xaml)[System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework")$Form=[Windows.Markup.XamlReader]::Load( $reader )$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}Function Get-DiskInfo {param($computername =$env:COMPUTERNAME)Get-WMIObject Win32_logicaldisk -ComputerName $computername | Select-Object @{Name='ComputerName';Ex={$computername}},`                                                                    @{Name=‘Drive Letter‘;Expression={$_.DeviceID}},`                                                                    @{Name=‘Drive Label’;Expression={$_.VolumeName}},`                                                                    @{Name=‘Size(MB)’;Expression={[int]($_.Size / 1MB)}},`                                                                    @{Name=‘FreeSpace%’;Expression={[math]::Round($_.FreeSpace / $_.Size,2)*100}}                                                                 }                                                                  $WPFtextBox.Text = $env:COMPUTERNAME$WPFbutton.Add_Click({$WPFlistView.Items.Clear()start-sleep -Milliseconds 840Get-DiskInfo -computername $WPFtextBox.Text | % {$WPFlistView.AddChild($_)}})$Form.ShowDialog() | out-null

前面的part都是获取xaml,将xaml写入到powershell,后面part的一部分是得正常携程get-diskinfo的powershell语句,至于如何给form添加点击的动作就可以显示function获取到的信息,这个知识点下一章节介绍。

这两小节知识给大家一个印象,原来可以结合visual studio来制作powershell的GUI而已。


0 0