0

我可以运行“hello world”脚本,但尝试运行这个示例脚本:

C:\Users\thufir\Desktop>
C:\Users\thufir\Desktop>powershell -ExecutionPolicy RemoteSigned
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> type .\stock.ps1
# 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
PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> .\stock.ps1


   TypeName: Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1vicex_net_stockquote_a
smx_WSDL.StockQuote

Name                                 MemberType Definition
----                                 ---------- ----------
Disposed                             Event      System.EventHandler Disposed(System.Object, System.EventArgs)
GetQuoteCompleted                    Event      Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes....
Abort                                Method     void Abort()
BeginGetQuote                        Method     System.IAsyncResult BeginGetQuote(string symbol, System.AsyncCallbac...
CancelAsync                          Method     void CancelAsync(System.Object userState)
CreateObjRef                         Method     System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Discover                             Method     void Discover()
Dispose                              Method     void Dispose(), void IDisposable.Dispose()
EndGetQuote                          Method     string EndGetQuote(System.IAsyncResult asyncResult)
Equals                               Method     bool Equals(System.Object obj)
GetHashCode                          Method     int GetHashCode()
GetLifetimeService                   Method     System.Object GetLifetimeService()
GetQuote                             Method     string GetQuote(string symbol)
GetQuoteAsync                        Method     void GetQuoteAsync(string symbol), void GetQuoteAsync(string symbol,...
GetType                              Method     type GetType()
InitializeLifetimeService            Method     System.Object InitializeLifetimeService()
ToString                             Method     string ToString()
AllowAutoRedirect                    Property   bool AllowAutoRedirect {get;set;}
ClientCertificates                   Property   System.Security.Cryptography.X509Certificates.X509CertificateCollect...
ConnectionGroupName                  Property   string ConnectionGroupName {get;set;}
Container                            Property   System.ComponentModel.IContainer Container {get;}
CookieContainer                      Property   System.Net.CookieContainer CookieContainer {get;set;}
Credentials                          Property   System.Net.ICredentials Credentials {get;set;}
EnableDecompression                  Property   bool EnableDecompression {get;set;}
PreAuthenticate                      Property   bool PreAuthenticate {get;set;}
Proxy                                Property   System.Net.IWebProxy Proxy {get;set;}
RequestEncoding                      Property   System.Text.Encoding RequestEncoding {get;set;}
Site                                 Property   System.ComponentModel.ISite Site {get;set;}
SoapVersion                          Property   System.Web.Services.Protocols.SoapProtocolVersion SoapVersion {get;s...
Timeout                              Property   int Timeout {get;set;}
UnsafeAuthenticatedConnectionSharing Property   bool UnsafeAuthenticatedConnectionSharing {get;set;}
Url                                  Property   string Url {get;set;}
UseDefaultCredentials                Property   bool UseDefaultCredentials {get;set;}
UserAgent                            Property   string UserAgent {get;set;}
exception
exception
exception


PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop>

这是政策问题吗?或者,脚本本身有问题?

还:

PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> .\hello.ps1
hi
Cannot convert value "Data Not Found" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted
as the valid child of this node, because the specified node is the wrong type."
At C:\Users\thufir\Desktop\hello.ps1:8 char:1
+ [xml]$response = $proxy.GetWeather("Tampa", "United States")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastToXmlDocument

PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> type .\hello.ps1



echo "hi"


$proxy = New-WebServiceProxy -Uri http://www.webservicex.net/globalweather.asmx?WSDL
[xml]$response = $proxy.GetWeather("Tampa", "United States")
$response.CurrentWeather | Format-List

PS C:\Users\thufir\Desktop>
4

3 回答 3

1

尝试运行它并且也有一个异常:

PS:14:27:35>$quote = Invoke-RestMethod -Uri " http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=GOOGL " -Method Get

PS:14:27:49>$quote.string

xmlns #text
----- -----
http://www.webserviceX.NET/异常

PS:14:27:53>$quote.string.'#text'

于 2018-02-15T19:29:07.877 回答
1

在我看来,您正在调用的 Web 服务存在问题。它返回字符串“异常”。您的代码运行正常。

导航到站点Stock Quote Detail并运行测试,我也收到异常返回。

于 2018-02-15T19:23:53.500 回答
0

快速而肮脏的输出:

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\thufir>cd Desktop

C:\Users\thufir\Desktop>powershell -ExecutionPolicy RemoteSigned
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> .\foobar.ps1


   TypeName: WebServiceProxy.GlobalWeather

Name                                 MemberType Definition
----                                 ---------- ----------
Disposed                             Event      System.EventHandler Disposed(System.Object, System.EventArgs)
GetCitiesByCountryCompleted          Event      WebServiceProxy.GetCitiesByCountryCompletedEventHandler GetCitiesByC...
GetWeatherCompleted                  Event      WebServiceProxy.GetWeatherCompletedEventHandler GetWeatherCompleted(...
Abort                                Method     void Abort()
BeginGetCitiesByCountry              Method     System.IAsyncResult BeginGetCitiesByCountry(string CountryName, Syst...
BeginGetWeather                      Method     System.IAsyncResult BeginGetWeather(string CityName, string CountryN...
CancelAsync                          Method     void CancelAsync(System.Object userState)
CreateObjRef                         Method     System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Discover                             Method     void Discover()
Dispose                              Method     void Dispose(), void IDisposable.Dispose()
EndGetCitiesByCountry                Method     string EndGetCitiesByCountry(System.IAsyncResult asyncResult)
EndGetWeather                        Method     string EndGetWeather(System.IAsyncResult asyncResult)
Equals                               Method     bool Equals(System.Object obj)
GetCitiesByCountry                   Method     string GetCitiesByCountry(string CountryName)
GetCitiesByCountryAsync              Method     void GetCitiesByCountryAsync(string CountryName), void GetCitiesByCo...
GetHashCode                          Method     int GetHashCode()
GetLifetimeService                   Method     System.Object GetLifetimeService()
GetType                              Method     type GetType()
GetWeather                           Method     string GetWeather(string CityName, string CountryName)
GetWeatherAsync                      Method     void GetWeatherAsync(string CityName, string CountryName), void GetW...
InitializeLifetimeService            Method     System.Object InitializeLifetimeService()
ToString                             Method     string ToString()
AllowAutoRedirect                    Property   bool AllowAutoRedirect {get;set;}
ClientCertificates                   Property   System.Security.Cryptography.X509Certificates.X509CertificateCollect...
ConnectionGroupName                  Property   string ConnectionGroupName {get;set;}
Container                            Property   System.ComponentModel.IContainer Container {get;}
CookieContainer                      Property   System.Net.CookieContainer CookieContainer {get;set;}
Credentials                          Property   System.Net.ICredentials Credentials {get;set;}
EnableDecompression                  Property   bool EnableDecompression {get;set;}
PreAuthenticate                      Property   bool PreAuthenticate {get;set;}
Proxy                                Property   System.Net.IWebProxy Proxy {get;set;}
RequestEncoding                      Property   System.Text.Encoding RequestEncoding {get;set;}
Site                                 Property   System.ComponentModel.ISite Site {get;set;}
SoapVersion                          Property   System.Web.Services.Protocols.SoapProtocolVersion SoapVersion {get;s...
Timeout                              Property   int Timeout {get;set;}
UnsafeAuthenticatedConnectionSharing Property   bool UnsafeAuthenticatedConnectionSharing {get;set;}
Url                                  Property   string Url {get;set;}
UseDefaultCredentials                Property   bool UseDefaultCredentials {get;set;}
UserAgent                            Property   string UserAgent {get;set;}
<NewDataSet>
  <Table>
    <Country>Australia</Country>
    <City>Archerfield Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Amberley Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Alice Springs Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Brisbane Airport M. O</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Coolangatta Airport Aws</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Cairns Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Charleville Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Gladstone</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Longreach Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Mount Isa Amo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Mackay Mo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Oakey Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Proserpine Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Rockhampton Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Broome Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Townsville Amo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Weipa City</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Gove Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Tennant Creek Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Yulara Aws</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Albury Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Devonport East</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Goldstream Aws</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>East Sale Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Hobart Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Launceston Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Laverton Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Moorabbin Airport Aws</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Mount Gambier Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Mildura Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Melbourne Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Macquarie Island</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Wynyard West</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Adelaide Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Albany Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Broken Hill Patton Street</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Ceduna Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Derby</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Darwin Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Bullsbrook Pearce Amo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Edinburgh M. O.</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Forrest Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Geraldton Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Kalgoorlie Boulder Amo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Kununurra Kununurra Aws</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Leigh Creek Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Learmonth Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Meekatharra Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Port Hedland Pardoo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Parafield Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Belmont Perth Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Katherine Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Woomera Aerodrome</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Bankstown Airport Aws</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Canberra</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Coffs Harbour Mo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Cooma</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Camden Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Dubbo</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Norfolk Island Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Nowra Ran Air Station</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Richmond Aus-Afb</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Sydney Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Tamworth Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Wagga Airport</City>
  </Table>
  <Table>
    <Country>Australia</Country>
    <City>Williamtown Aerodrome</City>
  </Table>
</NewDataSet>
done


PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> type .\foobar.ps1

$url = "http://www.webservicex.net/globalweather.asmx?wsdl"


$webservicex = New-WebServiceProxy -Uri $url -namespace WebServiceProxy -Class GlobalWeatherSoap


$webservicex | gm


echo $australiancities


$australiancities.NewDataSet


$australiancities.NewDataSet.Table



 $AustralianCities = $webservicex.GetCitiesByCountry("Australia")

echo $AustralianCities

echo "done"





PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop>

只是在黑暗中摸索。但是,有趣:)

于 2018-02-15T19:49:49.537 回答