Author Posts

April 4, 2017 at 9:09 am

How can i run powershell scripts or multiple PS Commands from WPF? I am using this thing but getting some Method invocation Error.

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

Creating runspace and calling that script. [I am calling an external script.]
another thing i tried is :

using (PowerShell PowerShellInstance = PowerShell.Create())

But from this i can run a single command.

One working method i found was using script below:

Add-Type -AssemblyName PresentationFramework
[xml]$xaml = 
@"

    
        
        
        

    

"@

}
Function Test-InternetConnection {
[cmdletbinding(
    DefaultParameterSetName = 'Site'
)]
param(
    [Parameter(
        Mandatory = $True,
        ParameterSetName = '',
        ValueFromPipeline = $True)]
        [string]$Site,
    [Parameter(
        Mandatory = $True,
        ParameterSetName = '',
        ValueFromPipeline = $False)]
        [Int]$Wait
    )
    #Clear the screen
    Clear
    #Start testing the connection and continue until the connection is good.
    While (!(Test-Connection -computer $site -count 1 -quiet)) {
       # Write-Host -ForegroundColor Red -NoNewline "Connection down..."
        Write-Output "Connection down..."
        Start-Sleep -Seconds $wait
        }
    #Connection is good
    #Write-host -ForegroundColor Green "$(Get-Date): Connection up!"
     Write-Output "$(Get-Date): Connection up!"
}
Function Servicereport()
{
$obj = Get-Process | Out-GridView
Start-Sleep 2
}

Function Processreport()
{
Test-InternetConnection -Site google.com -Wait 2
Start-Sleep 2
}

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

$button1 = $Window.FindName("Services")
$button2 = $Window.FindName("Process")
$Method1=$button1.add_click
$Method2=$button2.add_click
$Method1.Invoke({Servicereport})
$Method2.Invoke({Processreport})
$Window.ShowDialog() | Out-Null

But with this approach i am unable to get Modern UI.

Please help i am actually confused.

April 5, 2017 at 3:57 pm

So, this isn't "from WPF." WPF is a presentation framework, not a language.

What language are you writing in?