2015-02-06 45 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 
    } 
}