EDN Admin
Well-known member
Hi all,
hope someone here, who is more experienced than I am, could help me with this?
I want to execute a Powershell pipeline remotely including filtering, i.e. with the where object. Something like this
<pre class="prettyprint blalba.. | where-object { $_.DisconnectDate -ge $null } | blabla..[/code]
I can do filtering on the PS console on the server, no problem. However, what do I need to do to get it via Visual Basic .Net?
I tried with creating a new command object, adding parameters, and then adding into the pipeline. Also tried to use .Addscript("..."), however, all of that does not work at all. Something I am doing wrong here.
So, my question is: how to handle the filtering ?
<pre class="prettyprint lang-vb Dim SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
Dim arl As New ArrayList
Dim serverUri As System.Uri = New Uri(String.Format("https://" & server & "/powershell"))
Dim creds As PSCredential = DirectCast(Nothing, PSCredential)
Dim wsManInfo As WSManConnectionInfo = New WSManConnectionInfo(serverUri, SHELL_URI, creds)
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential
wsManInfo.SkipCACheck = True
wsManInfo.SkipCNCheck = True
wsManInfo.NoEncryption = False
wsManInfo.MaximumConnectionRedirectionCount = 4
myRunSpace = RunspaceFactory.CreateRunspace(wsManInfo)
Console.WriteLine("Opening workspace on " & server & "...")
myRunSpace.Open()
------- here it gets interesting
Dim ps_1 As New Command("Get-Mailbox")
ps_1.Parameters.Add("server", server)
Dim ps_2 As New Command("Get-Mailboxstatistics")
pipeLine = myRunSpace.CreatePipeline()
pipeLine.Commands.Add(ps_1)
pipeLine.Commands.Add(ps_2)
Dim commandResults As Collection(Of PSObject)
commandResults = pipeLine.Invoke()
For Each obj As PSObject In commandResults
..do something here with the results in obj ..
Next
-------
pipeline.dispose()
myrunspace.close()[/code]
<br/>
View the full article
hope someone here, who is more experienced than I am, could help me with this?
I want to execute a Powershell pipeline remotely including filtering, i.e. with the where object. Something like this
<pre class="prettyprint blalba.. | where-object { $_.DisconnectDate -ge $null } | blabla..[/code]
I can do filtering on the PS console on the server, no problem. However, what do I need to do to get it via Visual Basic .Net?
I tried with creating a new command object, adding parameters, and then adding into the pipeline. Also tried to use .Addscript("..."), however, all of that does not work at all. Something I am doing wrong here.
So, my question is: how to handle the filtering ?
<pre class="prettyprint lang-vb Dim SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
Dim arl As New ArrayList
Dim serverUri As System.Uri = New Uri(String.Format("https://" & server & "/powershell"))
Dim creds As PSCredential = DirectCast(Nothing, PSCredential)
Dim wsManInfo As WSManConnectionInfo = New WSManConnectionInfo(serverUri, SHELL_URI, creds)
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential
wsManInfo.SkipCACheck = True
wsManInfo.SkipCNCheck = True
wsManInfo.NoEncryption = False
wsManInfo.MaximumConnectionRedirectionCount = 4
myRunSpace = RunspaceFactory.CreateRunspace(wsManInfo)
Console.WriteLine("Opening workspace on " & server & "...")
myRunSpace.Open()
------- here it gets interesting
Dim ps_1 As New Command("Get-Mailbox")
ps_1.Parameters.Add("server", server)
Dim ps_2 As New Command("Get-Mailboxstatistics")
pipeLine = myRunSpace.CreatePipeline()
pipeLine.Commands.Add(ps_1)
pipeLine.Commands.Add(ps_2)
Dim commandResults As Collection(Of PSObject)
commandResults = pipeLine.Invoke()
For Each obj As PSObject In commandResults
..do something here with the results in obj ..
Next
-------
pipeline.dispose()
myrunspace.close()[/code]
<br/>
View the full article