开发者

How to append to powershell Hashtable value?

开发者 https://www.devze.com 2023-01-29 10:00 出处:网络
I am interating through a list of Microsoft.SqlServer.Management.Smo.Server objects and adding them to a hashtable like so:

I am interating through a list of Microsoft.SqlServer.Management.Smo.Server objects and adding them to a hashtable like so:

$instances = Get-Content -Path .\Instances.txt
$scripts = @{}

foreach ($i in $instances)
{
    $instance = New-Object Microsoft.SqlServer.Management.Smo.Server $i
    foreach($login in $instance.Logins)
    {
        $scripts.Add($instance.Name, $login.Script())       
    }
}

So far so good. What I want to do now is append a str开发者_运维问答ing to the end of the hashtable value. So for an $instance I want to append a string to the hashtable value for that $instance. How would I do that? I have started with this, but I'm not sure if I'm on the right track:

foreach ($db in $instance.Databases)
{       
    foreach ($luser in $db.Users)
    {
        if(!$luser.IsSystemObject)
        {
            $scripts.Set_Item ($instance, <what do I add in here?>)
        }
    }
}

Cheers


$h= @{}

$h.add("Test", "Item")
$h

Name                           Value                                                                                                                                                   
----                           -----                                                                                                                                                   
Test                           Item                                                                                                                                                    

$h."Test" += " is changed"
$h

Name                           Value                                                                                                                                                   
----                           -----                                                                                                                                                   
Test                           Item is changed                                                                                                                                         


I would go with this code.

$instances = Get-Content -Path .\Instances.txt
$scripts = @{}

foreach ($i in $instances)
{
    $instance = New-Object Microsoft.SqlServer.Management.Smo.Server $i
    foreach($login in $instance.Logins)
    {
        $scripts[$instance.Name] = @($scripts[$instance.Name]) + $login.Script().ToString()
    }
}

.

foreach ($db in $instance.Databases)
{
    foreach ($luser in $db.Users)
    {
        if(!$luser.IsSystemObject)
        {
            $scripts[$instance] = @($scripts[$instance]) + $luser.Script().ToString()
        }
    }
}

The result will be a hash table with each instance as a key, and an array of strings where each string is the T-SQL script for a user.


The .Script() method returns a string collection. There's probably a more elegant way of doing it, but replacing

$scripts.Set_Item ($instance, <what do I add in here?>)

with

$val = $scripts[$instance]
$val.Add("text to add")
$scripts.Set_Item($instance, $val)

should work.


$test = @{}

$test.Hello = "Hello World"

Write-Host "message from $($test.Hello)"

$test.Hello += " Cosmonaut"

Write-Host "message from $($test.Hello)"

How to append to powershell Hashtable value?

0

精彩评论

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