title: Powershell New-WebServiceProxy - how to call WCF service
body: <p>I'm trying to call a WCF service from PowerShell.</p>
  <p>This is what I have so far based on a few misc example I found on the web:</p>
  <pre><code># Create the WebSvcURL variable and pass the WSDL URL
  $WebSvcURL= “http://localhost/DEMO/SetPassKey/SetPassKey_Logic_SetPassKeyWebService_SetPassKeyWCF.svc?wsdl“
  
  #Create the Web Service Proxy Object
  
  #$serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -Namespace &quot;http://Sample.SetPassKey.Logic&quot; -Class Program -UseDefaultCredential
  $serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -UseDefaultCredential
  
  # Create Request Object
  $namespace = $serviceProxy.getType().namespace
  write-host &quot;Namespace=$namespace&quot;
  $req  = New-Object ($namespace + &quot;/SetPassKeyOrchWebRequest&quot;)  
  $resp = New-Object ($namespace + &quot;/SetPassKeyOrchWebResponse&quot;)  
  
  $req.NewPassKey = &quot;TEST&quot;    # &lt;--- PUT YOUR NEW PASSWORD HERE 
  
  $resp = $serviceProxy.SetPassKey($req) 
  
  $resp 
  </code></pre>
  <p>I'm confused on a few things:</p>
  <ol>
  <li>If and when I need to include the -Namespace and -Class parameters on the. Since it looks at the WSDL, I don't understand why it needs the -Namespace and -Class.</li>
  <li>How to construct the request/response objects</li>
  <li>Should the URL be the actual URL of the web service or should it include the ?wsdl suffix</li>
  </ol>
  <p>UPDATE:
  I found this <a href="https://sarafian.github.io/amadeus/2019/06/28/powershell-soap-proxy-new-webserviceproxy.html" rel="nofollow noreferrer">blog</a> which states:</p>
  <blockquote>
  <p>The -Namespace parameter is optional and when not
  specified then it gets a random value from the cmdlet.</p>
  </blockquote>
  <p>I was thinking it was an XML Namespace, but it's a .NET framework namespace.</p>
  <pre><code># Create the WebSvcURL variable and pass the WSDL URL
  $WebSvcURL= “http://localhost/DEMO/SetPassKey/SetPassKey_Logic_SetPassKeyWebService_SetPassKeyWCF.svc?wsdl“
  
  #Create the Web Service Proxy Object
  
  $serviceProxy = New-WebServiceProxy -Uri $WebSvcURL -UseDefaultCredential -Namespace &quot;MyNamespace&quot; -Class Program 
  
  # Create Request Object
  $req  = New-Object (&quot;MyNamespace.SetPassKeyOrchWebRequest&quot;)  
  $resp = New-Object (&quot;MyNamespace.SetPassKeyOrchWebResponse&quot;)  
  
  $req.NewPassKey = &quot;TEST&quot;    # &lt;--- PUT YOUR NEW PASSWORD HERE 
  
  $resp = $serviceProxy.SetPassKey($req) 
  
  $resp 
  </code></pre>
  <p>Now I get this error:
  Exception calling &quot;SetPassKey&quot; with &quot;1&quot; argument(s): &quot;The underlying connection was closed: An unexpected error occurred on a
  receive.&quot;</p>
  <p>The webservice can be called by a C# console program, so I know it works. Just need to call it from PowerShell.</p>