programing

powershell: 문자열을 평가하는 방법

itmemos 2023. 9. 9. 09:04
반응형

powershell: 문자열을 평가하는 방법

내파일a.txt포함 내용:

delete from test_$suffix

My PowerShell은 다음과 같습니다.

$a = get-content a.txt
$suffix = "tableA"

내가 어떻게 조작하겠습니까?$a끈을 얻다delete from test_tableA?

$a=get-content a.txt
$suffix="tableA"

$ExecutionContext.InvokeCommand.ExpandString($a)

Invoke-Expression이 동치입니다.

$strExpression = "5 + 5 -eq 10"
Invoke-Expression $strExpression
True

자세한 정보는 http://technet.microsoft.com/en-us/library/ee176880.aspx 을 참조하십시오.

한가지 방법이 있습니다.이중 따옴표가 있는 여기 문자열의 변수는 자동으로 대체됩니다.입력 파일이 여기 문자열에 대한 PS 규칙을 준수하는지 확인하십시오.

 function convertto-herestring { 
 begin {$temp_h_string = '@"' + "`n"} 
 process {$temp_h_string += $_ + "`n"} 
 end { 
     $temp_h_string += '"@' 
     iex $temp_h_string 
     } 
 } 

 $suffix = "tableA"

 get-content testfile.txt

 delete from test_$suffix

 get-content testfile.txt | convertto-herestring

 delete from test_tableA

언급URL : https://stackoverflow.com/questions/4836703/powershell-how-to-evaluate-a-string

반응형