列表框上的WPF值不可读/使用PowerShell

hk8txs48  于 2023-06-07  发布在  Shell
关注(0)|答案(1)|浏览(165)

我在powershell上编写脚本,我在visual studio上使用WPF来编写一个更用户友好的脚本,我觉得它很酷。
我也完成了我的powershell脚本和xml wpf。
然而,当我在列表框上获取值时,这些值是不可见的,当我的鼠标在列表框上时,列表框看起来已填充,但这是不可读的。

WPF
<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:Migrate_Storage_CLU_Hyper_v"
        mc:Ignorable="d"
        Title="Migration Storage Cluster Hyper-v" Height="884" Width="820" WindowState="Normal" ResizeMode="CanResize" MinHeight="745" MinWidth="820" MaxHeight="755" MaxWidth="820">
    <Grid Margin="0,0,0,-6">
        <Grid.RowDefinitions>
            <RowDefinition Height="722*"/>
            <RowDefinition Height="7*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="161*"/>
        </Grid.ColumnDefinitions>
        <Label Name="Label_Cluster_Hyper_v" Content="Cluster Hyper-v :" HorizontalAlignment="Left" Margin="10,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.ColumnSpan="2"/>
        <ListBox Name="Listbox_Host_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="14,27,0,0" VerticalAlignment="Top" Width="296" Grid.ColumnSpan="2"/>
        <Label Name="Label_VMs" Content="Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="10,228,0,0" VerticalAlignment="Top" Width="149" Grid.ColumnSpan="2"/>
        <ListBox Name="Listbox_VMs" HorizontalAlignment="Left" Height="445" Margin="14,267,0,0" VerticalAlignment="Top" Width="400" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
        <Label Name="Label_Storage_Path_Src" Content="Storage Path source :" HorizontalAlignment="Left" Height="29" Margin="10,96,0,0" VerticalAlignment="Top" Width="185" Grid.ColumnSpan="2"/>
        <ListBox Name="Listbox_Storage_Path_Src" HorizontalAlignment="Left" Height="90" Margin="14,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
        <Button Name="Button_Ok" Content="OK" Margin="558,685,134,1" Grid.Column="1" MinHeight="35" MinWidth="91" IsEnabled="False"/>
        <Button Name="Button_Cancel" Content="Cancel" Margin="666,685,27,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
        <ListBox Name="Listbox_Selected_VMs" HorizontalAlignment="Left" Height="379" Margin="446,267,0,0" VerticalAlignment="Top" Width="293" SelectionMode="Multiple" Grid.Column="1"/>
        <Label Name="Label_Selected_VMs" Content="Selected Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="442,233,0,0" VerticalAlignment="Top" Width="149" Grid.Column="1"/>
        <Button Name="Button_Add" Grid.Column="1" Content=">" HorizontalAlignment="Left" Height="55" Margin="403,351,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
        <Button Name="Button_Remove" Grid.Column="1" Content="&gt;" HorizontalAlignment="Left" Height="55" Margin="403,431,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="RightToLeft"/>
        <Button Name="Button_Check" Content="Check" Margin="454,685,240,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
        <Button Name="Button_Display_VMs" Grid.Column="1" Content="Display VM" HorizontalAlignment="Left" Height="21" Margin="105,232,0,0" VerticalAlignment="Top" Width="93" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight" FontSize="10"/>
        <ListBox Name="Listbox_Storage_Path_Dst" HorizontalAlignment="Left" Height="90" Margin="442,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.Column="1"/>
        <Label Name="Label_Storage_Path_Dst" Content="Storage Path destination :" HorizontalAlignment="Left" Height="29" Margin="439,96,0,0" VerticalAlignment="Top" Width="185" Grid.Column="1"/>
        <ListBox Name="Listbox_Node_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="445,27,0,0" VerticalAlignment="Top" Width="296" Grid.Column="1"/>
        <Label Name="Label_Node_Hyper_v" Content=" Node Hyper-v :" HorizontalAlignment="Left" Margin="440,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.Column="1"/>
        <Button Name="Button_Show_Node" Grid.Column="1" Content="Show Node" HorizontalAlignment="Left" Height="44" Margin="339,37,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
        <Button Name="Button_Show_Path" Grid.Column="1" Content="Show Path" HorizontalAlignment="Left" Height="44" Margin="359,142,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
    </Grid>
</Window>
Powershell
Add-Type -AssemblyName PresentationFramework

#########################################################################
###########################***___Function___***##########################

