locked
Trying to pass variables into Powershell script RRS feed

  • Question

  • Hi,

    I have a simple powershell script

    $Filename = "c:\temp\book5.xlsx"
    $outputpath = "c:\temp"
    ConvertFrom-ExcelSheet -Path $Filename -Delimiter "," -OutputPath $path

    I am wanting to pass the script the two variables $Filename and $Outputpath when i run the script from the command line or a vb script.

    How would I go about doing that?

    Thanks

    Thursday, May 3, 2018 10:21 AM

All replies

  • Add a "Param" statement at the beginning of the script.

    Start by actually learning basic PowerShell.

    Learn PowerShell  


    \_(ツ)_/

    Thursday, May 3, 2018 10:38 AM
  • you have to declare your variables as parameters and use named arguments when calling the script.

    param($inputFile)
    param($outputDir)
    ......

    powershell.exe -file script.ps1 -inputFile iuhweidh.xls -outputDir c:\temp


    Gleb.

    Thursday, May 3, 2018 10:39 AM
  • you have to declare your variables as parameters and use named arguments when calling the script.

    param($inputFile)
    param($outputDir)
    ......

    powershell.exe -file script.ps1 -inputFile iuhweidh.xls -outputDir c:\temp


    Gleb.

    This is not how a Pram statement works.  Please refere to the PS documentation for correct instructions.

    help about_Scripts


    \_(ツ)_/

    Thursday, May 3, 2018 10:42 AM
  • Trying to learn and work at the same time...

    I have changed it to 

    Param(
      [string]$Filename,
      [string]$outputpath
    )
    Write-Host $Filename
    Write-Host $outputpath
    ConvertFrom-ExcelSheet -Path $Filename -Delimiter "," -OutputPath $path

    Then tried to call it with 

    powershell c:\temp\myscript.ps1 $Filename = "C:\temp\Book5.xlsx" $outputpath - "C:\temp

    A box pops up with red writing then disappears.  The code before passing worked, so it seems like it is not passing the variables in correctly.

    Thursday, May 3, 2018 10:50 AM
  • Please read the help I posted very carefully.

    We cannot teach you PowerShell.  Review the tutorial I posted a link to. It will teach you the very fist basic things about PowerShell that you need to know before you try to write a script.


    \_(ツ)_/

    Thursday, May 3, 2018 10:53 AM
  • Will have a look at it. Thanks
    Thursday, May 3, 2018 10:56 AM