简体   繁体   English

如何将 append 转换为 powershell 的 Hashtable 值?

[英]How to append to powershell Hashtable value?

I am interating through a list of Microsoft.SqlServer.Management.Smo.Server objects and adding them to a hashtable like so:我正在通过Microsoft.SqlServer.Management.Smo.Server对象列表进行交互,并将它们添加到哈希表中,如下所示:

$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 string to the end of the hashtable value.我现在要做的是 append 一个字符串到哈希表值的末尾。 So for an $instance I want to append a string to the hashtable value for that $instance.因此,对于一个 $instance,我想将一个字符串 append 作为该 $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; ""
$h."Test" += " is changed"
$h

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

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. 结果将是一个散列表,每个实例作为键,以及一个字符串数组,其中每个字符串是用户的T-SQL脚本。

The .Script() method returns a string collection. .Script()方法返回一个字符串集合。 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)"

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM