Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get Key names and Values?

47 views
Skip to first unread message

kar...@markmanns.dk

unread,
Jan 4, 2013, 5:32:31 PM1/4/13
to
Hi
I'am a bit stuck on this one, so any help would be appriciated:
I have the following code snippet:

c:\get-host
Which returns:
Name : Windows PowerShell ISE Host
Version : 2.0
InstanceId : e5ab551b-4fad-4ae6-89c3-e91e0633d4ff
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : da-DK
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace

I want to create a foreach or a for loop, iterating every key and value, and (for a beginng) display the key names and their values on the screen.
The code should present something like the above list, but I need to get the keys names and values out seperatly for later manipulation.

I have created the following which obvioiusly doesn't work, because I don't know how to syntax it. But I hope You get the idea:

$b = get-host
foreach($KeyValuePair in $b)
{
write-host "Key is: " + $KeyValuePair.key
write-host "Value is: " + $KeyValuePair.value
}

Kind regards
Karsten Markmann

Jürgen Exner

unread,
Jan 4, 2013, 5:40:42 PM1/4/13
to
On Fri, 4 Jan 2013 14:32:31 -0800 (PST), kar...@markmanns.dk wrote in
microsoft.public.windows.powershell:

>Hi
>I'am a bit stuck on this one, so any help would be appriciated:
>I have the following code snippet:
>
>c:\get-host
>Which returns:
>Name : Windows PowerShell ISE Host
>Version : 2.0
>InstanceId : e5ab551b-4fad-4ae6-89c3-e91e0633d4ff
>UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
>CurrentCulture : da-DK
>CurrentUICulture : en-US
>PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions
>IsRunspacePushed : False
>Runspace : System.Management.Automation.Runspaces.LocalRunspace
>
>I want to create a foreach or a for loop, iterating every key and value, and (for a beginng) display the key names and their values on the screen.

You are mistaken about the data structure. There is no hash and there
are no key-value pairs anywhwere in the result of get-host. Try
"get-member" to see the actual nature of the result type:

PS C:\> get-host | get-member
TypeName: System.Management.Automation.Internal.Host.InternalHost

Name MemberType Definition
---- ---------- ----------
EnterNestedPrompt Method System.Void EnterNestedPrompt()
Equals Method bool Equals(System.Object obj)
ExitNestedPrompt Method System.Void ExitNestedPrompt()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
NotifyBeginApplication Method System.Void NotifyBeginApplication()
NotifyEndApplication Method System.Void NotifyEndApplication()
PopRunspace Method System.Void PopRunspace()
PushRunspace Method System.Void PushRunspace(runspace
runspace)
SetShouldExit Method System.Void SetShouldExit(int
exitCode)
ToString Method string ToString()
CurrentCulture Property System.Globalization.CultureInfo
CurrentCulture {get;}
CurrentUICulture Property System.Globalization.CultureInfo
CurrentUICulture {get;}
InstanceId Property System.Guid InstanceId {get;}
IsRunspacePushed Property System.Boolean IsRunspacePushed {get;}
Name Property System.String Name {get;}
PrivateData Property System.Management.Automation.PSObject
PrivateData {get;}
Runspace Property
System.Management.Automation.Runspaces.Runspace Runspace {get;}
UI Property
System.Management.Automation.Host.PSHostUserInterface UI {get;}
Version Property System.Version Version {get;}

>I have created the following which obvioiusly doesn't work, because I don't know how to syntax it. But I hope You get the idea:

Has nothing to do with syntax but with assuming the wrong data
structure.

jue

kar...@markmanns.dk

unread,
Jan 5, 2013, 5:48:29 PM1/5/13
to
I did the Get-Member and saw the structure, but still that didnt really help me on how to get the names on every property an their corresponding values.
The case is that I need to make a generic function that can display the above, on any cmdlet with the same structure.
Lets assume I have to retrieve data from a cmdlet called Get-X, which returns:
a : 1
b : 30
c : any

The next cmdlet could be Get-Y, which returns:
thing : abc
ding : 12345

Therefore I need my function to be able to retireve the names and values of these properties dynamically.
Hope the above clarifies my problem, and that anyone maybe have an idea for this.
Kind regards Karsten Markmann

kar...@markmanns.dk

unread,
Jan 7, 2013, 5:14:14 AM1/7/13
to
get-host | Get-Member -MemberType Properties | Foreach {
"key is " + $_.Name
"Value is " + $Host.($_.Name)
}

I got this solution from another forum.
Credits to Blindrood
0 new messages