我创建了一个简短的PowerShell脚本,用于从Web服务中提取一些XML。这似乎有效。我运行了一个tcpdump,可以看到返回的正确XML。如果我运行get-member,我会看到所有正确的参数。但是,当我尝试输出任何这些参数时,我什么也看不见。没有错误,它似乎只输出任何东西。
Function Get-AssetByMacAddress {
Param(
[Parameter(Mandatory=$true)]
[string]$mac
)
$username = "abc"
$password = ConvertTo-SecureString 'xyz' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
$uri = "http://mywebservice.com/assetservice.asmx?WSDL"
# print password (testing)
$cred.GetNetworkCredential().password
# Create web service
$phatWebService = New-WebServiceProxy -Uri $uri -Credential $cred -Namespace "PhatWebService"
# Call method
$phatWebService.GetAssetByMacAddress($mac)
}
$xml = Get-AssetByMacAddress -mac "90:b1:1c:61:8e:41"
$xml | get-member
Write-Host "**************************"
Write-Host $xml.CurrencyCodeBook
输出如下所示:
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB)
Contains Method bool Contains(string value)
CopyTo Method System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool EndsWith(string value, bool ignoreCase, System.Globalization.CultureInfo culture)
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison comparisonType)
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex, int count), int IndexOf(string value), int IndexOf(string value, int startIndex), int IndexOf(string value, int...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf, int startIndex, int count)
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int startIndex, int count), int LastIndexOf(string value), int LastIndexOf(string value, int startIndex), int LastI...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastIndexOfAny(char[] anyOf, int startIndex, int count)
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count, System.StringSplit...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool StartsWith(string value, bool ignoreCase, System.Globalization.CultureInfo culture)
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower Method string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToString Method string ToString(), string ToString(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property System.Int32 Length {get;}
**************************
TypeName: PhatWebService.AssetInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AssetLife Property System.Decimal AssetLife {get;set;}
BookValue Property System.Decimal BookValue {get;set;}
ChildAssets Property PhatWebService.AssetInfo[] ChildAssets {get;set;}
ChildrenCount Property System.Int32 ChildrenCount {get;set;}
CurrencyCodeBook Property System.String CurrencyCodeBook {get;set;}
CurrencyCodePurchase Property System.String CurrencyCodePurchase {get;set;}
CurrencyCodeWritten Property System.String CurrencyCodeWritten {get;set;}
DateCreated Property System.DateTime DateCreated {get;set;}
DepreciationFactor Property System.Decimal DepreciationFactor {get;set;}
ExternalID Property System.String ExternalID {get;set;}
ExternalID2 Property System.String ExternalID2 {get;set;}
ExternallyOwned Property System.Int16 ExternallyOwned {get;set;}
Fullname Property System.String Fullname {get;set;}
Hostname Property System.String Hostname {get;set;}
HttpPort Property System.Int32 HttpPort {get;set;}
HttpsPort Property System.Int32 HttpsPort {get;set;}
ID Property System.Int32 ID {get;set;}
InstallationScript Property System.String InstallationScript {get;set;}
InstallationScriptID Property System.Int32 InstallationScriptID {get;set;}
IP Property System.String IP {get;set;}
IsExpensed Property System.Boolean IsExpensed {get;set;}
IsMonitored Property System.Boolean IsMonitored {get;set;}
IsObsolete Property System.Boolean IsObsolete {get;set;}
Label Property System.String Label {get;set;}
LastDepreciation Property System.DateTime LastDepreciation {get;set;}
LastHeard Property System.String LastHeard {get;set;}
Line Property PhatWebService.LineInfo Line {get;set;}
LineID Property System.Int32 LineID {get;set;}
Location Property System.String Location {get;set;}
MacAddress Property System.String MacAddress {get;set;}
MaintPort Property System.Int32 MaintPort {get;set;}
Manufacturer Property PhatWebService.ManufacturerInfo Manufacturer {get;set;}
ParentID Property System.Int32 ParentID {get;set;}
Password Property System.String Password {get;set;}
PrinterAssetID Property System.Int32 PrinterAssetID {get;set;}
PurchaseValue Property System.Decimal PurchaseValue {get;set;}
RefreshRate Property System.String RefreshRate {get;set;}
ResolutionX Property System.String ResolutionX {get;set;}
ResolutionY Property System.String ResolutionY {get;set;}
RoleID Property System.Int32 RoleID {get;set;}
SalvageValue Property System.Decimal SalvageValue {get;set;}
SerialNo Property System.String SerialNo {get;set;}
SiteID Property System.Int32 SiteID {get;set;}
SoftwareVersion Property System.String SoftwareVersion {get;set;}
SSHPort Property System.Int32 SSHPort {get;set;}
Status Property System.String Status {get;set;}
Tag Property System.String Tag {get;set;}
TelnetPort Property System.Int32 TelnetPort {get;set;}
UserIDCreated Property System.Int32 UserIDCreated {get;set;}
Username Property System.String Username {get;set;}
VNCPort Property System.Int32 VNCPort {get;set;}
WarrantyEnd Property System.DateTime WarrantyEnd {get;set;}
WarrantyStart Property System.DateTime WarrantyStart {get;set;}
WrittenDownValue Property System.Decimal WrittenDownValue {get;set;}
出于某种原因,当我尝试查看属性" CurrencyCodeBook"时,它再次向我展示了所有成员属性。
一些Google搜索建议我需要将Web服务方法定义为[xml]
E.g。
[xml]$phatWebService.GetAssetByMacAddress($mac)
...但这只是给我一个错误"根级别的数据无效"
答案 0 :(得分:0)
尝试这样:
$wsdl = "http://mywebservice.com/assetservice.asmx?WSDL"
$proxy = New-WebServiceProxy -Uri $wsdl -Namespace WebServiceProx
$sample = $proxy.GetAssetByMacAddress($mac)
[xml]$xmlfile = $sample
现在获取值
$fullName = $xmlfile.AssetInfo.FullName
$hostName = $xmlfile.AssetInfo.HostName
$httpPort = $xmlfile.AssetInfo.HttpPort
OR
$fullName = $xmlfile.FullName
$hostName = $xmlfile.HostName
$httpPort = $xmlfile.HttpPort
编辑:
尝试检查您的xml文件。你能在这里发布xml文件吗?