Forum Discussion
How to use Office 365 Service Communications API V2 with PowerShell
We are using almost same script which works just fine when run manually. However, when running as a scheduled job, it fails with below error. Initial auth token call is successful but fails on next call to retrieve messages. Any idea/suggestions? Appreciate it!
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
I believe I've run into that error, or a similar one, when connections are being blocked by firewall or some other locally running security app. Do you have anything like that running on this machine? If so, I'd suggest temporarily disabling to see if that allows you to run the scheduled script.