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?










share|improve this question





















  • 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










  • 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














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?










share|improve this question





















  • 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










  • 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












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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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










  • 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











  • 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












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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer


























      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.






      share|improve this answer
























        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 15:47

























        answered Nov 11 at 2:39









        mklement0

        122k20233264




        122k20233264



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3