0

I'm working with some SOAP-based Web services (Opsware Twister, to be more specific) via New-WebServiceProxy cmdlet (obviously) and encountered some difficulties when providing parameters for methods I call - it results in typecasting error. Here's the code:

$user = "myuser"
$passw = Get-Content "z:\creds.txt" | ConvertTo-SecureString

$hpsa_core = "core.dom.loc"

$creds = [PSCredential]::New($user, $passw)

$URI_ss = "https://$hpsa_core/osapi/com/opsware/server/ServerService?WSDL"
$ws_ss = New-WebServiceProxy -URI $URI_ss -Credential $creds -Namespace 'ns_ss'

$URI_search = "https://$hpsa_core/osapi/com/opsware/search/SearchService?WSDL"
$ws_search = New-WebServiceProxy -URI $URI_search -Credential $creds -Namespace 'ns_search'

$hostname = "SQLSERVER1.dom.loc"
$objectType = "device"
$expression = "ServerVO.hostname CONTAINS $hostname"

$filter = [ns_search.Filter] @{
    objectType = $objectType
    expression = $expression
}

$refs = $ws_ss.findServerRefs($filter)

The problem is, that findServerRefs method expects not a primitive type, but another class (com.opsware.search.Filter), and methods to create its instance are not exposed via the service. This straightforward approach doesn't work, resulting in error:

Cannot convert argument "filter", with value: "ns_search.Filter", for "findServerRefs" to type "ns_ss.Filter": "Cannot convert the "ns_search.Filter" value of type "ns_search.Filter" to type "ns_ss.Filter"." At line:36 char:1 + $refs = $ws_ss.findServerRefs($filter) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

SearchService is implemented as Java interface (com.opsware.search -Interface SearchService)

Am I using the wrong approach?

One important remark: there is an integration component for Powershell from the vendor (DLL) which provides an easeir experience, but I want to achieve the same results with pure Powershell functionality (if it is possible at all).

1 Answer 1

0

Replacing this fragment

$filter = [ns_search.Filter] @{
    objectType = $objectType
    expression = $expression
}

with this one

$filter = [ns_ss.Filter] @{
    objectType = $objectType
    expression = $expression
}

will probably do the job.

Update:

Found the cause of this problem: Bug in New-WebServiceProxy cmdlet when using -Namespace?

In short, you must save script as a file and invoke that way (Using & operator, or as a command line argument: powershell.exe -File script.ps1).

Sign up to request clarification or add additional context in comments.

1 Comment

no, it generates the same typecast error, only with "ns_ss" context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.