开发者

How do I manipulate data out of a variable in powershell

开发者 https://www.devze.com 2023-03-10 13:04 出处:网络
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

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.


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"


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消