我的脚本有点麻烦。这是我的代码:
[int]$NumberOfProfiles = Read-Host "Enter the number of profiles You need"
$Array = @(Folder1..Folder100)
while ($NumberOfProfiles -gt 0) {
$rstr = $Array[$NumberOfProfiles]
Copy-Item $WhereToCopyFrom$rstr $WhereToCopyTo
$NumberOfProfiles--
}
所以基本上我有数组,那里有100个对象。但问题是当$ NumberOfProfiles大于100时,我将耗尽$ Array中的对象,所以我需要它从$ Array结束时开始重新开始。例如。当它击中$ Array中的Folder100时,它需要从Folder1再次启动。
答案 0 :(得分:1)
我不确定你的样品是做什么的。您的示例将具有无限循环,因为$NumberOfProfiles
始终大于0.此外,您每次都从数组中选择相同的文件夹,因为$NumberOfProfiles
永远不会更改。这是你想要的吗?
[int]$NumberOfProfiles = Read-Host "Enter the number of profiles You need"
$Array = @(Folder1..Folder100)
$index = 0
#Run as many times as specified in $NumberOfProfiles
for($i=0; $i -lt $NumberOfProfiles; $i++) {
#When reached end of array, reset index
if($index -eq $Array.Length) { $index = 0 }
#Get value
$rstr = $Array[$index]
#Copy folder
Copy-Item "$WhereToCopyFrom$rstr" "$WhereToCopyTo"
#Increase index
$index++
}