title: Using Powershell to call a WCF service method
body: <p>I have a WCF service that uses wsHttpBinding with message security and clientcredentialtype as windows, and the service has a simple method</p>
  
  <pre><code>[OperationContract]
  string SayHello();
  
  public string SayHello()
      {
          return "HELLO";
      } 
  
  &lt;wsHttpBinding&gt;
      &lt;binding name="WSHttpBinding"&gt;          
        &lt;security mode="Message"&gt;
          &lt;message clientCredentialType="Windows" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/wsHttpBinding&gt;
  </code></pre>
  
  <p>I am trying to execute the below on powershell(version >= 2) and I get the below error</p>
  
  <pre><code>$wshttpbinding= New-WebServiceProxy -uri http://localhost:52871/Service.svc -Credential DOMAIN\gop
  
  PS&gt; $wshttpbinding.SayHello.Invoke()
      Exception calling "SayHello" with "0" argument(s): "The operation has timed out"
      At line:1 char:1
      + $wshttpbinding.SayHello.Invoke()
      + ~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>
  
  <p>However when i changed the binding to use basicHttpBinding, it works fine</p>
  
  <pre><code>&lt;basicHttpBinding&gt;
            &lt;binding name="basicconfig" 
        &lt;security mode="TransportCredentialOnly"&gt;
          &lt;transport clientCredentialType="Windows" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
  &lt;/basicHttpBinding&gt;
  
  $basichttpbinding= New-WebServiceProxy -uri http://localhost:52871/Service.svc -Credential DOMAIN\gop
  
  PS&gt; $basichttpbinding.SayHello.Invoke()
  HELLO   
  </code></pre>
  
  <p>Is there anything differently that i need to do in my script when using wsHttpBinding ?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Final Approach</strong>
  I was using wsHttpBinding only for WCF transaction support. However I quickly realised that the service method call that was required to be called by the powershell script has nothing to do with transactions. Hence I exposed another  BasicHttpBinding endpoint with Windows Authentication and it worked with the below script. See snippet  below</p>
  
  <pre><code>Try
  {
      $cred = new-object -typename System.Management.Automation.PSCredential ` -argumentlist $username, $password -ErrorAction Stop
  }
  Catch {
      LogWrite "Could not create PS Credential"
      $credErrorMessage = $_.Exception.Message
      LogWrite $credErrorMessage
      Break
  }
  
  Try{
      $service=New-WebServiceProxy –Uri $url -Credential $cred -ErrorAction Stop
  } Catch {
      LogWrite "Could not create WebServiceProxy with $url"
      $proxyErrorMessage = $_.Exception.Message
      LogWrite $proxyErrorMessage
      Break
  }
  
  # Create Request Object
  $namespace = $service.getType().namespace
  $req = New-Object ($namespace + ".UpdateJobRequest")    
  
  LogWrite "Calling service..."
  $response = $service.UpdateJob($req)
  </code></pre>