将PowerShell与.NET4一起使用时WPK出现问题

问题描述

| 当我不使用.NET 4.0 Framwork时,以下代码有效
ipmo WPK


$ConnectionString = $ConnectionString = \"Server=localhost;Integrated Security=True\"

$conn = new-object System.Data.sqlClient.sqlConnection
$conn.ConnectionString = $ConnectionString 
$conn.open() 


function Invoke-sql1
{
    param( [string]$sql,[System.Data.sqlClient.sqlConnection]$connection
           )
    $cmd = new-object System.Data.sqlClient.sqlCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.sqlClient.sqlDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0]
}


function Show-Bockmarks ($resource) {
    New-Window   {
    New-Grid -ColumnDeFinitions @(
            New-ColumnDeFinition -Width 900*
        ) -RowDeFinitions @(
            New-RowDeFinition -Height 30
            New-RowDeFinition -Height 600*
        )     {

        New-StackPanel -Orientation horizontal -column 0 -row 0 -Children {
            New-Button -Name Search \"Press me\" -On_Click {
            $ff_sql = @\"
SELECT \'abc\' title,getdate() dateAdded,\'xyz\' url
UNION
SELECT \'efg\' title,\'xyz\' url
\"@
            $conn = $resource.conn
            $window.Title = \"$($conn.database) Database browser\"
            $TableView = $window | Get-ChildControl TableView
            $TableView.ItemsSource = Invoke-sql1 -sql $ff_sql -connection $conn
             } 
             New-Button -Name Cancel \"Close\" -On_Click {$window.Close()} 
        }
        New-ListView -Column 0 -Row 1 -Name TableView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn \"title\" 
               New-GridViewColumn \"dateAdded\" 
               New-GridViewColumn \"url\" 
           }
        }
    }
    } -asjob -Resource $resource
}

Show-Bockmarks -resource @{conn = $conn}
按下显示我的按钮,网格将填充3行。 但是当我通过添加powershell_ise.exe.Config这样使用.Net 4.0框架时
<?xml version=\"1.0\"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy=\"true\">
        <supportedRuntime version=\"v4.0.30319\" />
        <supportedRuntime version=\"v2.0.50727\" />
    </startup>
</configuration>
我收到以下错误
Add-Type : c:\\Users\\berndk.MMEDVNT\\AppData\\Local\\Temp\\eps22jnq.0.cs(24) : The type \'System.Windows.Markup.IQueryAmbient\' is defined in an assembly that is not referenced. You must add a
 reference to assembly \'System.Xaml,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089\'.
我想,在某些情况下,将WPK与-asJob一起使用会引起问题,但我不确定。     

解决方法

打开Start-WPFJob.ps1文件,并在第48行上:
    Add-Type -IgnoreWarnings -ReferencedAssemblies \"WindowsBase\",\"PresentationCore\",\"PresentationFramework\" @\"
用。。。来代替:
    Add-Type -IgnoreWarnings -ReferencedAssemblies \"WindowsBase\",\"PresentationFramework\",\"System.Xaml\" @\"
WPK仅适用于.NET 3.5。您可能还需要修复一些问题。 :)