Execute a one-way wcf service operation from powershell

后端 未结 5 1731
情歌与酒
情歌与酒 2021-02-06 12:52

I have a scheduled task which executes a powershell script every hour. The powershell script must make a call to a one-way WCF service operation. Essentially it just needs to

5条回答
  •  我在风中等你
    2021-02-06 13:34

    I like jdmichal's answer because it is intuitive to make it work for inputs where the parameters are objects... the code required some modifications, this is what worked for me:

    [Reflection.Assembly]::LoadFrom("C:\.....\WcfService2.dll")
    [Reflection.Assembly]::LoadWithPartialName("System.ServiceModel")
    
    $httpBinding = New-Object System.ServiceModel.BasicHttpBinding
    if($useNTLM){
        #passes the default creds
        $httpBinding.Security.Mode = "TransportCredentialOnly"
        $httpBinding.Security.Transport.ClientCredentialType = "Ntlm"
    }
    $endpointAddress = New-Object System.ServiceModel.EndpointAddress 'http://localhost:63600/Service1.svc'
    
    $contractDescription = [System.ServiceModel.Description.ContractDescription]::GetContract([WcfService2.IService1])
    
    $serviceEndpoint = New-Object System.ServiceModel.Description.ServiceEndpoint($contractDescription, $httpBinding, $endpointAddress)
    $channelFactory = New-Object "System.ServiceModel.ChannelFactory``1[WcfService2.IService1]"($serviceEndpoint)
    
    $webProxy = $channelFactory.CreateChannel();
    $result = $webProxy.GetData(123);
    Write-Output $result #prints 123
    $comptype = new-object WcfService2.CompositeType
    $comptype.BoolValue =$false
    $comptype.StringValue = "whatever123zxcv"
    $result = $webProxy.GetDataUsingDataContract($comptype);
    Write-Output $result #prints whatever123zxcv
    

    UPDATE: I've found out how to use composite objects with the New-WebServiceProxy cmdlet (thanks to http://www.sqlmusings.com/2012/02/04/resolving-ssrs-and-powershell-new-webserviceproxy-namespace-issue/)

    $wsProxy = New-WebServiceProxy -uri http://localhost:63600/Service1.svc
    $wsProxy.UseDefaultCredentials = $true
    $namespace = $wsProxy.GetType().Namespace
    $myct = New-Object "$namespace.CompositeType"
    $myct.BoolValue = $true;
    $myct.StringValue = "asdfasdg";
    
    $wsProxy.GetDataUsingDataContract($myct)
    $wsProxy.GetData(123,$true)
    

提交回复
热议问题