[英]powershell command into batch - it doesn't work
我想批量運行 powershell 命令。 我的 powershell 命令:
gc 'C:\Users\Test\Test-01\source.csv' | %{
$cols = $_.split(";")
1..$cols[-2]| %{
"$($cols[0..9] -join ';');$($cols[(9+$_)] -join ';');$($cols[(24+$_)] -join ';');;;;;;;;;;;;;;;$($cols[-2]);"
}
} | sc "C:\Users\Test\Test-01\target.csv"
我的批處理命令:
powershell -Executionpolicy ByPass gc 'C:\Users\Test\Test-01\source.csv' | %{
$cols = $_.split(";")
1..$cols[-2]| %{
"$($cols[0..9] -join ';');$($cols[(9+$_)] -join ';');$($cols[(24+$_)] -join ';');;;;;;;;;;;;;;;$($cols[-2]);"
}
} | sc "C:\Users\Test\Test-01\target.csv"
它不起作用。 但為什么? 有人可以說為什么嗎?
要將 PowerShell 代碼嵌入到 .BATch 文件中,您只需要注意一些細節:
-ExecutionPolicy
和-Command
東西。 只需在powershell
命令后編寫純 powershell 代碼即可。^
;
\"like this\"
並且這些修改是必需的,因為批處理文件特殊字符:
%%
| > < &
| > < &
帶脫字符號,像這樣: ^| ^> ^< ^&
^| ^> ^< ^&
在您的情況下,這是最終的工作代碼:
powershell ^
gc 'C:\Users\Test\Test-01\source.csv' ^| %%{ ^
$cols = $_.split(\";\"); ^
1..$cols[-2]^| %%{ ^
\"$($cols[0..9] -join ';');$($cols[(9+$_)] -join ';');$($cols[(24+$_)] -join ';');;;;;;;;;;;;;;;$($cols[-2]);\" ^
} ^
} ^| sc \"C:\Users\Test\Test-01\target.csv\"
您也可以查看這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.