Pass variable from batch to powershell

后端 未结 3 1382
北荒
北荒 2021-01-16 05:20

I have a batch file that ask the user for a variable line set /p asset=. Im calling my powershell script like this

SET ThisScripts

相关标签:
3条回答
  • 2021-01-16 05:54

    If you have a file.ps1 that takes a parameter,

    param($asset)
    "The asset tag is $asset"
    

    You can pass in the variable as an argument.

    SET ThisScriptsDirectory=%~dp0
    SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps1
    SET /p asset=Enter the asset name
    
    PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" "%asset%"
    
    0 讨论(0)
  • 2021-01-16 06:08
    param($asset)
    

    This has to be the very first line in the PowerShell script for it to work, else it will fail.

    0 讨论(0)
  • 2021-01-16 06:10

    You can use $env:variable_name syntax to access curent cmd environment variables from powershell. To get hold of your variable you'd use $env:asset
    To try, open cmd, do set "myvar=my value", start powershell, do $env:myvar (this will simply print it, but of course you can use it as any other ps variable)

    Just as a sidenote, ps has good help system. If you do help env it will list two relevant topics which you can examine in turn to get detailed information.

    Hope this helps

    0 讨论(0)
提交回复
热议问题