function Get-Check {
   
    if (($Window.Findname("Listbox_Host_Hyper_v").SelectedItems -eq $null) -or ($Window.Findname("Listbox_Selected_VMs").Items -eq $null) -or ($Window.Findname("Listbox_Storage_Path").Items -eq $null) )
       {
        $Window.FindName("Label_EmptyField").Visibility ="Visible"
        $Window.FindName("Button_Ok").IsEnabled = $False
        $Window.FindName("Label_Resume").Visibility ="Hidden"
       }
         else 
             {
             $Window.FindName("Button_Ok").IsEnabled = $True
             $Window.FindName("Label_EmptyField").Visibility ="Hidden"
             $Window.FindName("Label_Resume").Visibility ="Visible"
             $Window.FindName("Label_Resume").Content = "
             Hôte Hyper-V = $Selected_Host
             Chemin UNC = $Selected_UNCPATH
             VM(s) sélectionné = $Selected_Virtuals
             CSV = 
        
             "
             }

                    }
#########################################################################
#############################***___Variable___***#############################

#$global:Host_Hyper_v = Get-Cluster -Domain mydomain.lan | Where-object {$_.name -like "srv[1-9]*"}
#$global:Selected_UNCPATH = $($Window.Findname("Listbox_Storage_Path").SelectedItems)
#$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
#$global:Selected_Virtuals = $($Window.Findname("Listbox_Selected_VMs").SelectedItems)
#$global:Vm_On_Host = Get-VM -ClusterObject (Get-ClusterResource -Cluster $Selected_Host | where ResourceType -eq "Virtual Machine")
#$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like '*NB_*'}
#$global:Src_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -notlike '*NB_*'}
#$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
#$global:Selected_Dst_Path = $($Window.Findname("Listbox_Storage_Path_Dst").SelectedItems)

#########################################################################
#############################***___XML___***#############################
[xml]$XML = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="Migration Storage Cluster Hyper-v" Height="760" Width="820" WindowState="Maximized" ResizeMode="CanResize" MinHeight="745" MinWidth="820" MaxHeight="755" MaxWidth="820">
    <Grid Margin="0,0,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="722*"/>
            <RowDefinition Height="7*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="161*"/>
        </Grid.ColumnDefinitions>
        <Label Name="Label_Cluster_Hyper_v" Content="Cluster Hyper-v :" HorizontalAlignment="Left" Margin="10,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.ColumnSpan="2"/>
        <ListBox Name="Listbox_Host_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="14,27,0,0" VerticalAlignment="Top" Width="296" Grid.ColumnSpan="2"/>
        <Label Name="Label_VMs" Content="Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="10,228,0,0" VerticalAlignment="Top" Width="149" Grid.ColumnSpan="2"/>
        <ListBox Name="Listbox_VMs" HorizontalAlignment="Left" Height="445" Margin="14,267,0,0" VerticalAlignment="Top" Width="400" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
        <Label Name="Label_Storage_Path_Src" Content="Storage Path source :" HorizontalAlignment="Left" Height="29" Margin="10,96,0,0" VerticalAlignment="Top" Width="185" Grid.ColumnSpan="2"/>
        <ListBox Name="Listbox_Storage_Path_Src" HorizontalAlignment="Left" Height="90" Margin="14,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
        <Button Name="Button_Ok" Content="OK" Margin="558,685,134,1" Grid.Column="1" MinHeight="35" MinWidth="91" IsEnabled="False"/>
        <Button Name="Button_Cancel" Content="Cancel" Margin="666,685,27,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
        <ListBox Name="Listbox_Selected_VMs" HorizontalAlignment="Left" Height="379" Margin="446,267,0,0" VerticalAlignment="Top" Width="293" SelectionMode="Multiple" Grid.Column="1"/>
        <Label Name="Label_Selected_VMs" Content="Selected Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="442,233,0,0" VerticalAlignment="Top" Width="149" Grid.Column="1"/>
        <Button Name="Button_Add" Grid.Column="1" Content=">" HorizontalAlignment="Left" Height="55" Margin="403,351,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
        <Button Name="Button_Remove" Grid.Column="1" Content="&gt;" HorizontalAlignment="Left" Height="55" Margin="403,431,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="RightToLeft"/>
        <Button Name="Button_Check" Content="Check" Margin="454,685,240,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
        <Button Name="Button_Display_VMs" Grid.Column="1" Content="Display VM" HorizontalAlignment="Left" Height="21" Margin="105,232,0,0" VerticalAlignment="Top" Width="93" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight" FontSize="10"/>
        <ListBox Name="Listbox_Storage_Path_Dst" HorizontalAlignment="Left" Height="90" Margin="442,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.Column="1"/>
        <Label Name="Label_Storage_Path_Dst" Content="Storage Path destination :" HorizontalAlignment="Left" Height="29" Margin="439,96,0,0" VerticalAlignment="Top" Width="185" Grid.Column="1"/>
        <ListBox Name="Listbox_Node_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="445,27,0,0" VerticalAlignment="Top" Width="296" Grid.Column="1"/>
        <Label Name="Label_Node_Hyper_v" Content=" Node Hyper-v :" HorizontalAlignment="Left" Margin="440,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.Column="1"/>
        <Button Name="Button_Show_Node" Grid.Column="1" Content="Show Node" HorizontalAlignment="Left" Height="44" Margin="339,37,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
        <Button Name="Button_Show_Path" Grid.Column="1" Content="Show Path" HorizontalAlignment="Left" Height="44" Margin="359,142,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
    </Grid>
