none
Do Get-Content + Invoke-Expression Execute Statements Separately, or All At Once? RRS feed

  • General discussion

  • Consider this statement:

    Get-Content .\zipper.ps1 | Invoke-Expression

    Here's a variation documented in Technet.

    Invoke-Expression (Get-Content '.\zipper.ps1' -Raw)
    Both statements work just fine, tho' the 2nd one executes much faster. 

    Assume zipper.ps1 is a script containing multiple lines of executable code. 

    Will the above statement execute each line in zipper.ps1 as an individual command? 

    Or, will the entire script get executed as just one big concatenated string? 

    My question pertains to character limits for powershell statements-- if each code-line is within the powershell character-limit, but the entire script would exceed the character limit, will the above command exceed the character limit?

    ---

    Details:

    Technet states:

    "Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file."

    and

    "Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command."

    So far so good-- that explains pretty clearly how and why my statement above works. But it does not tell me if the statements are executed as individual statements, or as one big concatenated string. The fact that the 2nd variation runs faster may be a clue. 

    The technet doc says Get-Content returns a collection, while this Technet article says it returns an array. That's conflicting info, but not sure array vs collection matters re my question here. 

    thx




    Monday, October 3, 2016 2:52 AM

All replies

  • A script is a batch of commands. Commands are either one per line or are pipelines that can  be on one line or formatted to appear on more than one line.  A script is always executed from the first line and ends when the last line comes of when an exit, return or break statement is executed.  Two command lines can be concerned into one text line be placing a semicolon between commands.

    N0 a script is not a concatenated string. It would not work.


    \_(ツ)_/

    Monday, October 3, 2016 3:32 AM
  • To be more clear.

    Get-Content reads one line at a time into a variable or into the pipeline.

    Pipe it into Invoke expression feeds i one line at a time.

    Why would you do this?

    ".\zipper.ps1" will execute the script.  We would not use Invoke-Expression as it would not likely exe cute all kinds of and will not resolve dependencies between lines.  It would fail for nearly all scripts.

     


    \_(ツ)_/

    Monday, October 3, 2016 4:59 AM
  •  It would fail for nearly all scripts.

    You're saying the statement in my OP cannot work. 

    However, it works just fine for me. 

    It's documented here

    Therefor, i'll need to follow my direct experience, and the above documentation,

    and disregard your comments. 

    • Edited by johny w Monday, October 3, 2016 12:49 PM
    Monday, October 3, 2016 12:41 PM
  • What's provoking the question?

    -- Bill Stewart [Bill_Stewart]

    Monday, October 3, 2016 2:49 PM
    Moderator
  •  It would fail for nearly all scripts.

    You're saying the statement in my OP cannot work. 

    However, it works just fine for me. 

    It's documented here

    Therefor, i'll need to follow my direct experience, and the above documentation,

    and disregard your comments. 

    The discussion is not about what you are asking.  Please read it very carefully.  As I noted the Invoke-Expression against a file array may not be able to resolve all lines of a file individually.  If you fie works then OK but remember that it can fail.

    There is really little point to using this command even if it does work.  It has been used to execute arbitrary created text from an array or an @here string.


    \_(ツ)_/

    Monday, October 3, 2016 6:27 PM
  • will not resolve dependencies between lines. 

    i'm not seeing that issue.  Can you support your claim with reference to documentation?
    Tuesday, October 4, 2016 2:35 PM
  • Here is one of many ways that it can fail:

    Simple comamnd:

    $x='Get-Process spooler'
    $x | iex

    Now use an array of two lines.

    PS D:\scripts> $x='Get-Process `','spooler'
    PS D:\scripts> $x|iex
    iex : At line:1 char:15
    + Get-Process `
    +               ~
    Incomplete string token.
    At line:1 char:4
    + $x|iex


    \_(ツ)_/

    Tuesday, October 4, 2016 7:19 PM