在一行中创建包含4个值的变量

时间:2015-11-10 09:56:54

标签: powershell

我通过读取这样的哈希表来部署NetworkDrives和PowerShell:

$ITDrives = @{
    "l:" = "\\server\share";
    "m:" = "\\server\share";
    "o:" = "\\server\share";
    "p:" = "\\server\share";
    "v:" = "\\server\share";
}

[...]

$map.MapNetworkDrive($_.Name, $_.Value, $true)

[...] =中间有一些代码。

现在我想为DMZ中的NetworkDrives做同样的事情。不幸的是,我需要4个值。 " Letter,Path,Username和Passwor"

我无法通过此方式创建Hashtable。我还有其他选择吗?

它应该是这样的

    $DMZDrives = @(
    "u:" "\\SERVER1\SHARE" "domain\administrator" "$PW";
    "r:" "\\SERVER2\SHARE" "domain\administrator" "$PW";
    "w:" "\\SERVER3\SHARE" "domain\administrator" "$PW";
    )

1 个答案:

答案 0 :(得分:4)

我建议您使用PsCustomObject

$DMZDrives = @(
    [PSCustomObject]@{Letter='u:'; Path='\\SERVER1\SHARE'; Username='domain\administrator'; Password='yourPassword'}    
    [PSCustomObject]@{Letter='r:'; Path='\\SERVER2\SHARE'; Username='domain\administrator'; Password='yourPassword'}
    [PSCustomObject]@{Letter='w:'; Path='\\SERVER3\SHARE'; Username='domain\administrator'; Password='yourPassword'}
)

$DMZDrives的输出:

Letter Path            Username             Password    
------ ----            --------             --------    
u:     \\SERVER1\SHARE domain\administrator yourPassword
r:     \\SERVER2\SHARE domain\administrator yourPassword
w:     \\SERVER3\SHARE domain\administrator yourPassword

现在您有一个所需对象的列表,可以使用众所周知的过滤器,例如:

$DMZDrives | where Letter -eq 'u:'

或者循环遍历它们并映射网络驱动器:

$DMZDrives | % {
    $map.MapNetworkDrive($_.Letter, $_.Path, $true, $_.Username, $_.Password)
}