.net 如何从 PowerShell 访问 Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2022678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access a web service from PowerShell?
提问by tangens
I'd like to access a web service with a given (simple) WSDL from within Windows PowerShell.
我想从 Windows PowerShell 中访问具有给定(简单)WSDL 的 Web 服务。
Is there an easy way to do this?
是否有捷径可寻?
回答by doer
# Working example of how to use PowerShell (version >= 2) to access a web service.
$svc = New-WebServiceProxy –Uri ‘http://www.webservicex.net/stockquote.asmx?WSDL'
$svc | Get-Member # Use Get-Member to discover the interface of a web service.
# Get stock quotes.
$svc.GetQuote(‘BA') # Boeing
$svc.GetQuote(‘AMZN') # Amazon
$svc.GetQuote(‘SBUX') # Starbucks
回答by driis
One way would be to use WSDL.exe to generate wrapper classes- compile the generated source and use the strongly typed classes from PowerShell. The whole generate - compile - instantiate process can be easily done automatically from PowerShell.
一种方法是使用WSDL.exe 生成包装类- 编译生成的源代码并使用来自 PowerShell 的强类型类。整个生成 - 编译 - 实例化过程可以从 PowerShell 轻松自动完成。
If you are using PowerShell 2.0, use New-WebServiceProxyas suggested in the other answer.
如果您使用的是 PowerShell 2.0,请按照其他答案中的建议使用New-WebServiceProxy。

