New-WebServiceProxy as way for update information

Hi Powershell Experts,

Currently we are developing a script that allows to open/update incidents in service-now automatically, allowing to monitoring incidents directly with the user, crossing our ticket platform. In the past, I did we could open tickets from the script. I explain a little which steps we are using:

  1. We save the mail and webpage in two variables. The credentials used are mail (for example my mail account)

$SN_Mail = “hexagontechnologytest@service-now.com
$SN_Web = “https://hexagontechnology.service-now.com/incident.do?WSDL
$cred = Get-Credential

  1. We set a web-proxy connection using the Incident Namespace saved in a object

$SNowWS_Incidents = New-WebServiceProxy -Uri $uri -Namespace Incidents -Credential $credential

  1. We define a object called $records using the type Incidents.getRecords. Then we can set attributes for filtering the query (here we want active incidents but we can extend with a lot of properties like we fill in Service-Now when we open a incident).

$records = New-Object Incidents.getRecords
$records.active = $true
$records.activeSpecified = $true

  1. The webproxy object include a function called getRecords which parameter is a record object with attributes defined previously. This function will return all incidents which include the same attributes (in this case all incidents active).

$INC = $SNowWS_Incidents.getRecords($records)

first execution: we receive the correct information.

Well until now, looks good and should work but here is when I need your knowledge and experience. If I execute the same code a second time, I receive the following error:

ERROR: Cannot convert argument “getRecords1”, with value: “Incidents.getRecords”, for “getRecords” to type “Incidents.getRecords”: “Cannot convert the
ERROR: “Incidents.getRecords” value of type “Incidents.getRecords” to type “Incidents.getRecords”.”

I used PowerShell studio, PowerShell GUI, PowerShell ISE, PowerShell consoled and the error successes in all. We have no idea why we receive this error. Could be coming from PowerShell but maybe you have knowledge about the error or logic problem.

Thanks so much for your support.

This is a wild guess, but try structuring your code so that New-WebServiceProxy is only called once, and stored in something like a module’s script-scoped variable, and then only repeat the code in steps 3-4 and see what happens. I suspect that when you call New-WebServiceProxy more than once with the same -Namespace value, it might be making some funny things happen under the hood with the auto-generated types.

Hi Dave,

I will follow your advice. Thanks so much as soon as I have a score I will share it.

Hi Dave,

I think my problem is here, in this line.

  • $SNowWS_Incidents = New-WebServiceProxy -Uri $uri -Namespace Incidents -Credential $credential

If I execute 2,3,…10 times the script, is when I receive the error. But if I change the -Namespace then works

  1. Execution: -Namespace Incidents
  2. Execution: -Namespace Incidents2
  3. Execution: -Namespace Incidents3

I extended the code doing

$global:SNowWS_Incidents = New-WebServiceProxy -Uri $uri -Namespace Incidents -Credential $credential
$SNowWS_Incidents.Dispose()

It not solves the problem.

that means namespace should be modified in each execution while the script is not closed.

Cool, glad it’s working. :slight_smile:

Well the problem defined is … you can not execute the script more than one time because the namespace produces an error … could be a bug, I don’t know, but I don’t like it because that means I need to close the script and open again for each execution, that means to lose time :frowning:

Now my feeiling is: how can I solve this problem? :slight_smile: , google shows few information about it.

Thanks for your support dave

Nevermind, I misunderstood. I thought you said calling Dispose() on the proxy solved your problem.

I’d still go back to my original suggestion, then. Put this code into a psm1 file and structure it like this:

$script:proxy = New-WebServiceProxy # parameters

function Do-Something
{
    $script:proxy.doSomething()
}

This way, the proxy is only created once when you import the module, and every time you call your function, it just uses the existing object.

Dave, I will apply your idea with a little change but I am not using a module, is a simple script :slight_smile:

The module will be done in the future If I have skills for doing it.

Solution:

$SNowWS_Incidents = New-WebServiceProxy -Uri $SN_Web -Credential $cred #don’t define the namespace here
$type = $SNowWS_Incidents.GetType().Namespace
$records = new-object ($type + ‘.getRecords’)

now it is solved :slight_smile:

Nice, cool fix! :slight_smile: