Larvaman
Larvaman

Reputation: 45

Powershell search path with wildcard-string-wildcard

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.

Upvotes: 0

Views: 4284

Answers (1)

Shawn Esterman
Shawn Esterman

Reputation: 2342

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
}

Upvotes: 1

Related Questions