2013-09-30 31 views
0

我想找到一種方法來替換數組變量末尾的字符..試過各種方式,並沒有運氣..這裏是我有什麼Foreach和如果狀態替換字符powershell結束

foreach ($File in $Files){ 
if ($File.EndsWith(".test")) 
{ 
    #Replaces test with EURTest at the end of the string 
    $File2 += $_ -replace "test", "EURTest" 

} 
elseif ($CanBeRemovedRoamingProfile.EndsWith("CTE")) 
{ 
    # Do nothing 
    $File2 += $File 

} 
else{ 
    $File2 += $_ + '.Final' 
} 
} 

有什麼想法嗎?

+0

這樣做會產生什麼結果是不可取的?如何定義'$ File2'? – BACON

+0

好吧..它似乎並沒有工作..它沒有吐出任何錯誤.. $ File2只是另一個變量(數組),將持有正確的結局的新列表 – tyson619

回答

3

在...

$File2 += $_ -replace "test", "EURTest" 

......還有......

$File2 += $_ + '.Final' 

...你在$_ variable操作。 $_將被用於ForEach-Object cmdlet,而在foreach循環中,您應該使用循環變量,在這種情況下爲$File

+0

偉大的,做了伎倆。 。 再次感謝您的幫助 :) – tyson619