Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, October 18, 2019 11:01 AM
Hi All.
I am trying to read key valued pairs from a vault system and was wondering what is the most efficient way of doing this and also converting into a standard function. Once given a URL, I would like the function/routine to be able to read all the keys and values and then this can be converted into a string to be used within variables.
This is what I have so far.
$pass = @{
password='xxxxxxxxxxxx'
}
$body = (ConvertTo-Json $pass)
$secret_url_login = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/loginname"
$result1 = Invoke-RestMethod -secret_url $secret_url_login -Method Post -Body $body
$authct = $result1.auth.client_token
$secret_url_metadata = "http://xxxxxxxxxxxxxxxxxxx/status/project1"
$header = @{'X-Vault-Token'=$authct}
$raw_result = Invoke-RestMethod -secret_url $secret_url_metadata -Method Get -Headers $header
$result_data = $raw_result.data
From the vault this is what the Json looks like
{
"host": "server123",
"department": "department",
"rack": "false",
"owner": "business",
"environment": "Development",
"location": "USA West"
}
From Powershell
$result_data looks like the below.
@{host=server123; department=department; rack=false; owner=business; environment=Development; location=USA West}
All replies (24)
Friday, October 18, 2019 1:42 PM
Hi MrFlinstone,
I didn't get what exactly you want to do but if you want to use $result_Data variable as JSON body for your Invoke-RestMethod command then you can use ConvertTo-Json command to convert $result_Data into JSON.
Friday, October 18, 2019 1:43 PM
I want to read the values of the following keys
host
department
rack
owners
environment
location
Friday, October 18, 2019 1:53 PM
To list only values :
$result_Data.Keys|foreach{$result_Data[$_]}
To list keys and values
$resultData.Keys|foreach{ Write-Output "$($_ +":"+ $resultData[$_])"}
Friday, October 18, 2019 2:05 PM
Another way to loop through a hashtable:
foreach ($i in $result_data.GetEnumerator()) { write-host $i.name $i.value }
Friday, October 18, 2019 3:52 PM
Try this:
$x = @{host='server123'; department='department'; rack='false'; owner='business'; environment='Development'; location='USA West'}
$x.GetEnumerator()|
foreach {
"{0,-14} `t {1}" -f $_.Key, $_.Value
}
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Monday, October 21, 2019 11:53 PM
Another way to loop through a hashtable:
foreach ($i in $result_data.GetEnumerator()) { write-host $i.name $i.value }
Thanks for the reply.
I had the error below.
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'GetEnumerator'.
At line:1 char:16
+ foreach ($i in $result_data.GetEnumerator()) { write-host $i.name $i.valu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (GetEnumerator:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Monday, October 21, 2019 11:55 PM
Try this:
$x = @{host='server123'; department='department'; rack='false'; owner='business'; environment='Development'; location='USA West'} $x.GetEnumerator()| foreach { "{0,-14} `t {1}" -f $_.Key, $_.Value }
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Hi,
When I tried this, nothing came out of it.
Monday, October 21, 2019 11:58 PM
To list only values :
$result_Data.Keys|foreach{$result_Data[$_]}
To list keys and values
$resultData.Keys|foreach{ Write-Output "$($_ +":"+ $resultData[$_])"}
Hi,
I tried the above.
I got the error below.
$result_data.Keys|foreach{$result_data[$_]}
Index operation failed; the array index evaluated to null.
At line:1 char:23
+ $result_data.Keys|foreach{$result_data[$_]}
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
The data type is of type custom object.
Tuesday, October 22, 2019 12:31 AM
Json is NOT a hash - it is text.
$object = $raw_result.data | ConvertFrom-Json
If resultdat ais a hash then this would work:
$raw_result.data.Keys
which would list the keys.
\(ツ)_/
Tuesday, October 22, 2019 1:44 AM
Really? Here's what I see:
PS C:\Users\richm> $x = @{host='server123'; department='department'; rack='false'; owner='business'; environment='Development'; location='USA West'}
PS C:\Users\richm>
PS C:\Users\richm> $x.GetEnumerator()|
>> foreach {
>> "{0,-14} `t {1}" -f $_.Key, $_.Value
>>
>> }
department department
host server123
rack false
environment Development
owner business
location USA West
PS C:\Users\richm>
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Tuesday, October 22, 2019 11:36 AM
Json is NOT a hash - it is text.
$object = $raw_result.data | ConvertFrom-Json
If resultdat ais a hash then this would work:
$raw_result.data.Keys
which would list the keys.
\(ツ)_/
Its not a hash, the data type is string. When I tried to convert from jSon. I get the error below.
ConvertFrom-Json : Invalid JSON primitive: .
At line:1 char:29
+ $object = $result_data.data | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
Tuesday, October 22, 2019 11:40 AM
Json is NOT a hash - it is text.
$object = $raw_result.data | ConvertFrom-Json
If resultdat ais a hash then this would work:
$raw_result.data.Keys
which would list the keys.
\(ツ)_/
Its not a hash, the data type is string. When I tried to convert from jSon. I get the error below.
ConvertFrom-Json : Invalid JSON primitive: . At line:1 char:29 + $object = $result_data.data | ConvertFrom-Json + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
The data type returned is a string.
I run the get-Member command and got the below.
TypeName: System.String
Name MemberType Definition
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object obj), int IComparable[string].CompareTo(string other)
Contains Method bool Contains(string value)
CopyTo Method 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, cultureinfo culture)
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison comparisonType), bool IEquatable[string].Equals(string other)
GetEnumerator Method System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator(), System.Collections.Generic.IEnumerator[char] IEnumerable[char].GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.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 startIndex...
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 LastIndexOf(stri...
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.StringSplitOptions opt...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool StartsWith(string value, bool ignoreCase, cultureinfo culture)
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar Method char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower Method string ToLower(), string ToLower(cultureinfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider)
ToString Method string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormatProvider provider)
ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(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;}
Tuesday, October 22, 2019 12:13 PM
Copy and post the string.
\(ツ)_/
Tuesday, October 22, 2019 2:32 PM
Copy and post the string.
\(ツ)_/
This is what it looks like, I am interested in what is in the data section, but the others could also be needed later.
data
@{buildstatus=built; department=department; rack=false; owner=business; environment=Development; location=USA West; os=Windows Server 2012}
metadata
@{created_time=2019-10-15T14:27:09.609277032Z; deletion_time=; decomission=False; version=2}
Tuesday, October 22, 2019 2:44 PM
What you have posted is not Json and it is not a hash it is just a set of strings. Someone screwed up the web side. Contact the site owner for help.
\(ツ)_/
Tuesday, October 22, 2019 2:46 PM
The following will convert the string into a hash.
$hash = $data -replace '@|\{|\}' | ConvertFrom-StringData
$hash.Keys
\(ツ)_/
Tuesday, October 22, 2019 3:26 PM
The following will convert the string into a hash.
$hash = $data -replace '@|\{|\}' | ConvertFrom-StringData $hash.Keys
\(ツ)_/
This just outputs data.
Tuesday, October 22, 2019 3:27 PM
You have to replace $data with your variable.
\(ツ)_/
Tuesday, October 22, 2019 9:37 PM
You have to replace $data with your variable.
\(ツ)_/
Please confirm if you mean the below, see the modification below.
$pass = @{
password='xxxxxxxxxxxx'
}
$body = (ConvertTo-Json $pass)
$secret_url_login = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/loginname"
$result1 = Invoke-RestMethod -secret_url $secret_url_login -Method Post -Body $body
$authct = $result1.auth.client_token
$secret_url_metadata = "http://xxxxxxxxxxxxxxxxxxx/status/project1"
$header = @{'X-Vault-Token'=$authct}
$raw_result = Invoke-RestMethod -secret_url $secret_url_metadata -Method Get -Headers $header
$result_data = $raw_result.data$hash = $result_data.data -replace '@|\{|\}' | ConvertFrom-StringData$hash.Keys
Tuesday, October 22, 2019 10:29 PM
You have to replace $data with your variable.
\(ツ)_/
So I am finding that trying to convert the value into hash gives the following results.
Name Value
build_status built; department=department; rack=false; owner=business; environment=Development; location=USA West; os=Windows Server 2012}
The powershell output of $raw_result.data is similar to what I expect, but I just cannot extract the values individually. On the Ps console, $raw_result.data gives.
buildstatus: built
department: department
rack: false
owner: business
environment: Development
location: USA West
os: Windows Server 2012
Wednesday, October 23, 2019 12:07 AM
Without your learning enough basic PowerShell this could go on for days. The answers above sow you how to deal with a hash.
Without some basic understanding of these things it is not possible to understand what your issues are or what you have. If we can access the web site this would be simple. Your answers and questions seem to only confuse you and us more.
Sorry.
\(ツ)_/
Wednesday, October 23, 2019 12:42 PM
Thanks for the help, I have read up about it but its just not coming out the way I expect it to. On the website, this is the raw Json data.
{
"host": "server123",
"department": "department",
"rack": "false",
"owner": "business",
"environment": "Development",
"location": "USA West"
}
Wednesday, October 23, 2019 3:03 PM
$raw = @'
{
"host": "server123",
"department": "department",
"rack": "false",
"owner": "business",
"environment": "Development",
"location": "USA West"
}
'@
$raw | ConvertFrom-Json
\(ツ)_/
Friday, November 8, 2019 1:41 PM
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected].