2017-09-23 143 views
0

运行我需要运行单行PowerShell脚本像这样的:多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

'powershell -Command“$ a = Get-Content test.tmp; $ b = [timespan] :: fromseconds($ a); \“{0:HH:mm:ss,fff} \”-f([datetime] $ b.Ticks)“' – PetSerAl

回答

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