输出文件时使用Powershell环境变量作为字符串 [英] Using Powershell environmental variables as strings when outputting files

查看:34
本文介绍了输出文件时使用Powershell环境变量作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Get-WindowsAutopilotInfo 来生成计算机的序列号和一个哈希码并将该信息导出为 CSV.

I am using Get-WindowsAutopilotInfo to generate a computer's serial number and a hash code and export that info as a CSV.

这是我通常使用的代码:

Here is the code I usually use:

new-item "c:Autopilot_Export" -type directory -force

Set-Location "C:Autopilot_Export"

Get-WindowsAutopilotInfo.ps1 -OutputFile Autopilot_CSV.csv

Robocopy C:Autopilot_Export \ZapppcHash_Exports /copyall

这会输出一个名为Autopilot_CSV.csv"的 CSV 文件;到 C:Autopilot_Export 目录,然后 Robocopy 将其复制到上面列出的 Hash_Exports 文件夹内的网络共享驱动器.在上面的代码中,如果我手动输入test"、123"、ABC"等并替换Autopilot_CSV"它也会在所有这些名称下输出一个 CSV.所以看起来 Get-WindowsAutopilotInfo 将创建一个 CSV 文件并保存该文件用你传递给它的任何字符串命名.太好了.

This outputs a CSV file named "Autopilot_CSV.csv" to the C:Autopilot_Export directory and then Robocopy copies it to the Network Share drive inside of the Hash_Exports folder listed above. In the above code, if I manually type in "test", "123", "ABC", etc. and replace "Autopilot_CSV" it will output a CSV under all of those names as well. So it looks like Get-WindowsAutopilotInfo will create a CSV file and save the file name with whatever string you pass into it. Great.

但是,我在多台不同的计算机上运行这个脚本,我希望导出的 CSV 被命名为它运行的机器所独有的名称,这样我就可以在复制后轻松识别它.我试图将环境变量 $env:computername 的值作为 CSV 的字符串输入传递,但它不起作用.

However, I am running this script on multiple different computers and I would like the exported CSV to be named something unique to the machine it is running on so I can easily identify it once it's copied. I am trying to pass the value of the environmental variable $env:computername as a string input for the CSV and it isn't working.

这是我尝试使用的代码:

Here's the code I'm trying to use:

new-item "c:Autopilot_Export" -type directory -force

Set-Location "C:Autopilot_Export"

Get-WindowsAutopilotInfo.ps1 -OutputFile $env:computername.csv

Robocopy C:Autopilot_Export C:UsersdanielnDesktop /copyall

此代码根本不会生成 CSV 文件.为什么不呢?

This code does not generate the CSV file at all. Why not?

我是否只是对 Powershell 中如何使用环境变量有基本的误解?$env:computername 似乎返回了计算机名称的字符串,但我无法将其传递到 Get-WindowsAutopilotInfo 并保存它,但是如果我手动键入它工作一个字符串作为输入.

Do I just have a basic misunderstanding of how environmental variables are used in Powershell? $env:computername appears to return a string of the computer's name, but I cannot pass it into Get-WindowsAutopilotInfo and have it save, but it will work if I manually type a string in as the input.

我也试过将它设置为一个变量 $computername = [string]$env:computername 并且只是在 .CSV 之前传递 $computername 而这并没有t 似乎工作.根据文档,环境变量显然已经是字符串了.

I have also tried setting it to a variable $computername = [string]$env:computername and just passing $computername in before the .CSV and that doesn't appear to work either. And per the docmentation, environmental variables are apparently already strings.

我错过了什么吗?

谢谢!

推荐答案

双引号似乎有效.冒号在 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

这篇关于输出文件时使用Powershell环境变量作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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