0

I have a nice little powershell script that works

$URL = $args[0]
$proxy = New-WebServiceProxy -Uri $URL -Namespace webservice -UseDefaultCredential
$result = $proxy.TestWebMethod()

usage from cmd: 
powershell.exe myscript.ps1 "http://somesite.com/someservice.asmx"

What I want to do is also be able to pass in the method name dynamically, something to the effect of:

$URL = $args[0]
$proxy = New-WebServiceProxy -Uri $URL -Namespace webservice -UseDefaultCredential
$result = $proxy.$args[1]

usage from cmd: 
powershell.exe myscript.ps1 "http://somesite.com/someservice.asmx" "TestWebMethod"

Is there some way to make it work dynamically the second way?

4

2 回答 2

1

我没有要测试的服务,但是您尝试过invoke-expression(iex)吗?

$result = iex "`$proxy.$($args[1])()"
于 2013-11-07T20:19:21.230 回答
1

这看起来有点奇怪,但在 PowerShell V3 中你可以这样做:

$proxy | Foreach $args[1]

如果方法不带参数,你也可以这样做:

$proxy."$args[1]"

如果您对该方法有参数:

$proxy."$args[1]".Invoke(<args here>)

这是 V2 上的一个示例,它使用 Web 服务并采用 arg:

$URI = "http://www.webservicex.net/uszip.asmx?WSDL"
$zip = New-WebServiceProxy -uri $URI -namespace WebServiceProxy -class ZipClass
$method = "GetInfoByZIP"
$zip."$method".Invoke('80525')
于 2013-11-07T20:22:21.860 回答