【发布时间】:2016-09-08 08:59:16
【问题描述】:
我刚刚从 foxdeploy.com 获取示例代码来创建一个带有 GUI 的 PowerShell 脚本。
它可以在 Windows 10 机器上运行,但由于某种原因它不能在 Windows 7 或 Windows Server 2008 R2 上运行,它应该在最后运行(不是这个代码,而是带有 GUI 的最终 PS 脚本)。
代码如下:
#ERASE ALL THIS AND PUT XAML BELOW between the @" "@
$inputXML = @"
<Window x:Class="WpfApplication2.MainWindow"
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:WpfApplication2"
mc:Ignorable="d"
Title="FoxDeploy Awesome Tool" Height="416.794" Width="598.474" Topmost="True">
<Grid Margin="0,0,45,0">
<Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="24,28,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\Stephen\Dropbox\Docs\blog\foxdeploy favicon.png"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="100" Margin="174,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" FontSize="16"><Run Text="Use this tool to find out all sorts of useful disk information, and also to get rich input from your scripts and tools"/><InlineUIContainer>
<TextBlock x:Name="textBlock1" TextWrapping="Wrap" Text="TextBlock"/>
</InlineUIContainer></TextBlock>
<Button x:Name="button" Content="get-DiskInfo" HorizontalAlignment="Left" Height="35" Margin="393,144,0,0" VerticalAlignment="Top" Width="121" FontSize="18.667"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="35" Margin="186,144,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="168" FontSize="16"/>
<Label x:Name="label" Content="ComputerName" HorizontalAlignment="Left" Height="46" Margin="24,144,0,0" VerticalAlignment="Top" Width="138" FontSize="16"/>
<ListView x:Name="listView" HorizontalAlignment="Left" Height="156" Margin="24,195,0,0" VerticalAlignment="Top" Width="511" FontSize="16">
<ListView.View>
<GridView>
<GridViewColumn Header="Drive Letter" DisplayMemberBinding ="{Binding 'Drive Letter'}" Width="120"/>
<GridViewColumn Header="Drive Label" DisplayMemberBinding ="{Binding 'Drive Label'}" Width="120"/>
<GridViewColumn Header="Size(MB)" DisplayMemberBinding ="{Binding Size(MB)}" Width="120"/>
<GridViewColumn Header="FreeSpace%" DisplayMemberBinding ="{Binding FreeSpace%}" Width="120"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
Get-FormVariables
#===========================================================================
# Actually make the objects work
#===========================================================================
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 840
Get-DiskInfo -computername $WPFtextBox.Text | % {$WPFlistView.AddChild($_)}
})
#Sample entry of how to add data to a field
#$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})
#===========================================================================
# Shows the form
#===========================================================================
write-host "To show the form, run the following" -ForegroundColor Cyan
$Form.ShowDialog() | out-null
脚本似乎在 $Form.FindName 的第 49 行失败
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
到目前为止,我注意到的是 $Form 在 Windows 7 上不返回任何内容,而在 Windows 10 上它返回一些属性。所以我认为真正的问题在第 42 行:
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
当我将脚本执行到第 43 行时,它会返回捕获消息。
那么我该如何解决这个问题?安装了最新的 .NET 版本,我想语法应该没问题,因为它适用于 Wondows 10。
【问题讨论】:
-
如果删除
Try/Catch块会出现什么错误? -
尝试在 Win7+PS5.1 上运行代码,没有发现错误:puu.sh/r40Wp/c08f57cf1f.png 按钮有效。
-
@MathiasR.Jessen 我的安装是德语,但应该是“您不能在空值表达式上调用方法”错误(我之前在同一行得到的相同)跨度>
-
@wOxxOm 看起来这就是问题所在,我安装了 PS2.0,更新到 4.0 并且成功了!没想到。
标签: wpf windows xaml powershell