Hello i would like to prepare a Currency converter Function which in the first step display rates in an specific days:
Function Get-Rates {
>>
>> $ConversionRate=New-Object System.Data.DataSet
>>
>> $ConversionRate.Tables.Add("tstable")
>> $Currency=(New-WebServiceProxy -Uri http://currencyconverter.kowabunga.net/converter.asmx?WSDL)
>> $ConversionRate.Tables["tstable"].Columns.Add($Currency.GetCurrencyRates("3/5/2021"))
>>
>> $( $ConversionRate.Tables["tstable"]) | foreach {
>> write-host "Name value is : "$_.name
>> write-host "Nath value is : "$_.path
>> }
>> }
I have located an section in XML which contain type of it (so it is date time format):
<s:element name="GetCurrencyRates">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="RateDate" type="s:dateTime"/>
</s:sequence>
</s:complexType>
</s:element>
I have tryed with get-date -format “HH/dd/yyyy”
The point is to able to loop throught the returned Datatable object. But as though it not return anything
Looking through the documentation for this API, I think you might be over complicating things. Here’s a basic example I cobbled together which I think achieves what you’re trying to do.
function Get-Rates {
param (
[Parameter()]
$date = (Get-Date -Format 'MM/dd/yyyy')
)
$uri = "http://currencyconverter.kowabunga.net/converter.asmx/GetCurrencyRates?RateDate=$date"
$response = Invoke-RestMethod -Uri $uri -Method Get
$response.DataSet.diffgram.NewDataSet.table | Select-Object Description,Rate
}
Get-Rates -date '03/01/2021'
Can you help me how you know that:
$response.DataSet.diffgram.NewDataSet.table
.diffgram is?
.NewDataSet?
.table?
I mean, what does it?
So we have an object by invoke-restmethod output. How do i know i need the refer to .DataSet.diffgram.NewDataSet.table ?
Sory i am an beginner
It’s really just a case of drilling down until you get what you want
When we type $response
at the prompt, we see the two properties xml
and DataSet
, so then we try $response.DataSet
. That gives the next three properties and so on.
In time i edited in my shell the function, it not offered property as normal. So i greb that 2 line, and beginning to write and after $response it offered it yet.
So i would find a better option to discover the object what i am working with. So i found the Get-Member cmdlet which get the pipeline input of that and list all available methid, alias, propertys and so on. So the only missing things is go deaper in the object without get directly it from pipeline
So i guess:
$response| Get-Member -MemberTypes Property
output
Name MemberType Definition
DataSet Property System.Xml.XmlElement DataSet {get;}
xml Property string xml {get;set;}
Than i cannot discover or peak into to DataSet property without calling it from original object
$response.DataSet| Get-Member -MemberTypes Property
Name MemberType Definition
diffgram Property System.Xml.XmlElement diffgram {get;}
schema Property System.Xml.XmlElement schema {get;}
xmlns Property string xmlns {get;set;}
Do you have any idea for that ?
Discovering the child nodes is always going to involve inspecting the returned XML in some way or another. I think using the dot notation, is probably the quickest way. Although you could use $response.InnerXML
to look at the document structure.
Perhaps someone with more experience working with XML can advise on what techniques they would use.