Powershell脚本语法问题

时间:2015-02-25 04:05:48

标签: powershell

$Source = "[source]"   
$Destination = "[destination]"


xcopy $Source\* $Destination /Y

Get-ChildItem -path $Destination\* -Include *.zip,*.rar,*.7z | %{ 
    if($_.Name -match "^*.'.zip$" -or $_.Name -match "^*.'.7z$" -or $_.Name -match "^*.'.rar$"){
        $parent="$(Split-Path $_.FullName -Parent)";    
        $arguments=@("x", "'"$($_.FullName)'"", "-o'"$($parent)'" -y");
        $ex = start-process -FilePath "'"C:\Program Files\7-Zip\7z.exe'"" -ArgumentList $arguments -wait -PassThru;
        if( $ex.ExitCode -eq 0){
            rmdir -Path $_.FullName -Force
        }
    }
}
rmdir -Path $Source\* -recurse -Force

当我尝试运行此脚本时,出现以下错误:

Unexpected token '$(' in expression or statement.
At D:\Bluedoor\WealthNET Files\Interface Data\RMS\Untitled3.ps1:10 char:32
+         $arguments=@("x", "'"$( <<<< $_.FullName)'"", "-o'"$($parent)'" -y");
    + CategoryInfo          : ParserError: ($(:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken.

2 个答案:

答案 0 :(得分:0)

问题是你的报价。我想你想要这个

$arguments=@("x", ""'$($_.FullName)'"", "-o'"$($parent)'" -y");

答案 1 :(得分:0)

不确定你想要做什么,但你应该试试这个:

$arguments=@("x", "$($_.FullName)", "-o`"$($parent)`"", "-y");

in -o argument`(back quote)允许你使用&#34; (双引号)在由双引号分隔的字符串中。

Get-ChildItem t.txt | % {$parent="coucou";$arguments=@("x", "$($_.FullName)", "-o`"$($parent)`"", "-y");$arguments}

给出:

x
C:\temp\t.txt
-o"coucou"
-y