我试图搜索未安装的更新的日志文件,然后使用返回的数组安装更新。 问题是我的文件被命名为:
Windows6.1-KB3102429-v2-x64.msu我的解析数组有一个KB3102429项我怎样才能使用通配符 - 调用数组项 - 然后是另一个通配符.msu
我的代码如下:
# Read KBLIST.txt and create array of KB Updates that were not installed $Failed = Get-Content -Path C:/Updates/KBLIST.txt | Where-Object {$_ -like '*NOT*'} # create a list of all items in Updates folder $dir = (Get-Item -Path "C:\Updates" -Verbose).FullName # Parse the $Failed array down to just the KB####### for($i = $Failed.GetLowerBound(0); $i -le $Failed.GetUpperBound(0); $i++) { $Failed[$i][1..9] -join "" # Search the $dir list for files that contain KB####### and end in .msu then quiet install Foreach($item in (ls $dir *$Failed[$i]*.msu -Name)) { echo $item $item = "C:\Updates\" + $item wusa $item /quiet /norestart | Out-Null } }它适用于Foreach($item in (ls $dir *$Failed[$i]*.msu -Name)) 。
如果我只是使用*而不是wildcard,string,wildcard它将返回所有.msu文件的列表,以查找其正确的基本语法。
I am trying to search a log file for updates that were not installed, then using the returned array install the updates. Problem is my files are named:
Windows6.1-KB3102429-v2-x64.msuMy parsed array has a item of KB3102429 how can I use a wild card - call the array item - then another wildcard .msu
my code is listed below:
# Read KBLIST.txt and create array of KB Updates that were not installed $Failed = Get-Content -Path C:/Updates/KBLIST.txt | Where-Object {$_ -like '*NOT*'} # create a list of all items in Updates folder $dir = (Get-Item -Path "C:\Updates" -Verbose).FullName # Parse the $Failed array down to just the KB####### for($i = $Failed.GetLowerBound(0); $i -le $Failed.GetUpperBound(0); $i++) { $Failed[$i][1..9] -join "" # Search the $dir list for files that contain KB####### and end in .msu then quiet install Foreach($item in (ls $dir *$Failed[$i]*.msu -Name)) { echo $item $item = "C:\Updates\" + $item wusa $item /quiet /norestart | Out-Null } }It works down to the Foreach($item in (ls $dir *$Failed[$i]*.msu -Name)).
If I just use * instead of the wildcard,string,wildcard it returns a list of all the .msu files for the basic syntax it correct.
最满意答案
自从您使用别名后,很难继续您的工作,但我认为这应该能够完成您要查找的内容。
$UpdateFolder = 'C:\Updates' $FailedUpdates = Get-Content -Path C:/Updates/KBLIST.txt | Where-Object {$_ -like '*NOT*'} foreach ( $Update in $FailedUpdates ) { Write-Host -Object "Update $Update failed" $UpdatePath = Get-Item -Path "$UpdateFolder\*$Update*.msu" | Select-Object -ExpandProperty FullName Write-Host -Object "`tReinstalling from path: $UpdatePath" wusa $UpdatePath /quiet /norestart | Out-Null }It was hard to follow your work since you used aliases, but I think this should be able to accomplish what you're looking for.
$UpdateFolder = 'C:\Updates' $FailedUpdates = Get-Content -Path C:/Updates/KBLIST.txt | Where-Object {$_ -like '*NOT*'} foreach ( $Update in $FailedUpdates ) { Write-Host -Object "Update $Update failed" $UpdatePath = Get-Item -Path "$UpdateFolder\*$Update*.msu" | Select-Object -ExpandProperty FullName Write-Host -Object "`tReinstalling from path: $UpdatePath" wusa $UpdatePath /quiet /norestart | Out-Null }更多推荐
发布评论