使用通配符字符串通配符的Powershell搜索路径(Powershell search path with wildcard

编程入门 行业动态 更新时间:2024-10-23 03:27:41
使用通配符字符串通配符的Powershell搜索路径(Powershell search path with wildcard-string-wildcard)

我试图搜索未安装的更新的日志文件,然后使用返回的数组安装更新。 问题是我的文件被命名为:

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.msu

My 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 }

更多推荐

本文发布于:2023-08-04 14:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串   路径   path   wildcard

发布评论

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

>www.elefans.com

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