且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

输出文件时使用Powershell环境变量作为字符串

更新时间:2022-03-09 09:42:53

双引号似乎有效.冒号在 powershell 参数中很特殊.

Doublequoting seems to work. The colon is special in powershell parameters.

echo hi | set-content "$env:computername.csv"

dir


    Directory: C:usersmefoo


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2/11/2021   1:02 PM              4 COMP001.csv

冒号是特殊的.例如在开关参数中:

The colon is special. For example in switch parameters:

get-childitem -force:$true

实际上,它正在尝试查找名为 csv 的属性:

Actually, it's trying to find a property named csv:

echo hi | Set-Content  $env:COMPUTERNAME.length
dir


    Directory: C:Usersmefoo


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2/11/2021   3:04 PM              4 8