New-WebServiceProxy - can't assign a value to MemberType Property

Hello,

I try to post a soap to an API on my servers but I’m not able to make it working with the New-WebServiceProxy Cmdlet. I’m not able to assign a username and password to the Security property.

When I use the Invoke_Webrequest cmdlet I’ve got a correct response.

Example1 PS1-script:

$body = “<soap:Envelope xmlns:soap=‘http://www.w3.org/2003/05/soap-envelope’ xmlns:oas=‘http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd’ xmlns:urn=‘urn:vpom-ws’>
<soap:Header>
<oas:Security>
<oas:UsernameToken>
<oas:Username>username</oas:Username>
<oas:Password>password</oas:Password>
</oas:UsernameToken>
</oas:Security>
</soap:Header>
<soap:Body>
<urn:VPOMGetJobInfo>
<urn:entryIdentifier>2845708</urn:entryIdentifier>
</urn:VPOMGetJobInfo>
</soap:Body>
</soap:Envelope>”

$Request = Invoke-WebRequest -Credential $Credential -Uri http://10.2.206.11:8081 -Headers (@{SOAPAction=‘Read’}) -Method Post -Body $Body -ContentType application/xml
$Request.Content

Result1 = ok

 

When I try to use New-WebServiceProxy and call the “VPOMGetJobInfo”-method I’ve got an authentication problem.

Example2 PS1-script:

$url = “D:\VPOM_API\vpomwsapi.wsdl”
$proxy = New-WebServiceProxy -uri $url -UseDefaultCredential -Namespace “VpomApi”
$proxy.VPOMGetJobInfo(2044814)

Result2 = not ok

Exception calling “VPOMGetJobInfo” with “1” argument(s): “VPOMGetJobInfo
Authentication Required”
At line:3 char:1
+ $proxy.VPOMGetJobInfo(2044814)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : SoapException

When I perform a Get-Member on the $proxy I see that I have a property “Security” which I suppose is the property where I should assign the username and password to which I in example1 added in the Header of the request.

This is the result of the gm.

$proxy | gm

