New to objects, trying to convert C#

Still learning PS and struggling with objects. I have sample C# code and can not properly implement it in PS.

//Instantiate the web service as an object.
Service1.Service1 obj = new Service1.Service1();
//Instantiate the authentication as an object.
Service1.ValidationSoapHeader authentication = new
//Assign a DevToken value to the authentication object.
authentication.DevToken = token
obj.ValidationSoapHeaderValue = authentication;
//Build the XML Input String
xmlInput = somexml
//Send the XML Input to the MWS
serviceOutput = obj.OrderList(xmlInput);

Here is where I am at with the PS. Obviously this does not work...

$obj = New-WebServiceProxy -Uri $URI -Namespace "WSProxy"-Class "Midnight"
$authentication = New-Object ("WSProxy.ValidationSoapHeader")

$authentication.DevToken = $token
$obj.ValidationSoapHeaderValue = $authentication

# This line works
$obj | Get-Member -MemberType method

$output = $obj.OrderList($inputXML)

Exception calling "OrderList" with "1" argument(s): "System.Web.Services.Protocols.SoapException: Server 
request. ---> System.InvalidOperationException: The ConnectionString property has not been initialized.

Hi, welcome to the forum :wave:

I’m not sure there’s enough information here for people to help you.

The OrderList method will be specific to the object referenced by $obj which is being returned by whichever service you’re accessing. If it’s a public service or a service that publishes their documentation can you share a link?

How are you setting the $inputXML variable and what is its value?

Thanks for the reply. I thought my problem was just on how the objects were being built, specifically the authentication part. Did I translate the C# example to PS correctly? If so, I am proud! Here is the rest of the code and resources, hope this helps.

$inputXML = "<OrderList>" + "<InputParameter>" + "<OrderNumber>" + "1528" + "</OrderNumber>" + "<CustomerCode></CustomerCode>" + "<CustomerID></CustomerID>" + "<OrderDateFrom></OrderDateFrom>" + "<OrderDateTo></OrderDateTo>" + "<OrderStatus></OrderStatus>" + "</InputParameter>" + "</OrderList>"
$URI = ""

$obj = New-WebServiceProxy -Uri $URI -Namespace "WSProxy"-Class "Midnight"
$authentication = New-Object ("WSProxy.ValidationSoapHeader")

$authentication.DevToken = $token
$obj.ValidationSoapHeaderValue = $authentication

# This line works
$obj | Get-Member -MemberType method

$output = $obj.OrderList($inputXML)

Your conversion looks OK to me :+1:

I can replicate the error after adding the $authentication object to the ValidationSoapHeaderValue field. I’m using a made up value for the token but I get a different error when it’s not present.

As the object seems to be created properly, I wonder if you just have an authentication problem.

I’m quite happy I at least got that part right! Cool! I am getting help from another person who has provided a different approach but I am getting a different error.

