Combining Start-BitsTransfer with Select-String without a temporary file
up vote
1
down vote
favorite
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?
powershell
add a comment |
up vote
1
down vote
favorite
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?
powershell
the*bits*
cmdlets are file transfer cmdlets. you need to use it for files. [grin] ///// there is an automatic $Var named$Matches
- you REALLY otta not use that $Var name. ///// what are you trying to achieve by NOT putting the result ofSelect-String
into a $Var? if all you want is the 1st match, you can use(Get-Content C:file.txt | Select-String -SimpleMatch 'WordToMatch')[0].ToString()
to get the text of the 1st match found.
– Lee_Dailey
Nov 11 at 1:41
If bits are for file transfers, is there something better to do? Or is creating a temporary file the best way to go? $Matches came from a different post when I was learning how to isolate a part of a file: codereview.stackexchange.com/a/51478 . What I'm trying to do by not creating a file is just to simplify the code. Since all I want is to quickly access a string once, I was hoping not to find it, save it as a variable, and then read the variable.
– GFL
Nov 11 at 2:34
it looks like mklement0 has posted the answer to your question. his added suggestion to get the HTML file directly seems likely to be what you want for getting a file from a web page.
– Lee_Dailey
Nov 11 at 14:07
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
powershell
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?
powershell
powershell
asked Nov 11 at 0:59
GFL
300310
300310
the*bits*
cmdlets are file transfer cmdlets. you need to use it for files. [grin] ///// there is an automatic $Var named$Matches
- you REALLY otta not use that $Var name. ///// what are you trying to achieve by NOT putting the result ofSelect-String
into a $Var? if all you want is the 1st match, you can use(Get-Content C:file.txt | Select-String -SimpleMatch 'WordToMatch')[0].ToString()
to get the text of the 1st match found.
– Lee_Dailey
Nov 11 at 1:41
If bits are for file transfers, is there something better to do? Or is creating a temporary file the best way to go? $Matches came from a different post when I was learning how to isolate a part of a file: codereview.stackexchange.com/a/51478 . What I'm trying to do by not creating a file is just to simplify the code. Since all I want is to quickly access a string once, I was hoping not to find it, save it as a variable, and then read the variable.
– GFL
Nov 11 at 2:34
it looks like mklement0 has posted the answer to your question. his added suggestion to get the HTML file directly seems likely to be what you want for getting a file from a web page.
– Lee_Dailey
Nov 11 at 14:07
add a comment |
the*bits*
cmdlets are file transfer cmdlets. you need to use it for files. [grin] ///// there is an automatic $Var named$Matches
- you REALLY otta not use that $Var name. ///// what are you trying to achieve by NOT putting the result ofSelect-String
into a $Var? if all you want is the 1st match, you can use(Get-Content C:file.txt | Select-String -SimpleMatch 'WordToMatch')[0].ToString()
to get the text of the 1st match found.
– Lee_Dailey
Nov 11 at 1:41
If bits are for file transfers, is there something better to do? Or is creating a temporary file the best way to go? $Matches came from a different post when I was learning how to isolate a part of a file: codereview.stackexchange.com/a/51478 . What I'm trying to do by not creating a file is just to simplify the code. Since all I want is to quickly access a string once, I was hoping not to find it, save it as a variable, and then read the variable.
– GFL
Nov 11 at 2:34
it looks like mklement0 has posted the answer to your question. his added suggestion to get the HTML file directly seems likely to be what you want for getting a file from a web page.
– Lee_Dailey
Nov 11 at 14:07
the
*bits*
cmdlets are file transfer cmdlets. you need to use it for files. [grin] ///// there is an automatic $Var named $Matches
- you REALLY otta not use that $Var name. ///// what are you trying to achieve by NOT putting the result of Select-String
into a $Var? if all you want is the 1st match, you can use (Get-Content C:file.txt | Select-String -SimpleMatch 'WordToMatch')[0].ToString()
to get the text of the 1st match found.– Lee_Dailey
Nov 11 at 1:41
the
*bits*
cmdlets are file transfer cmdlets. you need to use it for files. [grin] ///// there is an automatic $Var named $Matches
- you REALLY otta not use that $Var name. ///// what are you trying to achieve by NOT putting the result of Select-String
into a $Var? if all you want is the 1st match, you can use (Get-Content C:file.txt | Select-String -SimpleMatch 'WordToMatch')[0].ToString()
to get the text of the 1st match found.– Lee_Dailey
Nov 11 at 1:41
If bits are for file transfers, is there something better to do? Or is creating a temporary file the best way to go? $Matches came from a different post when I was learning how to isolate a part of a file: codereview.stackexchange.com/a/51478 . What I'm trying to do by not creating a file is just to simplify the code. Since all I want is to quickly access a string once, I was hoping not to find it, save it as a variable, and then read the variable.
– GFL
Nov 11 at 2:34
If bits are for file transfers, is there something better to do? Or is creating a temporary file the best way to go? $Matches came from a different post when I was learning how to isolate a part of a file: codereview.stackexchange.com/a/51478 . What I'm trying to do by not creating a file is just to simplify the code. Since all I want is to quickly access a string once, I was hoping not to find it, save it as a variable, and then read the variable.
– GFL
Nov 11 at 2:34
it looks like mklement0 has posted the answer to your question. his added suggestion to get the HTML file directly seems likely to be what you want for getting a file from a web page.
– Lee_Dailey
Nov 11 at 14:07
it looks like mklement0 has posted the answer to your question. his added suggestion to get the HTML file directly seems likely to be what you want for getting a file from a web page.
– Lee_Dailey
Nov 11 at 14:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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, i.e. 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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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, i.e. 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.
add a comment |
up vote
2
down vote
accepted
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, i.e. 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.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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, i.e. 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.
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, i.e. 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.
edited Nov 12 at 15:47
answered Nov 11 at 2:39
mklement0
122k20233264
122k20233264
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244911%2fcombining-start-bitstransfer-with-select-string-without-a-temporary-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
the
*bits*
cmdlets are file transfer cmdlets. you need to use it for files. [grin] ///// there is an automatic $Var named$Matches
- you REALLY otta not use that $Var name. ///// what are you trying to achieve by NOT putting the result ofSelect-String
into a $Var? if all you want is the 1st match, you can use(Get-Content C:file.txt | Select-String -SimpleMatch 'WordToMatch')[0].ToString()
to get the text of the 1st match found.– Lee_Dailey
Nov 11 at 1:41
If bits are for file transfers, is there something better to do? Or is creating a temporary file the best way to go? $Matches came from a different post when I was learning how to isolate a part of a file: codereview.stackexchange.com/a/51478 . What I'm trying to do by not creating a file is just to simplify the code. Since all I want is to quickly access a string once, I was hoping not to find it, save it as a variable, and then read the variable.
– GFL
Nov 11 at 2:34
it looks like mklement0 has posted the answer to your question. his added suggestion to get the HTML file directly seems likely to be what you want for getting a file from a web page.
– Lee_Dailey
Nov 11 at 14:07