Disposed Event System.EventHandler Disposed...
VPOMGetDeviceInfoCompleted Event VpomApi.VPOMGetDeviceInfoCom...
VPOMGetJobInfoCompleted Event VpomApi.VPOMGetJobInfoComple...
VPOMQueueRequestCompleted Event VpomApi.VPOMQueueRequestComp...
VPOMRegionRequestCompleted Event VpomApi.VPOMRegionRequestCom...
Abort Method void Abort()
BeginVPOMGetDeviceInfo Method System.IAsyncResult BeginVPO...
BeginVPOMGetJobInfo Method System.IAsyncResult BeginVPO...
BeginVPOMQueueRequest Method System.IAsyncResult BeginVPO...
BeginVPOMRegionRequest Method System.IAsyncResult BeginVPO...
CancelAsync Method void CancelAsync(System.Obje...
CreateObjRef Method System.Runtime.Remoting.ObjR...
Discover Method void Discover()
Dispose Method void Dispose(), void IDispos...
EndVPOMGetDeviceInfo Method VpomApi.DeviceInfoResponse, ...
EndVPOMGetJobInfo Method VpomApi.JobInfoResponse, 5u3...
EndVPOMQueueRequest Method uint64 EndVPOMQueueRequest(S...
EndVPOMRegionRequest Method VpomApi.VPOMRegionJobInfo[],...
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeSer...
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLife...
ToString Method string ToString()
VPOMGetDeviceInfo Method VpomApi.DeviceInfoResponse, ...
VPOMGetDeviceInfoAsync Method void VPOMGetDeviceInfoAsync(...
VPOMGetJobInfo Method VpomApi.JobInfoResponse, 5u3...
VPOMGetJobInfoAsync Method void VPOMGetJobInfoAsync(uin...
VPOMQueueRequest Method uint64 VPOMQueueRequest(Vpom...
VPOMQueueRequestAsync Method void VPOMQueueRequestAsync(V...
VPOMRegionRequest Method VpomApi.VPOMRegionJobInfo[],...
VPOMRegionRequestAsync Method void VPOMRegionRequestAsync(...
AllowAutoRedirect Property bool AllowAutoRedirect {get;...
ClientCertificates Property System.Security.Cryptography...
ConnectionGroupName Property string ConnectionGroupName {...
Container Property System.ComponentModel.IConta...
CookieContainer Property System.Net.CookieContainer C...
Credentials Property System.Net.ICredentials Cred...
EnableDecompression Property bool EnableDecompression {ge...
PreAuthenticate Property bool PreAuthenticate {get;set;}
Proxy Property System.Net.IWebProxy Proxy {...
RequestEncoding Property System.Text.Encoding Request...
Security Property VpomApi.SecurityHeaderType, ...
Site Property System.ComponentModel.ISite ...
SoapVersion Property System.Web.Services.Protocol...
Timeout Property int Timeout {get;set;}
UnsafeAuthenticatedConnectionSharing Property bool UnsafeAuthenticatedConn...
Url Property string Url {get;set;}
UseDefaultCredentials Property bool UseDefaultCredentials {...
UserAgent Property string UserAgent {get;set;}

<wbr /><wbr />

Could anybody help me out here because I don't understand how to assign the value to the security header.

Thank you.

Kind regards,

Koen

 

 

 

In your Invoke-WebRequest example, you’re specifying a credential ($credential) but in your New-WebServiceProxy example you’re using -UseDefaultCredential which, by default, is the local user. Does that local user have access to the service? If not you will to specify an alternate account.

$credential = Get-Credential
$url = “D:\VPOM\_API\vpomwsapi.wsdl”
$proxy = New-WebServiceProxy -uri $url -Credential $credential -Namespace “VpomApi”
$proxy.VPOMGetJobInfo(2044814)

 

Hi Matt,

I did some additional testing.

  1. In Invoke-WebRequest the -Credential is not taken in consideration. As you can see there is nowhere in the script I'm assigning a value to it. I did a couple additional tests.
    1. I always removed the -Credential $Credential from the script.
    2. In the header I used as username and password my admin-account, which I use to run Powershell, and I tried the same soap-request with an application-pool user. Both times were succesfull. When I use another normal Active-directory user an error will be generated.
  2. Regarding New-WebServiceProxy I added following lines in the beginning of the script:
$username = "username"
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
I think this is what you asked me to do. Unfortunately I still get the same error.

[quote quote=272935]Hi Matt,

I did some additional testing.

  1. In Invoke-WebRequest the -Credential is not taken in consideration. As you can see there is nowhere in the script I’m assigning a value to it. I did a couple additional tests.
    1. I always removed the -Credential $Credential from the script.
    2. In the header I used as username and password my admin-account, which I use to run Powershell, and I tried the same soap-request with an application-pool user. Both times were succesfull. When I use another normal Active-directory user an error will be generated.
  2. Regarding New-WebServiceProxy I added following lines in the beginning of the script:
PowerShell
<textarea class="urvanov-syntax-highlighter-plain print-no" style="-moz-tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">$username = "username" $password = ConvertTo-SecureString "password" -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)</textarea>
1
2
3
$username = "username"
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
I think this is what you asked me to do. Unfortunately I still get the same error.

[/quote]
It might be interesting to know that as long as I didn’t add security in the header of the soap post with Invoke-WebRequest , that also didn’t work.

Could it be that the solution could be found in the Security property?

Exception calling "VPOMGetJobInfo" with "1" argument(s): "VPOMGetJobInfo 
Authentication Required"
At line:7 char:1
+ $proxy.VPOMGetJobInfo(2845708)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SoapException
 


   TypeName: VpomApi.VPOMService

Name     MemberType Definition                                                    
                                                 
----     ---------- ----------                                                    
                                                 
Security Property   VpomApi.SecurityHeaderType, xzyyv4j3, Version=0.0.0.0, Culture
=neutral, PublicKeyToken=null Security {get;set;}

 

 

If that is the case, I don’t think you can do this with New-WebServiceProxy because there’s no easy way to send custom headers.

See: https://powershell.org/forums/topic/how-to-add-ws-security-element-to-soap-request-in-powershell/

[quote quote=272965]If that is the case, I don’t think you can do this with New-WebServiceProxy because there’s no easy way to send custom headers.

See: https://powershell.org/forums/topic/how-to-add-ws-security-element-to-soap-request-in-powershell/

[/quote]
I think we can consider this post resolved then.

This is the first time I create post here. On many forums posts have a status and you have to change it at the end to “solved” FI. Not here I think or do I overlook anything.

Thank you very much for your help

No problem and don’t worry, you’re not overlooking anything; only moderators can lock the threads.