none
How to run Powershell script using get-content cmdlet RRS feed

  • Question

  • Hi Guys,

      I am trying to read the contents of a text file but also get Powershell to run this  as well.

      Tried a few methods but cannot get it to work.

      Sure it is very simple. Is anyone able to help?

    Thanks

    Monday, November 26, 2018 8:31 PM

Answers

  • I suspect that you have a variable scope problem. See https://blogs.msdn.microsoft.com/powershell/2007/04/14/controlling-the-scope-of-variables/

    You may need to define variables that are referenced by different functions as global using the set-variable command. 

    • Marked as answer by sabedin77 Tuesday, November 27, 2018 6:19 PM
    Monday, November 26, 2018 10:47 PM

All replies

  • get-content  foobar.txt | Invoke-Expression

    Or 

    $FileData = get-content  foobar.txt
    $FileData | Invoke-Expression

    Monday, November 26, 2018 8:48 PM
  • #Gets content of file and assigns content to a Variable 

    $MyVar = Get-Content C:\temp\MyFile.txt
    
    invoke-item c:\temp\MyFile.txt


    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-6


    Monday, November 26, 2018 8:50 PM
  • [Note - Regarding the previously posted answers: This is a bad idea unless you trust the content of the text file. Why not just create a .ps1 script file and run that?]

    Why? What problem are you trying to solve?


    -- Bill Stewart [Bill_Stewart]

    Monday, November 26, 2018 8:51 PM
    Moderator
  • Hi Bill,




    Unfortunately the other answers did not help.


    I have written a Powershell script(.txt) and another Powershel(.psm1) module that the script imports. Everything works as long as I paste the .txt file into Powershell. After this it is all GUI based and allows me to log into O365 admin portal and add and remove permissions from users.


    Naively I thought all i needed to do was to turn the .txt file into a .ps1 and then run that. This completely messes up my script, at the point that I need to input my O365 credentials. It asks me to do this 3 times(should only be once) and then my script terminates with an error.


    This is really the last hurdle I need to cross with this.


    Any ideas?


    Thanks



    • Edited by sabedin77 Monday, November 26, 2018 9:03 PM
    Monday, November 26, 2018 9:02 PM
  • What error do you get with the method that I suggested?

    get-content  c:\yourfolder\yourscriptfile.txt | Invoke-Expression

    Monday, November 26, 2018 9:28 PM
  • The script does run, but as explained above, it then prompts me to input my O365 credentials 3 times and then terminates any further execution.

    When I paste the .txt script into powershell everything works as expected though. I really thought it was a simple case of changing the .txt to .ps1 and running that.





    • Edited by sabedin77 Monday, November 26, 2018 11:01 PM
    Monday, November 26, 2018 9:39 PM
  • $scripttext = Get-COntent file.txt

    [scriptblock]::Create($scripttext).Invoke()


    \_(ツ)_/

    Monday, November 26, 2018 9:56 PM
  • Interesting. The .invoke echo's the second write-output. 

    Monday, November 26, 2018 10:41 PM
  • I suspect that you have a variable scope problem. See https://blogs.msdn.microsoft.com/powershell/2007/04/14/controlling-the-scope-of-variables/

    You may need to define variables that are referenced by different functions as global using the set-variable command. 

    • Marked as answer by sabedin77 Tuesday, November 27, 2018 6:19 PM
    Monday, November 26, 2018 10:47 PM
  • Works fine if your file is a good script.

    PS D:\scripts> $scripttext = @'
    >> Write-Output 'line one'
    >> write-Output 'line two'
    >> '@
    >>
    PS D:\scripts> [scriptblock]::Create($scripttext).Invoke()
    line one
    line two
    PS D:\scripts>
    


    \_(ツ)_/

    Monday, November 26, 2018 10:55 PM
  • #test.txt
    Write-Output 'line one'
    write-Output 'line two'
    Write-Output $args[0]
    PS D:\scripts> $scripttext = Get-Content test.txt -Raw
    PS D:\scripts> [scriptblock]::Create($scripttext).Invoke()
    line one
    line two
    PS D:\scripts>
    PS D:\scripts> [scriptblock]::Create($scripttext).Invoke('Hello from caller')
    line one
    line two
    Hello from caller
    PS D:\scripts>
    You can even pass arguments.


    \_(ツ)_/


    • Edited by jrv Monday, November 26, 2018 10:59 PM
    Monday, November 26, 2018 10:58 PM
  • The difference is the -raw switch.

    PS C:\temp> $FileData = get-content  foobar.txt
    PS C:\temp> $filedata
    write-output 'Hello from foobar.'
    write-output 'Multi line works.'
    PS C:\temp>  [scriptblock]::Create($filedata).Invoke()
    Hello from foobar.
    write-output
    Multi line works.
    PS C:\temp> $FileData = get-content  foobar.txt -raw
    PS C:\temp> $filedata
    write-output 'Hello from foobar.'
    write-output 'Multi line works.'
    PS C:\temp>  [scriptblock]::Create($filedata).Invoke()
    Hello from foobar.
    Multi line works.

    -Raw

    Ignores newline characters and returns the entire contents of a file in one string with the newlines preserved. By default, newline characters in a file are used as delimiters to separate the input into an array of strings.

    -Raw is a dynamic parameter that the FileSystem provider adds to the Get-Content cmdlet This parameter works only in file system drives.

    Tuesday, November 27, 2018 2:18 AM
  • sabedin77,

    please add the -raw switch to the get-content command and see if that changes anything.

    get-content  c:\yourfolder\yourscriptfile.txt -raw | Invoke-Expression

    Tuesday, November 27, 2018 2:22 AM
  • Thanks guys for all your help.

    Now it is obvious that it is a scoping issue - All I needed to do was make two of my variables on my first script as global and the script and script module work perfectly together. 

    I am learning so much by posting here and always very greatful for the assistance.

    Tuesday, November 27, 2018 6:22 PM
  • $String = Get-Content C:\text.txt
    $ScriptBlock = [Scriptblock]::Create($String)
    Invoke-Command -ScriptBlock $ScriptBlock


    Tuesday, November 27, 2018 6:28 PM
  • Why Invoke-Command?

    Just write the text file as a .ps1 file and execute it.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, November 27, 2018 7:11 PM
    Moderator
  • Get-Content .\testps1.txt -raw | Invoke-Expression

    It is still easier to just use a PS1 file.

    Why do you think it is necessary to use a TXT file.  Are you trying to create a hack that bypasses execution Policy. Just do this

    powershell -ExecutionPolicy Bypass -File test.ps1


    \_(ツ)_/

    Tuesday, November 27, 2018 7:29 PM
  • Exactly my question. Why do things in a roundabout, indirect way when there's a more direct way that is simpler and easier?

    -- Bill Stewart [Bill_Stewart]

    Tuesday, November 27, 2018 8:01 PM
    Moderator