Matthias Puchta
Matthias Puchta

Reputation: 23

PowerShell, search for specific URL parts in .url files

Building on the code from this answer, I tried this to find all .url files which include http://kfhntwvap347, but I also tried http://kfhntwvap347:8080/consense/ in $urlParts knowing that there is such a .url file, but it always shows no results.

The intent is to find such URL and then to replace it.

# The literal URL parts to find, either prefixes or substrings.
$urlParts = 'http://kfhntwvap347'

$regex = '^URL=({0})' -f (
  $urlParts.ForEach({ 
    $escaped = [regex]::Escape($_) # Escape for literal matching, if needed.
    if ($escaped -match '^https?:') { $escaped } 
    else                            { '.*' + $escaped } # match anywhere in the URL
  }) -join '|'
)

# Search all *.url files in the subtree of D:\waveprod\DMS for the URL parts
# and output the full paths of matching files.
# Outputs to the screen and saves to a file.

$foundPaths = Get-ChildItem -Force -Recurse D:\waveprod\DMS -Filter *.url | 
  Select-String -Pattern $regex -List | 
  ForEach-Object {
    $path = $_.Path
    Write-Output $path
    $path
  }

# Display a message based on whether results were found or not
if ($foundPaths.Count -gt 0) {
  Write-Host "Results found!"
} else {
  Write-Host "No results found."
}

# Save the found paths to a text file in D:\powershell\
$foundPaths | Out-File -FilePath 'D:\powershell\found_paths.txt'

enter image description here

I would expect that it finds such files but I tried many variations without success.

Upvotes: 1

Views: 136

Answers (1)

Dennis
Dennis

Reputation: 1784

I find using type casting (if available) will help when working with some type of complex objects.

C:\> [uri]'http://kfhntwvap347:8080/consense/'


AbsolutePath   : /consense/
AbsoluteUri    : http://kfhntwvap347:8080/consense/
LocalPath      : /consense/
Authority      : kfhntwvap347:8080
HostNameType   : Dns
IsDefaultPort  : False
IsFile         : False
IsLoopback     : False
PathAndQuery   : /consense/
Segments       : {/, consense/}
IsUnc          : False
Host           : kfhntwvap347
Port           : 8080
Query          :
Fragment       :
Scheme         : http
OriginalString : http://kfhntwvap347:8080/consense/
DnsSafeHost    : kfhntwvap347
IdnHost        : kfhntwvap347
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

So by comparing the property Authority with Host (in this case filtering), you can easily tell if the URL-file needs to be handled.

[uri]'http://kfhntwvap347:8080/consense/',
[uri]'http://kfhntwvap347/consense/' |
  where {$_.Host -ne $_.Authority} |
    select AbsoluteUri

AbsoluteUri
-----------
http://kfhntwvap347:8080/consense/

In this case we also need a good way to handle ini-files. PsIni from PSGallery is a good bet.

Install-Module PsIni

Get-ChildItem .\*.url | select Name,
  @{ l = 'URI'; e = {[uri](Get-IniContent $_).InternetShortcut.Url} } |
    where {$_.URI.Host -ne $_.URI.Authority}

Edit: I didn't notice the property IsDefaultPort before which makes to selection even easier.

Upvotes: 0

Related Questions