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

查看:39
本文介绍了输出文件时将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 \\Zapp\pc\Hash_Exports /copyall

这会输出一个名为"Autopilot_CSV.csv"的CSV文件.到 C:\ Autopilot_Export 目录,然后Robocopy将其复制到上面列出的Hash_Exports文件夹内的Network Share驱动器中.在上面的代码中,如果我手动键入"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.

这是我要使用的代码:

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:\Users\danieln\Desktop /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 ,但这没有似乎也不起作用.根据文档,环境变量显然已经是字符串.

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:\users\me\foo


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:\Users\me\foo


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

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

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