2014-06-18 5 views
0

내가 함께 조금 고투하고이 코드를 가지고 :문자열을 PowerShell에서 변수로 바꾸는 방법은 무엇입니까?

$NumberOfProfiles = Read-Host "Enter a number of profiles" 
$Directory = "C:\Users\Apple\Folder(1..HowMuchProfilesCreated\Text.txt" 
Variable1 = "Plane" 
Variable2 = "Yacht" 
Variable100 = "Bike" 
while ($NumberOfProfiles -gt 0) { 
(Get-Content $Directory) | %{$_.Replace("Car", "Don'tKnowWhatToPutHere")} | Set-Content $Directory 
$NumberOfProfiles-- 

Text.txt 파일이 모든 폴더에 동일 나는이 경우 (예 : "자동차"로 변경하고자하는 동일한 문자열을 포함)하지만 새로운 문자열은 전에 선언 한 변수에서 가져온 것이어야합니다 (배열을 사용하는 것이 더 좋을 수도 있습니다)

예 : $ NumberOfProfiles = 10이고 정확히 동일한 Text.txt가있는 10 개의 폴더를 만들고 folder1의 Variable1, folder2의 Variable2 등으로 교체해야하는 "Car"가 필요합니다.

+0

확실하지가 발생합니다,하지만 당신은 PARAM ( [문자열] $ <여기서 variableName> 와 PS 스크립트에 전달 paramters를 호출 할 수 있습니다 : 예는 설명하기) – VikingBlooded

답변

2

변수 이름을 구성 할 수있는 경우 Get-Variable을 사용할 수 있습니다. 이 도움이된다면

$Variable1 = "Bike" 
$FolderID = 1 
Get-Variable -Name "Variable$FolderID" -ValueOnly 

Bike 
+0

완벽하게 작동했습니다! 너 덕분에 수천! – JohannesTK

관련 문제