如何将Powershell变量传递给CMD命令? [英] How can I pass a powershell variable into a CMD command?

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

问题描述

我正在尝试通过Powershell运行以下命令:

I am trying to run the following command through powershell:

schtasks /query /s $compname /v /fo csv | ConvertFrom-Csv

$ compname在powershell脚本的第一部分中设置,但不能传递给这样的命令。例如,当我尝试传递$ localhost时,出现以下错误:

$compname is set in the first part of the powershell script, but cannot be passed into the command like this. For example, when I try passing in $localhost, I get the following error:

ERROR: Invalid syntax. Value expected for '/s'.
Type "SCHTASKS /QUERY /?" for usage.

是否可以将变量传递给此命令?我不打算更改为Powershell的本机方式(通过COM对象)访问计划任务。

Is there a way to pass the variable into this command? I don't plan to change to powershell's native way to access scheduled tasks (through a COM object).

推荐答案

为我工作,已在v2中进行了测试。如果将变量用双引号引起来(例如 $ compname),是否有帮助?

Working for me, tested in v2. Does it help if you enclose the variable in double quotes (e.g "$compname") ?

$compname = 'localhost'
schtasks /query /s $compname /v /fo csv | ConvertFrom-Csv

这篇关于如何将Powershell变量传递给CMD命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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