需要从命令行运行多个Powershell命令

时间:2017-09-23 12:13:33

标签: powershell cmd timespan

我需要像这样运行单行PowerShell脚本:

$a = Get-Content test.tmp; $b = [timespan]::fromseconds($a); "{0:HH:mm:ss,fff}" -f ([datetime]$b.Ticks)

在命令行中。直接从powerhsell cli运行时,它工作正常。但是在尝试跑步时:

powershell "$a = Get-Content test.tmp; $b = [timespan]::fromseconds($a); "{0:HH:mm:ss,fff}" -f ([datetime]$b.Ticks)"

从命令行cli我得到错误。我不能以.ps1文件的形式从脚本运行它,因为我不允许更改有关运行powershell脚本的限制策略。

Anybodoy能够指出我需要改变什么来从命令行正确运行它?

非常感谢

1 个答案:

答案 0 :(得分:0)

使用正确的引号/括号,您不需要中间变量,只需要一个命令:

'{0:HH:mm:ss,fff}' -f ([datetime]([timespan]::fromseconds((Get-Content test.tmp))).Ticks)

批量生产:

powershell -C "'{0:HH:mm:ss,fff}' -f ([datetime]([timespan]::fromseconds((Get-Content test.tmp))).Ticks)"

或者将持续时间输入批量变量:

:: Q:\Test\2017\09\23\SO_46379453.cmd
@Echo off
For /f "tokens=1*" %%A in (
    'powershell -C "\"{0:HH:mm:ss,fff}\" -f ([datetime]([timespan]::fromseconds((Get-Content test.tmp))).Ticks)" '
) Do Set "Duration=%%A,%%B"
set Duration
相关问题