2016-09-08 17 views
1

foxdeploy.comのサンプルコードを使って、GUIを使用してPowerShellスクリプトを作成しました。PowershellはWindows.Markup.XamlReaderをロードできません

これは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

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)} 

で49行で失敗しているようだが、 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で動作するため、構文は問題ないはずです。

+0

'Try/Catch'ブロックを削除すると、どのようなエラーが発生しますか? –

+0

Win7 + PS5.1でコードを実行しようとしましたが、エラーは見られませんでした:https://puu.sh/r40Wp/c08f57cf1f.pngボタンが機能します。 – wOxxOm

+0

@ MathiasR.Jessen私のインストールはドイツ語ですが、 "null値の式でメソッドを呼び出すことはできません"というエラーがあります(これは同じ行の前に同じです)。 –

答えて

2

同じ問題がありました。私はVS 2015 Enterpriseでフォームを設計しました。 XAMLはあなたのもの(FoxDeployのチュートリアル)とまったく同じでした。私の個人的なクライアント(Windows 7、Powershell 4.0、.NET Framework 4.0)ではうまくいきました。しかし、私がWindows Server 2008 R2(Powershell 2.0、.NET 3.5)でスクリプトを起動しようとしていたとき、私はあなたと同じ問題に遭遇しました。 .NETのアップデート& Powershellのバージョンは、顧客がサービスを使用している生産的なシステムではありませんでした。最後に、この後

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="FoxDeploy Awesome Tool" Height="416.794" Width="598.474" Topmost="True"> 

:これまで

<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"> 

が、私はこのからXAML「窓」のタグを編集した:だから私はこのような環境に私のXAMLを実行するための回避策を見つけましたステップ私はシングルスレッドのアパートモードでpowershellを実行する必要がありました。

powershell -sta 

このモードでは、デフォルトでPowershell ISEが既に使用されています。

削除されたコードによってシステムでこのエラーが発生した理由を説明することはできませんが、XAMLと戦わなければならない他の人を助けることができるかもしれません。& Powershellを「古い」Windows環境にします。

関連する問題