Powershell: invoking New-WebServiceProxy ruins following Invoke-WebRequest

1.4k views Asked by At

The story so far: i'm writing a 'create-new-user' PoSh script, among all else i need it to:

  1. create WebServiceProxy object as variable (since Cisco UCM we use utilizes SOAP requests to interact with it)
  2. pull a PHP script on our Intranet website to sync it with Active Directory once the user is created, the easiest way is AFAIR Invoke-WebRequest commandlet.

So now it looks like this:

$MyCreds = Get-Credential -Message "Specify your password" -Username $($env:username)
$AXL = New-WebServiceProxy -Uri $("\\server\smbshare\AXLAPI.wsdl") -Credential $MyCreds
...
Invoke-WebRequest -Uri "https://intranet.company.com/ad_sync.php" -Method Get

What I found out is that invoking WebRequest before creating an WebServiceProxy object succeeds, however invoking WebRequest any time after fails with the following error:

Invoke-WebRequest: The underlying connection was closed: An unexpected error occurred on a send.

I have no experience with .NET, classes and such stuff so perhaps i'm wrong in my assumptions but for now i'm trying (and failing) to find out if this behavior is expected? Basic logic suggests that creating a variable shouldn't affect invoking simple requests, but still it does. So, i'd be grateful for a link to read which explains how-it-works.

And one more, the main question - is there a solution or at least a workaround? My theoretical suggestions are like removing the WebServiceProxy object out of the way (no idea how) or using alternative way of pulling a PHP script.

Some additional info:

  • Powershell version: 4.0
  • WebServiceProxy part is provided by a colleague so i'm not too familiar with it, just have to compose everything in one script
  • we cannot change the order of commands due to script logic, it has to be done in this particular order

Any advice on this is greatly appreciated, thanks!

1

There are 1 answers

0
TravisEz13 On BEST ANSWER

However New-WebServiceProxy mucks with Invoke-WebRequest it is probably only in the current process. So, a workaround is to run it out of the current process.

So change

Invoke-WebRequest -Uri "https://intranet.company.com/ad_sync.php" -Method Get

To

Start-Job -ScriptBlock { Invoke-WebRequest -Uri "https://intranet.company.com/ad_sync.php" -Method Get} | Receive-Job -Wait

This will run it in another process and wait for the results.