powershellwebrequestnew-webserviceproxy

Powershell: invoking New-WebServiceProxy ruins following Invoke-WebRequest


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:

Any advice on this is greatly appreciated, thanks!


Solution

  • 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.