powershell脚本出错

时间:2017-04-22 05:26:16

标签: powershell powershell-v4.0

我在脚本中遇到以下错误,该脚本会自动安装当前目录中的所有更新:

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
}

2 个答案:

答案 0 :(得分:1)

在“where”之后你需要一个通常用大括号括起来的脚本块,而不是简单的。

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

答案 1 :(得分:0)

  • 您在第一个foreach中使用var $ msu而不是$ package。
  • 除了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
}