Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何附加到powershell哈希表值?_Sql Server_Powershell_Hashtable_Smo - Fatal编程技术网

Sql server 如何附加到powershell哈希表值?

Sql server 如何附加到powershell哈希表值?,sql-server,powershell,hashtable,smo,Sql Server,Powershell,Hashtable,Smo,我正在浏览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($logi

我正在浏览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())       
    }
}
到目前为止还不错。我现在要做的是在哈希表值的末尾追加一个字符串。因此,对于$instance,我想在该$instance的哈希表值中附加一个字符串。我该怎么做?我已经开始这样做了,但我不确定我是否在正确的轨道上:

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

使用
.Script()
方法返回字符串集合。也许有一种更优雅的方法,但是

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

应该行。

我会用这个代码

$h= @{}

$h.add("Test", "Item")
$h; ""
$h."Test" += " is changed"
$h

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

Test                           Item is changed                                                                                                                                         
$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()
    }
}


结果将是一个哈希表,每个实例作为一个键,以及一个字符串数组,其中每个字符串都是用户的T-SQL脚本。

顺便说一句,它不必是一个哈希表,也许是一个
System.Collections.Generic.Dictionary[string,string]
会更好吗?马克:PowerShell中的泛型有点讨厌。大多数时候,我会说,如果你并不迫切需要它们,就不要使用它们,因为代码实际上不太清晰。
$h.Test实际上已经足够了。如果键名中没有奇怪的字符,则不需要引号。是。同意。我想展示这些引语,让其他人知道这是一种选择。
$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()
        }
    }
}