카테고리 없음

[.그물] PowerShell에서 단방향 WCF 서비스 작업 실행

행복을전해요 2020. 12. 31. 09:15

PowerShell 2.0은 New-WebServiceProxy cmdlet을 사용하여 간단하게 만듭니다. 예 :

$zip = New-WebServiceProxy -uri http://www.webservicex.net/uszip.asmx?WSDL
$zip.getinfobyzip(20500).table

CITY      : Washington
STATE     : DC
ZIP       : 20500
AREA_CODE : 202
TIME_ZONE : E
-------------------

문제는 코드가 실제로 WCF 요청이 아닌 HttpWebRequest를 만드는 것 입니다. (즉, SOAP 또는 .NET Remoting 정보없이 URL에서 HTTP GET 요청을 실행하는 것입니다.)

다음 지침에 따라 적절한 엔드 포인트를 생성 할 수 있어야합니다.

http://msdn.microsoft.com/en-us/magazine/cc163647.aspx#S11

다음과 같이 보일 것입니다.

$httpBinding = New-Object System.ServiceModel.BasicHttpBinding
$endpointAddress = New-Object System.ServiceModel.EndpointAddress 'http://myserver.com/myservice/dosomething'
$contractDescription = [System.ServiceModel.Description.ContractDescription]::GetContract([IYourInterface], $httpBinding, $endpointAddress)
$serviceEndpoint = New-Object System.ServiceModel.Description.ServiceEndpoint $contractDescription
$channelFactory = New-Object "System.ServiceModel.ChannelFactory``1[IYourInterface]" $serviceEndpoint
$webProxy = $channelFactory.CreateChannel();
$webProxy.yourServiceMethod();

IYourInterface이 작업을 수행 하려면 클래스 와 함께 DLL을 가져와야합니다 .

[void] [Reflection.Assembly]::LoadFrom('path/to/your.dll')

또는 서비스에 대해 정의 된 WSDL이있는 경우 다음과 같은 훨씬 더 쉬운 지침에 따라 서비스에 액세스 할 수 있습니다.

http://blogs.technet.com/heyscriptingguy/archive/2009/11/17/hey-scripting-guy-november-17-2009.aspx

또는 HTTP SOAP 요청이 어떻게 생겼는지 파악하고 HttpWebRequest.

-------------------

응답을 받기 전에 먼저 HTTP 메서드를 설정해야한다고 생각합니다. 기본적으로 WebRequest 객체의 기본값은 POST 라고 생각 하지만 실제로는 GET 으로 설정해야합니다 .

PowerShell 스크립팅을 많이 사용하지는 않았지만 샘플을 기반으로 할 때 다음과 같이 보일 것입니다.

$request = [System.Net.WebRequest]::Create("http://myserver.com/myservice/dosomething") 
$request.Method = "GET"
$request.GetResponse()

도움이 되었기를 바랍니다.

-------------------

HTTP 재생 방식 :

  1. Fiddler를 설치하고 실행하고 요청 기록 시작
  2. '일반'클라이언트를 사용하여 WCF 서비스 호출 (요청 기록)
  3. Invoke-WebRequest 사용-XML SOAP 요청 본문 및 일부 헤더를 재생하고 헤더 (Content-Type, SOAPAction 및 기타 가능)와 일치하는지 확인하십시오.
  4. 반복 (3)하고 유효한 SOAP 요청이있을 때까지 본문과 헤더를 계속 조정하십시오.

WCF 서비스를 호출하기 위해 WCF를 사용할 필요가 없습니다. HTTP 기본값에서 3 ~ 4 개의 변경 사항을 적용 할 수 있습니다. SOAP 봉투를 직접 구축하려고하지 마십시오. 하지만 '일반'HTTP를 통해 Fiddler에서 필요한 모든 것을 재생하는 것이 안전합니다.

-------------------

매개 변수가 객체 인 입력에 대해 작동하도록 직관적이기 때문에 jdmichal의 대답을 좋아합니다 ... 코드는 약간의 수정이 필요합니다. 이것은 나를 위해 일한 것입니다.

[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
            

업데이트 : New-WebServiceProxy cmdlet ( http://www.sqlmusings.com/2012/02/04/resolving-ssrs-and-powershell-new-webserviceproxy-namespace 덕분에)으로 복합 개체를 사용하는 방법을 알아 냈습니다 . -발행 / )

$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)


출처
https://stackoverflow.com/questions/2003049