locked
Running PowerShell script from CMD.exe with text input file as a command line argument RRS feed

  • Question

  • I want to be able to pass a text input file to a PowerShell script at the cmd.exe command console like this:

    PS C:\> H:\Scripts\POWERSHELL\NewScript.ps1 H:\Scripts\POWERSHELL\TextInputFile.txt

    With VBScript code listed below, I could run scripts and supply the input files simply with no hard coding of paths:

    1. Open a cmd.exe window
    2. Drag and drop the VBScript file onto the cmd.exe window
    3. Activate the cmd.exe window and hit the space bar
    4. Drag and drop the text input file onto the cmd.exe window
    5. Hit the Enter key (script runs and processes the input file)

    The following code would easily verify that there weren't too many or too few cmd.exe command line arguments and then grab the full path to the input file to be easily opened and processed by the rest of the script.

    ============== VBScript Code =================
    If wscript.arguments.length <> 1 Then
         MsgBox "Please provide an input file!"
         wscript.quit 1
    End If
    FileToBeParsed = wscript.arguments.item(0)

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If oFSO.FileExists(FileToBeParsed) Then
         Set oParseFile = oFSO.OpenTextFile(FileToBeParsed, cForReading)
    Else
         wscript.echo "ERROR"
         wscript.quit 1
    End If
    ================ End Code ===================

    I am looking to replicate this kind of easy functionality and versatility in my PowerShell scripts, but I haven't had much success. I think the problem is with $args. Trying to simplify things, without any error checking on the number of arguments passed, I tried to just assign the first argument to the FileToBeParsed variable, and this errors out:

    $FileToBeParsed = $args[0]

    ================= Start Error ====================
    Unable to index into an object of type System.RuntimeType.
    At H:\Scripts\POWERSHELL\NewScript.ps1:88 char:25
    + $FileToBeParsed = $args[ <<<< 1]
        + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
        + FullyQualifiedErrorId : CannotIndex
    =================== End Error ====================

    If I cannot pass an input file to a PowerShell script at the cmd.exe window, how can I pass an input file without hardcoding the full path? Any help would be greatly appreciated.

    Chris

    Tuesday, July 20, 2010 7:20 PM

Answers

  • Hi,

    Try this:

    param($args1)
    write-host $args1
    
    $FileToBeParsed = $args1.tostring()
    
    

    You can now use the $filetobeparsed variable.

    IF you try get-content $filetobeparsed it will get the file contents.

    thanks

    Thiyagu

     

     


    Thiyagu | MCTS/MCITP - Exchange 2007 | MCSE 2003[Messaging] | http://www.myExchangeWorld.com. This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Thiyagu14 Wednesday, July 21, 2010 6:37 AM
    • Marked as answer by Mervyn Zhang Friday, July 23, 2010 11:04 AM
    Tuesday, July 20, 2010 8:36 PM
  • what thiyagu wrote was correct, if you create a PS1 file that contains only the following.

    ###

    param($args1)
    write-host $args1
    get-content $args1

    ####

    and call the file test.ps1 and then run this code in a console.

    ###

    set-content test.txt "hello world!"

    .\test.ps1 test.txt

    ##

    you'll see that the PS1 file will write the contents of the test.txt file to the console.

    Justin

    • Marked as answer by Mervyn Zhang Friday, July 23, 2010 11:04 AM
    Wednesday, July 21, 2010 11:39 PM

All replies

  • Hi,

    Try this:

    param($args1)
    write-host $args1
    
    $FileToBeParsed = $args1.tostring()
    
    

    You can now use the $filetobeparsed variable.

    IF you try get-content $filetobeparsed it will get the file contents.

    thanks

    Thiyagu

     

     


    Thiyagu | MCTS/MCITP - Exchange 2007 | MCSE 2003[Messaging] | http://www.myExchangeWorld.com. This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Thiyagu14 Wednesday, July 21, 2010 6:37 AM
    • Marked as answer by Mervyn Zhang Friday, July 23, 2010 11:04 AM
    Tuesday, July 20, 2010 8:36 PM
  • Thiyagu,

    I couldn't get your code to work, but ended up finding two code snippets to supply an input text file to a PowerShell script (so it doesn't have to be hardcoded in the script). Solution number one is what I was looking for and solution number two gives the script user a popup input box to supply the input file's whole path:

    #1
    #Get INPUT FILE via the command line
    function new-array {$args}
    $FileToBeParsed = $args[0]

    #2
    #Get INPUT FILE via an input box popup
    $MsgBox = new-object -comobject MSScriptControl.ScriptControl
    $MsgBox.language = "vbscript"
    $MsgBox.addcode("function getInput() getInput = inputbox(`"Please input for full path name " + 
        "for the input file.     Ex. C:\temp\folder\input_file.txt`",`"INPUT FILE: ENTER FULL PATH`") end function" )
    $FileToBeParsed = $MsgBox.eval("getInput")

    Chris

    Wednesday, July 21, 2010 10:45 PM
  • what thiyagu wrote was correct, if you create a PS1 file that contains only the following.

    ###

    param($args1)
    write-host $args1
    get-content $args1

    ####

    and call the file test.ps1 and then run this code in a console.

    ###

    set-content test.txt "hello world!"

    .\test.ps1 test.txt

    ##

    you'll see that the PS1 file will write the contents of the test.txt file to the console.

    Justin

    • Marked as answer by Mervyn Zhang Friday, July 23, 2010 11:04 AM
    Wednesday, July 21, 2010 11:39 PM