Powershell脚本语法问题(Powershell script syntax issue)

编程入门 行业动态 更新时间:2024-10-27 08:25:21
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 -Force

When 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" -y

Not 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

更多推荐

本文发布于:2023-04-28 04:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   脚本   Powershell   issue   syntax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!