2015-02-06 66 views
0

這可能是基本問題,你可以得到關於PowerShell的,但...我卡在這個仍然。使用PowerShell的內部數組,循環

我試圖從分段區域遠程服務器來我們的開發服務器上移動了一些文件夾的內容。爲了簡化代碼,我將分段位置放在一個數組中,並將文件夾名稱放在單獨的數組中。

我想運行一個for loop和處理兩個平行陣列...簡單吧?

這裏是環

Write-Host "Copying Files" -ForegroundColor Yellow 
For ($i=0; $i -lt $periscopeServicesArray.Length; $i++) 
{ 
    Write-Host "Copying $periscopeServicesArray[$i]" -ForegroundColor Yellow 
    if(Test-Path -path "$domainpath\$periscopeWebServices\$periscopeServicesArray[$i]") 
    { 
     copy-item -Path "$stagingArray[$i]"\* -Destination "$domainpath\$periscopeWebServices\'$periscopeServicesArray[$i]'" -recurse -Force 
    } 
} 

出於某種原因,if statment返回false每次。如果我打印出的"$domainpath\$periscopeWebServices\$periscopeServicesArray[$i]"是給解釋此語句中的值是什麼,我得到:

E:\ web內容\ InsideServices.dev.com \ periscopeWebServices \ periscopeRetrieveComments periscopeRetrieveGeneral periscopeRetrieveMasterSubs periscopeRetrieveProducers periscopeRetrieveProfiles periscopeRetrieveRelationships periscopeSearch [0 ]

前兩個變量正確打印,但是數組的整個內容似乎每次都在索引值的末尾溢出。

這裏是變量/因爲我已經創造了他們的陣列

#web services 
$periscopeWebServices = "periscopeWebServices" 
$periscopeRetrieveComments = "periscopeRetrieveComments" 
$periscopeRetrieveGeneral = "periscopeRetrieveGeneral" 
$periscopeRetrieveMasterSubs = "periscopeRetrieveMasterSubs" 
$periscopeRetrieveProducers = "periscopeRetrieveProducers" 
$periscopeRetrieveProfiles = "periscopeRetrieveProfiles" 
$periscopeRetrieveRelationships = "periscopeRetrieveRelationships" 
$periscopeSearch = "periscopeSearch" 
$periscopeServicesArray = @($periscopeRetrieveComments, $periscopeRetrieveGeneral, 
          $periscopeRetrieveMasterSubs, $periscopeRetrieveProducers, 
          $periscopeRetrieveProfiles, $periscopeRetrieveRelationships, 
          $periscopeSearch) 


#staging areas 
$stagingComments = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveComments" 
$stagingGeneral = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveGeneral" 
$stagingMasterSubs = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveMasterSubs" 
$stagingProducers = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveProducers" 
$stagingProfiles = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveProfiles" 
$stagingRelationships = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveRelationships" 
$stagingSearch = "\\install\PeriscopeServices\periscopeWebServices\periscopeSearch" 
$stagingArray = @($stagingComments, $stagingGeneral, $stagingMasterSubs, $stagingProducers 
        $stagingProfiles, $stagingRelationships, $stagingSearch) 

#server variables 
$domain = "InsideServices.dev.com" 
$directory = "E:\webcontent" 
$domainpath = "$directory\$domain" 

回答

2

這是因爲你的引用變量的方法,你需要使用的時候「越獄」的數組元素訪問爲$($array[$elem])字符串"something $($isAt[$here])"

Write-Host "Copying Files" -ForegroundColor Yellow 
For ($i=0; $i -lt $periscopeServicesArray.Length; $i++) 
{ 
    Write-Host "Copying $($periscopeServicesArray[$i])" -ForegroundColor Yellow 
    if(Test-Path -path "$domainpath\$periscopeWebServices\$($periscopeServicesArray[$i])") 
    { 
     copy-item -Path "$($stagingArray[$i])"\* -Destination "$domainpath\$periscopeWebServices\$($periscopeServicesArray[$i])" -recurse -Force 
    } 
}