将变量从批处理传递到Powershell [英] Pass variable from batch to powershell

查看:87
本文介绍了将变量从批处理传递到Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,要求用户输入可变行set /p asset=.我这样称呼我的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%'";

我想知道如何从批处理文件中向powershell发送变量'asset'.

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

这是我的.bat文件内容

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

推荐答案

如果您的file.ps1带有参数,

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%"

这篇关于将变量从批处理传递到Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