我在脚本中遇到以下错误,该脚本会自动安装当前目录中的所有更新:
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 :(得分:1)
在“where”之后你需要一个通常用大括号括起来的脚本块,而不是简单的。
...| where {$_.extension -eq ".msu"} |...
答案 1 :(得分:0)
-Filter *.msu
参数。试试这个(未经测试的)
{
$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
}