fredag den 4. marts 2011

Implementing PowerShell in applications - Part 2

So we got our self a lot of code that only does 1 line of PowerShell script at the time. Lets try and make it a bit more interesting (red: able to do full scripts and syntax check )
Add another class too your project  myPS.vb and test it out.

Sub Main()
    Dim ps As New myPS

    Console.WriteLine(ps.InvokeString("Write-Host 'Hello world.'"))

    Dim script As String = "$services = Get-Service;" & _
        " foreach($service in $services) {" & _
        " Write-Host ($service.Name + ' is ' + $service.status)}"
    Console.WriteLine(ps.InvokeString(script))

    script = "$services = Get-Service; " & _
        " foreach($service in $services) { " & _
        "  $service}"


    For Each p As PSObject In ps.Execute("Get-Service")
        Dim out As String = p.Properties("Status").Value.ToString & " "
        out = out & p.Properties("Name").Value.ToString & " "
        out = out & p.Properties("DisplayName").Value.ToString & " "
        Console.WriteLine(out)
    Next

    Console.WriteLine("done.")
    Console.ReadLine()
End Sub

Ingen kommentarer:

Tilføj en kommentar