</Window>
"@

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

#########################################################################
###########################***___Button___***##########################

#Setup Check button
$Window.FindName("Button_Check").add_click({
Get-Check         
})

#Setup Cancel button 
$Window.FindName("Button_Cancel").add_click({
$Window.Close()
})

#Setup List Cluster Hyper-V 
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Host_Hyper_v = Get-Cluster -Domain mydomain.lan | Where-object {$_.name -like "srv[1-9]*"}
Foreach ($element in $Host_Hyper_v){
$Window.FindName("Listbox_Host_Hyper_v").Items.add($($element.Name)) | Out-Null
}

#Setup List Node Hyper-V 

#Setup List UNC Path source
#Foreach ($element in $Src_Path){
#$Window.FindName("Listbox_Storage_Path").Items.add($($element.Name)) | Out-Null
#}

#Setup List UNC Path destination
Foreach ($element in $Dst_Path){
$Window.FindName("Listbox_Storage_Path_Dst").Items.add($($element.Name)) | Out-Null
}

#Setup Button Display VM
$Window.FindName("Button_Display_VMs").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Vm_On_Host = Get-VM -ClusterObject (Get-ClusterResource -Cluster $Selected_Host | where ResourceType -eq "Virtual Machine")
Foreach ($element in $VM_On_Host){
$Window.FindName("Listbox_VMs").Items.add($($element.Name)) | Out-Null
}})

#Setup Button Show Node
$Window.FindName("Button_Show_Node").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
Foreach ($element in $Selected_Node){
$Window.FindName("Listbox_Node_Hyper_v").Items.add($($element.Name)) | Out-Null
}})

#Setup Button Show Path
$Window.FindName("Button_Show_Path").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
$global:Src_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -notlike '*NB_*'}
$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like '*NB_*'}
Foreach ($element in $Dst_path){
$Window.FindName("Listbox_Storage_Path_Dst").Items.add($($element.Name)) | Out-Null
}
Foreach ($element in $Src_path){
$Window.FindName("Listbox_Storage_Path_Src").Items.add($($element.Name)) | Out-Null
}})

#Setup Button Add
$Window.FindName("Button_Add").add_click({
$VMs = $($Window.FindName("Listbox_VMs").SelectedItems)
Foreach ($element in $VMs){
$Window.FindName("Listbox_SelectedVMs").Items.add($element) | Out-Null
$Window.FindName("Listbox_VMs").Items.remove($element) | Out-Null
$Window.UpdateLayout() 
}
})

#Setup Button Remove
$Window.FindName("Button_Remove").add_click({
$Server = $($Window.FindName("Listbox_SelectedVMs").SelectedItems)
Foreach ($element in $Server){
$Window.FindName("Listbox_VMs").Items.add($element) | Out-Null
$Window.FindName("Listbox_SelectedVMs").Items.remove($element) | Out-Null
$Window.UpdateLayout() 
}
})

#Setup OK button
$Window.FindName("Button_OK").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Virtuals = $($Window.Findname("Listbox_Selected_VMs").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like '*PS_*'}

foreach ($element in $Selected_Virtuals){

Move-VMStorage `
    ComputerName $element `
    DestinationStoragePath $Selected_Dst_Path`
    Name $element
    }
})

#########################################################################
#########################***___OPEN-WINDOWS___***########################

$Window.ShowDialog() | Out-Null

我试着在visual studio上调整大小,不同的窗口改变了很多行,这些行在实际代码上是改变的,但我不明白为什么这个值看起来像是不可见的。
谢谢你的帮助。
最好的问候。
初学者

9jyewag0

9jyewag01#

尝试设置ListBoxItemsSource,如下所示:

$items = (Get-Cluster ...
$listBox.ItemsSource = $items

相关问题