Nathan Kurtz
Nathan Kurtz

Reputation: 13

How do I manipulate data out of a variable in powershell

I have a power-shell script with which I am trying to back up a constantly changing number of SQL databases. Fortunately all of these databases are listed in a registry key. I am leveraging this in a for-each loop. The issue that I am having is that after grabbing the registry value that I want, when I try to pass it into my function to back up the databases there seems to be information in the variable that I can get rid of. If I output the contents of the variable to the screen by just calling the variable ($variable) is shows just fine. But if I write-host the variable to the screen the extra "content" that shows up when calling the function also shows up.

Here is the part of the script that generates the contents of the variable.

foreach ($childitem in get-childitem "HKLM:\SOFTWARE\Wow6432Node\Lanovation\Prism Deploy\Server Channels")
{$DBName = get-itemproperty Registry::$childitem | select "Channel Database Name"
write-host $DBname}

Here is what write-host displays :

@{Channel Database Name=Prism_Deploy_Sample_268CBD61_AC9E_4853_83DE_E161C72458DE}

but what I need is only this part :

Prism_Deploy_Sample_268CBD61_AC9E_4853_83DE_E161C72458DE

I have tried looking online at how to do this, and what I've found mentions things similar to $variable.split and then specifying my delimiters. But when I try this I get an error saying "Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'split'."

I'm at a loss as to where to go from where I'm at currently.

Upvotes: 1

Views: 2004

Answers (3)

zdan
zdan

Reputation: 29450

select-object will return an object that has the named properties that you "select". To get just value of that property, just access it by name:

write-host $DBname."Channel Database Name"

Upvotes: 1

ProfessionalAmateur
ProfessionalAmateur

Reputation: 4563

You should be able to write

foreach ($childitem in get-childitem "HKLM:\SOFTWARE\Wow6432Node\Lanovation\Prism Deploy\Server Channels")
{$DBName = get-itemproperty Registry::$childitem | select "Channel Database Name"
write-host $DBname.Name}

to get what you are looking for

Upvotes: 0

JNK
JNK

Reputation: 65157

Sounds like it's returning a hash table row object.

Try

write-host $DBName.value

or, failing that, do a

$DBName | Get-member

When in doubt, get-member gives you a nice idea of what you are dealing with.

Upvotes: 0

Related Questions