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

查看:43
本文介绍了输出文件时使用 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.

下面是我常用的代码:

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天全站免登陆