I'm trying to select a string in a remote file. Currently, I download the document to a temporary file and then search for my string there. I'm trying to pipe the commands together but it seems like Start-BitsTransfer needs a destination file. Can I do this without a temporary file?
Start-BitsTransfer -Source https://www.remoteserver/file.html -Destination C:\temp.html
$matches = Get-Content C:\temp.html -ErrorAction SilentlyContinue | Select-String '(http.*pdf)'
$matches[0].Matches.Groups[1].Value
Further, is it possible to output the first match in one line without having to create the variable?
it seems like Start-BitsTransfer needs a destination file. Can I do this without a temporary file?
No, because PowerShell has no construct that is equivalent to Bash's output process substitutions ( >(...)
) [1] , which is what you'd need here:
# Wishful thinking - does NOT work.
Start-BitsTransfer -Source https://www.remoteserver/file.html -Destination `
>(Select-String '(http.*pdf)')
However, you can use Invoke-RestMethod
to retrieve a text-based file such as an HTML page via HTTP and have its content output to the success stream, so you can pipe it to other commands:
Invoke-RestMethod -UseBasicParsing https://www.remoteserver/file.html |
Select-String '(http.*pdf)'
is it possible to output the first match in one line without having to create the variable?
Yes, you can use a ForEach-Object
call to extract the capture group of interest:
Invoke-RestMethod -UseBasicParsing https://www.remoteserver/file.html |
Select-String -List '(http.*pdf)' |
ForEach-Object { $_.Matches[0].Groups[1].Value }
Note that -List
makes Select-String
stop after the first line on which a match is found, ie after the first match in the input; omit it to find all matches in the file (one per line; to find multiple matches per line , add -AllMatches
).
[1] Making PowerShell support process substitutions is the subject of this feature request on GitHub .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.