1

Hi I'm working on a little VB.net apps to manage my distribution list on my exchange 2010 server. This code call a ps1 scripts with 2 arguments and its work. the only problem I have is I want to trap the output of the PowerShell.Invoke(). after the execution PowerShellCommandResults is allways empty no records in the collection.
Maybe the problem come from trapping from ps1 file but I really don't know where to look at to resolve my problem

thx

Dim PowerShell As Management.Automation.PowerShell = PowerShell.Create()
Dim PowerShellCommand As New PSCommand()
Dim PowerShellCommandResults As Collection(Of PSObject)

PowerShellCommand.AddCommand("c:\scripts\connect.ps1")

PowerShellCommand.AddCommand("c:\scripts\newld.ps1")
PowerShellCommand.AddArgument("[email protected]")
PowerShellCommand.AddArgument("Distribution liste test")

Try
            PowerShellCommandResults = PowerShell.Invoke()
            Dim sw As New StreamWriter("C:\" & Now.ToLongDateString & ".txt")
            For Each line In PowerShellCommandResults
                sw.WriteLine(line.ToString)
            Next

            sw.Dispose()
            PowerShell.Dispose()
 Catch ex As Exception

 End Try

1 Answer 1

2

Have you tried using Powershell.Invoke(IEnumerable, IList) signature instead? I normally use C# so I may not have this completely right...

' Old and busted: PowerShellCommandResults = PowerShell.Invoke()
' New hotness:
PowerShell.Invoke(Nothing, PowerShellCommandResults)

' Everything below here is unchanged
Dim sw As New StreamWriter("C:\" & Now.ToLongDateString & ".txt")
For Each line In PowerShellCommandResults
    sw.WriteLine(line.ToString)
Next

Alternative Answer: Try using PowerShellCommand.AddScript("C:\Scripts\Connect.ps1")

Alternative Answer: Make sure the connect.ps1 and newld.ps1 scripts are outputting to the pipeline, not the Host.

# Bad: Explicitly writes to the host, and skips pipeline output
Write-Host "Some value"

# Good: A string, or variable on its own will be output on the pipeline
"Some Value"
$OtherVariable
Sign up to request clarification or add additional context in comments.

1 Comment

thx the problem was my scripts they were not returning to pipeline!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.