I have an ASP.NET 3.5 application running Powershell scripts to add/update Exchange mailboxes. Most of the Powershell commands work great, but two of them so far have caused an error: get-calendarprocessing and set-calendarprocessing, which get or set users assigned to schedule a Room. The error is System.Management.Automation.CmdletInvocationException: Operation is not valid due to the current state of the object. When I run the commands from the Exchange Management Shell, they work fine. We have patched and rebooted the Exchange server.
Command example: get-calendarprocessing Room.1 | select -expand bookinpolicy (or similar variations)
Private Function RunScript(ByVal ScriptFileName As String, ByVal Arguments() As String) As Collection(Of PSObject)
Dim sb As New StringBuilder
Dim strScript As String
' create Powershell runspace and open
If psRunspace Is Nothing Then
psRunspace = InitializeRunspace()
End If
'Grab Powershell script from text (.ps1) file
strScript = File.ReadAllText(ScriptFileName)
'inject the arguments into the script
strScript = InsertArguments(strScript, Arguments)
Logging.LogMessage(strScript)
'Open the runspace and create a pipeline if it's not already open
If psRunspace.RunspaceStateInfo.State = RunspaceState.BeforeOpen Then
psRunspace.Open()
End If
Dim MyPipeline As Pipeline = psRunspace.CreatePipeline()
MyPipeline.Commands.AddScript(strScript)
Try
Dim psResults As Collection(Of PSObject)
psResults = MyPipeline.Invoke() 'ERRORS HERE
Return psResults
Catch ex As Exception
Logging.LogMessage(ex.Message)
Throw ex
End Try
End Function