問題:對ServiceNow的../cmdb_ci_win_server.do?WSDL表的所有Web服務調用均返回零結果。我可以使用PowerShell的New-WebServiceProxy過濾基於WSDL的SOAP查詢以僅發送一個參數嗎?
原因:我使用PowerShell的New-WebServiceProxy方法,該方法從ServiceNow提供的WSDL定義中動態創建.NET .dll。 WSDL定義了174個參數,我希望只針對其中的一個參數進行定義和查詢,但在執行時,.dll總是在其查詢的WHERE子句中發送其他173個空參數,這顯然會導致不匹配的情況。
希望:動態.dll讓我創建一個包含174個參數的參數對象,然後讓我根據需要設置屬性。是否有可能以某種方式創建一個類似的對象,只包含我需要的單個參數?我已經試過用$ param.PSObject.TypeNames.Insert(0,$ paramClassName)這樣做,但是生成的param對象對$ wsproxy.getRecords($ param)調用是不可接受的。另外,我無法直接添加本地屬性,只能使用NoteProperties。恢復使用原始參數對象,是否有可能刪除173個參數?底層對象似乎是不可改變的,但也許有一些我從未見過的技巧?
示範代碼:
$cred = Get-Credential
$wsproxy = New-WebServiceProxy -uri 'https://snowtest/cmdb_ci_win_server.do?WSDL' -Credential $cred
if ($wsproxy) {
# Force $cred onto the new wsproxy, or it will default to non-authenticated calls
$wsproxy.Credentials = $cred
# The parameter object we'll send with the query must be built from the custom object.
# That requires we extract the class name from the method's parameter definition.
# All my attempts to create a custom query object failed, including forcing the classname.
$overloadDefinitions = $wsproxy.getRecords.OverloadDefinitions
$paramClassName = $overloadDefinitions[0] -ireplace '^[^(]+\(([^ ]+).+$', '$1'
$param = New-Object $paramClassName
$param.host_name = 'ServerName'
$wsproxy.getRecords($param)
}
我稍後可能會回答這個問題。我有一個哥們正在尋找可能的第三條路。我目前的解決方案是使用New-WebServiceProxy方法背後的代碼(如果我回答,我會鏈接它)並攔截預編譯的基於WSDL的源代碼。我修改查詢方法,除去我想要查詢的所有屬性,然後將該源編譯成程序集並正常使用。 – codepoke