Function GetOrderIDfromOrderNumber() {
    param (
    $devToken = "removed"
    $soapUri = ""
    $midNightWS = New-WebServiceProxy -Uri $soapUri -Namespace WebServiceProxy -Class Service1
    $midNightWSAuth = New-WebServiceProxy -Uri $soapUri -Namespace WebServiceProxy -Class ValidationSoapHeader
    $midNightWSAuth.DevToken = $devToken
    $midNightWS.ValidationSoapHeaderValue = $midNightWSAuth
    $xmlInput = "<OrderList>" +
    "<InputParameter>" +
        "<OrderNumber>" + $OrderNum + "</OrderNumber>" +
        "<CustomerCode></CustomerCode>" +
        "<CustomerID></CustomerID>" +
        "<OrderDateFrom></OrderDateFrom>" +
        "<OrderDateTo></OrderDateTo>" +
        "<OrderStatus></OrderStatus>" +
    "</InputParameter>" +
  # Replace "ASDASD" with a valid order number
  $soapOutput = GetOrderIDfromOrderNumber("1528")
  New-WebServiceProxy : Could not load file or assembly 
'file:///C:\Users\jflener\AppData\Local\Temp\megcv3uz.dll' or one of its dependencies. The system       
cannot find the file specified.
At C:\Users\jflener\Downloads\soapApi.ps1:10 char:19
+ ... idNightWS = New-WebServiceProxy -Uri $soapUri -Namespace WebServicePr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-WebServiceProxy], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.NewWebServi 

New-WebServiceProxy : Could not load file or assembly 
'file:///C:\Users\jflener\AppData\Local\Temp\ez3qhm4k.dll' or one of its dependencies. The system 
cannot find the file specified.

Also in that error message the dll is a random name each time I run

The class isn’t Service1 it’s Midnight. That should get rid of the error from the first New-WebServiceProxy. I’m not sure why you’d create a second one for auth or what the class would be if you do that.

Have you been able to confirm that your token works?

A couple of other suggestions. Instead of sending a String try an XML object. I don’t think that’s required from the code sample, but it’s worth a try. I’d change your code to use a Here String rather than contatenation.

[XML]$xmlInput = @"
        <OrderNumber> $OrderNum </OrderNumber>

The other option worth testing would be to build what you’re sending using the examples for PHP or VBA and try sending it with Invoke-WebRequest or Invoke-RestMethod. Here’s an example:

I got a little bit further by changing up a couple lines and got rid of the first error. I’m really not sure what is going on as this is now above my understanding but hopefully this helps. I hope I can get this method working as it looks better but I will try the PHP method too.

    $midNightWS = New-WebServiceProxy -Uri $soapUri -Class Midnight
    $midNightWSAuth = [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy2idnight_com_Service1_asmx.ValidationSoapHeader]::new()

Unable to find type [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy2idnight_com_Service1_asmx.ValidationSoapHeader].
At T:\Automation Workspace\midnight\soap.ps1:14 char:23
+ ... ghtWSAuth = [Microsoft.PowerShell.Commands.NewWebserviceProxy.Autogen ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Power...ationSoapHeader:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

I tried that without the -Class and with Midnight as the name and both return same error

I also try this way and get another error.

$soapUri = ""
    $midNightWS = New-WebServiceProxy -Uri $soapUri -Class Midnight
    $midNightWSAuth = New-WebServiceProxy -Uri $soapUri -Class ValidationSoapHeader
    $midNightWSAuth.DevToken = $devToken

The property 'DevToken' cannot be found on this object. Verify that the property exists and can be set.
At T:\Automation Workspace\midnight\soap.ps1:15 char:5
+     $midNightWSAuth.DevToken = $devToken
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
Exception setting "ValidationSoapHeaderValue": "Cannot convert the 
"Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1s_vsmidnight_com_Service1_asmx.Service1" value of type 
"Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1s_vsmidnight_com_Service1_asmx.Service1" to type       
At T:\Automation Workspace\midnight\soap.ps1:16 char:5
+     $midNightWS.ValidationSoapHeaderValue = $midNightWSAuth
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

I’m not sure there was anything wrong with your first solution. It would appear that everything is being created properly in that solution but there’s a possible problem with the token.

I think I’ve managed to put together the correct code to test with Invoke-WebRequest can you give this a try with your token? It seems to give a more verbose error message (see below).

$URI = ""
$header = @{"Content-Type" = "text/xml"}

$body = @'
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv='' xmlns:vsm=''>

Invoke-WebRequest -Uri $URI -Method Post -Header $header -Body $body


The ConnectionString property has not been initialized.
   at System.Data.SqlClient.SqlConnection.PermissionDemand()


Thank you, this one works. And I remember now I saw that error before and did not realize it was a credential error. I much to do today now to try and wrap my head around all of this.

Would you believe my token had a $23 in the middle of it? I’m so embarrassed right now…

Glad to hear it’s working for you. Have you tried your original code with the correct token? New-WebServiceproxy is the preferred way of doing things.

Yes, it works with my original code. Thank you for your help, it was really an extra effort you put in to help me and I appreciate it.

1 Like