2017-04-22 109 views
0

我正在与自动安装在当前目录的所有更新的脚本如下错误:错误PowerShell脚本

At K:\Operating System\ConfigureWindows10\Updates\Install-Updates.ps1:15 char:25 
+   $command = "Expand –F:* '" + $msu.fullname + "' '" + $PSScr ... 
+        ~~~ 
Unexpected token 'F:*' in expression or statement. 
At K:\Operating System\ConfigureWindows10\Updates\Install-Updates.ps1:29 char:82 
+ ... = "Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase" 
+                   ~ 
The string is missing the terminator: ". 
At K:\Operating System\ConfigureWindows10\Updates\Install-Updates.ps1:13 char:5 
+  { 
+  ~ 
Missing closing '}' in statement block or type definition. 
At K:\Operating System\ConfigureWindows10\Updates\Install-Updates.ps1:10 char:1 
+ { 
+ ~ 
Missing closing '}' in statement block or type definition. 
    + CategoryInfo   : ParserError: (:) [], ParseException 
    + FullyQualifiedErrorId : UnexpectedToken 

这里是代码体:

{ 
$msu = get-childitem -path $PSScriptRoot -Recurse | where ($_.extension -eq ".msu") | select fullname 
foreach($package in $msu) 
{ 
    write-debug $msu.fullname 
    $command = "Expand –F:* '" + $msu.fullname + "' '" + $PSScriptRoot + "'" 
    write-debug $command 
    Invoke-Expression $command 
} 

$updates = get-childitem -path $PSScriptRoot -Recurse | where ($_.extension -eq ".cab") | select fullname 
foreach($update in $updates) 
{ 
    write-debug $update.fullname 
    $command = "dism /online /add-package /packagepath:'" + $update.fullname + "'" 
    write-debug $command 
    Invoke-Expression $command 
} 

$command = "Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase" 
Invoke-Expression $command 
} 
+0

当保存脚本文件因为UTF-8使用带** BOM **的UTF-8。 – PetSerAl

+0

谢谢。该脚本正常工作,但我收到另一个错误。该脚本不会扩展目录中的任何msu文件。 –

+0

代码更新: –

回答

1

后“其中”你需要一个脚本块,通常花括号,而不是简单的脚本块。

...| where {$_.extension -eq ".msu"} |... 
+0

更改()为{}确实可行,但该脚本仅执行Dism.exe/online/Cleanup-Image/StartComponentCleanup/ResetBase。没有别的被执行。 –

+0

在循环中:foreach($ package in $ msu){...}您使用的$ msu.fullname不好,您应该使用$ package.fullname来代替。 – placa

0
  • 使用var $ MSU而不是$包在第一的foreach。
  • 除了where子句中的大括号外,您可以省略它 在Get-ChildItem中指定-Filter *.msu参数。
  • IMO的选择是没有必要的
  • 的报价看起来不正确,请使用此字符串,而不是

试试这个(未经测试)

{ 
    $msu = get-childitem -path $PSScriptRoot -Recurse -Filter *.msu 
    foreach($package in $msu){ 
     write-debug $package.FullName 
$command = @' 
& Expand.exe –F:* "$package.FullName" "$PSScriptRoot" 
'@ 
     write-debug $command 
     Invoke-Expression $command 
    } 
    $updates = get-childitem -path $PSScriptRoot -Recurse -Filer *.cab 
    foreach($update in $updates){ 
     write-debug $update.fullname 
$command = @' 
& Dism.exe /online /add-package /packagepath:"$update.FullFame" 
'@ 
     write-debug $command 
     Invoke-Expression $command 
    } 
    $command = "Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase" 
    Invoke-Expression $command 
} 
+0

在K:\ OperatingSystem \ ConfigureWindows10 \ Updates \ Extract-Updates.ps1:8 char:5 +'@ + ~~ 在字符串终止符之前不允许有空格。 在K:\ OperatingSystem \ ConfigureWindows10 \ Updates \ Extract-Updates.ps1:4 char:1 + { +〜 在语句块或类型定义中缺少关闭'}'。 在K:\ OperatingSystem \ ConfigureWindows10 \ Updates \ Extract-Updates.ps1:1 char:1 + { +〜 在语句块或类型定义中缺少关闭'}'。 + CategoryInfo:ParserError:(:) [],ParseException + FullyQualifiedErrorId:WhitespaceBeforeHereStringFooter –

+0

错误。请帮忙。 –

+0

更改上面的脚本,使用这里的字符串删除缩进。 – LotPings