0
$resources = @()
$resources += "PC1"
$creds [email protected]()
write-host "Obtaining creds from PMP"
for ($i=0; $i -lt $resources.length; $i++) {
$creds = get-creds $resources[$i] root
}
for ($i=0; $i -lt $resources.length; $i++) {
$sessionA = $(New-SshSession -ComputerName $resources[$i] -Username root -Password $creds[$i])
if ($sessionA -match "Successfully connected") {
x
}
else
{
error
}}
我一直試圖建立ssh連接時出錯。 如果我回應$ resources [0]和$ creds [0]的值,我會得到正確的結果,並可以使用這些值與ssh用膩子。 如果我創建新的變量:PowerShell陣列中的字符串問題
$tes1 = $resources[0]
$test2 = $creds[0]
,並同時使用在:
$sessionA = $(New-SshSession -ComputerName $test1 -Username root -Password $test2)
它也可以...
出於某種原因,數組引用不:/
感謝您的反饋。
對不起,它只是代碼的一部分。我用$ creds替換$ pass,就像它在我的代碼中一樣。 – Chris
仍然相同:P不喜歡我的數組字符串:/ – Chris
嘗試在第二個循環中使用另一個變量作爲計數器。所以,例如試一下:for($ y = 0; $ y-lt $ resources.length; $ y ++){... –