I am trying to call PowerShell to add IP Addresses to Exchange's blocked IP
address list from an Exchange Server transport agent. The code works fine if
called from a vb app. I am really stumped.
-Rob
This is the message I am getting:
Cannot load Windows PowerShell snap-in
Microsoft.Exchange.Management.PowerShell.Admin because of the following
error: The process 'EdgeTransport' (PID = 1248) has been initialized twice as
Multiple instance type in the way we don't understand: current App Name is
'Transport' and to-be-set App Name is 'EMS'.
Reasons of this exception: 1. For any product executables, which will be
shipped out of box, they should never use AD driver functionality without
initializing its performance counter instance since we can NOT show a
intuitive Perf Counter instance name here.
2. We don't need the validation for test assembly because we don't care for
its performance counter naming; however, we definitely want to make sure it
is under our control, so that we don't miss any out of box product DLLs/EXEs.
What to do to avoid this exception: For any newly added Exchange test
executables which utilize AD perf, please add the executable name of the
process into exclusion list in
Microsoft.Exchange.Data.Directory.Globals.CheckExchangeTestExecutables.
The exception is thrown fom this line:
info =
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin",
snapInException)
Here is the class that I use to make the call:
Public Class clsPowerShell
Private objLock As New Object
Public snapInException As PSSnapInException = New PSSnapInException()
Private rsConfig As RunspaceConfiguration
Private info As PSSnapInInfo
Private myRunSpace As Runspace
'-------------------------------------------------------------
' New: Construct
'-------------------------------------------------------------
Public Sub New()
Try
rsConfig = RunspaceConfiguration.Create()
info =
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin",
snapInException)
myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)
myRunSpace.Open()
Catch ex As Exception
Throw New Exception("Error: Unable to create Init PowerShell. Reason =
" + ex.Message)
End Try
End Sub
'-------------------------------------------------------------
' New: Construct
'-------------------------------------------------------------
Protected Overrides Sub Finalize()
If (Not myRunSpace Is Nothing) Then
myRunSpace.Close()
End If
MyBase.Finalize()
End Sub
'-------------------------------------------------------------
' RunCommand: Run a power shell command
'-------------------------------------------------------------
Private Function RunCommand(ByVal MyCommand As Command) As Collection(Of
PSObject)
SyncLock objLock
' Create our command
Dim pipeLine As Pipeline = myRunSpace.CreatePipeline()
pipeLine.Commands.Add(MyCommand)
' Invoke and return the results
Dim RetVal As Collection(Of PSObject) = pipeLine.Invoke()
pipeLine.Dispose()
Return RetVal
End SyncLock
End Function
'-------------------------------------------------------------
' AddBlockedAddress
'-------------------------------------------------------------
Public Function AddBlockedAddress(ByVal strAddress As String, ByVal
ExpirationDate As Date) As Boolean
SyncLock objLock
Try
Dim retval As Collection(Of PSObject)
' Create our command
Dim MyCommand As New Command("Add-IPBlockListEntry")
MyCommand.Parameters.Add("IPAddress", strAddress)
If (Not ExpirationDate = Date.MaxValue) Then
MyCommand.Parameters.Add("ExpirationTime", ExpirationDate)
End If
Dim strCommand As String = "Add-IPBlockListEntry -IPAddress """ +
strAddress + """"
retval = RunCommand(MyCommand)
Return (retval.Count = 0)
Catch ex As Exception
Throw New Exception("Error: Unable to add new block list entry.
Reason = " + ex.Message)
End Try
End SyncLock
End Function
'-------------------------------------------------------------
' GetBlockedIPAddresses
'-------------------------------------------------------------
Public Function GetBlockedIPAddresses() As List(Of clsBlockedIPAddress)
SyncLock objLock
Dim IPs As New List(Of clsBlockedIPAddress)
Try
Dim retval As Collection(Of PSObject)
' Create our command
Dim MyCommand As New Command("Get-IPBlockListEntry")
retval = RunCommand(MyCommand)
For Each pso As PSObject In retval
IPs.Add(New clsBlockedIPAddress(pso))
Next
Return IPs
Catch ex As Exception
Throw New Exception("Error: Unable to add new block list entry.
Reason = " + ex.Message)
End Try
End SyncLock
End Function
End Class


