Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable from batch to powershell

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

SET ThisScriptsDirectory=%~dp0

SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

Im wondering how i send powershell the variable 'asset' from my batch file.

Here is my .bat file content

@Echo off  
cls
Color E
cls

@echo Enter asset below
set /p asset=

@echo.
@echo your asset is %asset%
@echo.
goto startusmt

:startusmt
@echo.
@echo executing psexec ...
@echo.

SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%RemoteUSMT.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -file %PowerShellScriptPath% %asset%
psexec \\%asset% -u domain\username -p password cmd 

goto EOF

:EOF 
PAUSE
like image 234
rangerr Avatar asked Nov 01 '25 15:11

rangerr


1 Answers

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

like image 104
wmz Avatar answered Nov 04 '25 06:11

wmz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!