Powershell脚本语法问题(Powershell script syntax issue)
$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. $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 -ForceWhen I try to run this script, I get the following error:
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.最满意答案
不确定你想要做什么,但你应该试试这个:
$arguments=@("x", "$($_.FullName)", "-o`"$($parent)`"", "-y");in -o argument`(back quote)允许你在由双引号分隔的字符串中使用“(双引号)”。
Get-ChildItem t.txt | % {$parent="coucou";$arguments=@("x", "$($_.FullName)", "-o`"$($parent)`"", "-y");$arguments}给出:
x C:\temp\t.txt -o"coucou" -yNot sure exactly what you want to do, but you should try this :
$arguments=@("x", "$($_.FullName)", "-o`"$($parent)`"", "-y");in -o argument `(back quote) allow you to use " (double quote) inside a string delimited by doubles quotes.
Get-ChildItem t.txt | % {$parent="coucou";$arguments=@("x", "$($_.FullName)", "-o`"$($parent)`"", "-y");$arguments}gives :
x C:\temp\t.txt -o"coucou" -y更多推荐
发布评论