Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导入Csv Powershell_Powershell_Csv - Fatal编程技术网

导入Csv Powershell

导入Csv Powershell,powershell,csv,Powershell,Csv,代码: CSV文件(其中US-1是A1,US-2是A2,依此类推) 我认为导入Csv导入不正确,因为写入主机$Computers会给我以下信息: @{US-1=US-2}{US-1=US-3}{US-1=US-4} 但如果以这种方式分配$Computers: US-1 US-2 US-3 US-4 输出正确: US-1 US-2 US-3 US-4 因此,为了方便起见,我需要从CSV导入,但要以正确的方式保存每台计算机的名称,不使用括号或符号。这使得在我程序的其余部分中使用计算机名称非常困难

代码:

CSV文件(其中US-1是A1,US-2是A2,依此类推)

我认为导入Csv导入不正确,因为写入主机$Computers会给我以下信息:

@{US-1=US-2}{US-1=US-3}{US-1=US-4}

但如果以这种方式分配$Computers:

US-1
US-2
US-3
US-4
输出正确:

US-1 US-2 US-3 US-4

因此,为了方便起见,我需要从CSV导入,但要以正确的方式保存每台计算机的名称,不使用括号或符号。这使得在我程序的其余部分中使用计算机名称非常困难

编辑:如前所述,我已正确格式化csv,现在是:

$Computers = "US-1", "US-2", "US-3", "US-4"
现在获得输出:


@{Computers=US-1}{Computers=US-2}{Computers=US-3}{Computers=US-4}

您的CSV文件格式不正确;它没有标题行。重新生成文件,使其具有标题行,或使用
-header
参数来删除

一旦有了格式正确的CSV(或使用手动提供的标题导入),您将能够引用
$computers
作为PSObject数组,其中每个PSObject都包含一个具有提供名称的成员(例如,如果使用

Computers,
US-1
US-2
US-3
US-4
然后您可以将各个记录引用为
$Computers[$n].RegionName


在对原始问题进行编辑之后,访问数组中各个项目的正确方法是
$Computers[$n].Computers
。要从最初描述的文本文件检索计算机,而不需要使用字段名,请使用而不是
导入CSV

您的CSV文件格式不正确;它没有标题行。重新生成文件,使其具有标题行,或使用
-header
参数来删除

一旦有了格式正确的CSV(或使用手动提供的标题导入),您将能够引用
$computers
作为PSObject数组,其中每个PSObject都包含一个具有提供名称的成员(例如,如果使用

Computers,
US-1
US-2
US-3
US-4
然后您可以将各个记录引用为
$Computers[$n].RegionName


在对原始问题进行编辑之后,访问数组中各个项目的正确方法是
$Computers[$n].Computers
。要从最初描述的文本文件检索计算机,而不需要使用字段名,请使用而不是
导入CSV

简单CSV

创建您的CSV(computers.CSV):

导入您的CSV:

Computer
US-1
US-2
US-3
Computers, OperatingSystem
US-1, Windows
US-2, Linux
US-3, SomeOtherUnix
US-4, MacOS
访问您的值:

$computers = Import-Csv Computers.csv
$computers = Import-Csv Computers.csv
访问每个值:

Write-Host $computers # This results to something like this: @{Computers=US-1} @{Computers=US-2} @{Computers=US-3} @{Computers=US-4}
# This is perfectly fine. It's all of your computer-objects
Write-Host $computers # This results to something like this: 
# @{Computers=US-1; OperatingSystem=Windows} @{Computers=US-2; OperatingSystem=Linux} @{Computers=US-3; OperatingSystem=SomeOtherUnix} @{Computers=US-4; OperatingSystem=MacOS}
# This is perfectly fine. It's all of your computer-objects
如果你对电脑感兴趣,你可以做如下事情:

write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in US-2
write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in 

Computers OperatingSystem
--------- ---------------
US-1      Windows
对于更大的CSV也是如此:

write-host $computers.Computer # restults in US-1 US-2 US-3 US-4
导入您的CSV:

Computer
US-1
US-2
US-3
Computers, OperatingSystem
US-1, Windows
US-2, Linux
US-3, SomeOtherUnix
US-4, MacOS
访问您的值:

$computers = Import-Csv Computers.csv
$computers = Import-Csv Computers.csv
访问每个值:

Write-Host $computers # This results to something like this: @{Computers=US-1} @{Computers=US-2} @{Computers=US-3} @{Computers=US-4}
# This is perfectly fine. It's all of your computer-objects
Write-Host $computers # This results to something like this: 
# @{Computers=US-1; OperatingSystem=Windows} @{Computers=US-2; OperatingSystem=Linux} @{Computers=US-3; OperatingSystem=SomeOtherUnix} @{Computers=US-4; OperatingSystem=MacOS}
# This is perfectly fine. It's all of your computer-objects
如果只对计算机/操作系统感兴趣,您可以这样做:

write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in US-2
write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in 

Computers OperatingSystem
--------- ---------------
US-1      Windows

等等……:)

简单CSV

创建您的CSV(computers.CSV):

导入您的CSV:

Computer
US-1
US-2
US-3
Computers, OperatingSystem
US-1, Windows
US-2, Linux
US-3, SomeOtherUnix
US-4, MacOS
访问您的值:

$computers = Import-Csv Computers.csv
$computers = Import-Csv Computers.csv
访问每个值:

Write-Host $computers # This results to something like this: @{Computers=US-1} @{Computers=US-2} @{Computers=US-3} @{Computers=US-4}
# This is perfectly fine. It's all of your computer-objects
Write-Host $computers # This results to something like this: 
# @{Computers=US-1; OperatingSystem=Windows} @{Computers=US-2; OperatingSystem=Linux} @{Computers=US-3; OperatingSystem=SomeOtherUnix} @{Computers=US-4; OperatingSystem=MacOS}
# This is perfectly fine. It's all of your computer-objects
如果你对电脑感兴趣,你可以做如下事情:

write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in US-2
write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in 

Computers OperatingSystem
--------- ---------------
US-1      Windows
对于更大的CSV也是如此:

write-host $computers.Computer # restults in US-1 US-2 US-3 US-4
导入您的CSV:

Computer
US-1
US-2
US-3
Computers, OperatingSystem
US-1, Windows
US-2, Linux
US-3, SomeOtherUnix
US-4, MacOS
访问您的值:

$computers = Import-Csv Computers.csv
$computers = Import-Csv Computers.csv
访问每个值:

Write-Host $computers # This results to something like this: @{Computers=US-1} @{Computers=US-2} @{Computers=US-3} @{Computers=US-4}
# This is perfectly fine. It's all of your computer-objects
Write-Host $computers # This results to something like this: 
# @{Computers=US-1; OperatingSystem=Windows} @{Computers=US-2; OperatingSystem=Linux} @{Computers=US-3; OperatingSystem=SomeOtherUnix} @{Computers=US-4; OperatingSystem=MacOS}
# This is perfectly fine. It's all of your computer-objects
如果只对计算机/操作系统感兴趣,您可以这样做:

write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in US-2
write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in 

Computers OperatingSystem
--------- ---------------
US-1      Windows


等等……:)

奇怪的输出。$Computers是什么数据类型?默认数据类型,很可能是字符串,不确定。如果我为堆栈溢出创建的整个测试程序发布了CSV文件的示例,$Computers.gettype()的输出是什么?导入CSV将第一条记录解释为标题,而您希望它是数据。因此,它将csv数据解码为一个字段,名为US-1。见下面杰夫·泽特林的答案。奇怪的输出。$Computers是什么数据类型?默认数据类型,很可能是字符串,不确定。如果我为堆栈溢出创建的整个测试程序发布了CSV文件的示例,$Computers.gettype()的输出是什么?导入CSV将第一条记录解释为标题,而您希望它是数据。因此,它将csv数据解码为一个字段,名为US-1。请参见下面Jeff Zeitlin的回答。您好,我已经查阅了一个关于创建csv标题的指南,现在我的csv文件看起来是这样的:如果您已更改了csv,并且仍然存在问题,请更新您的问题以反映这一点-或者问一个新问题。是的,很抱歉,我在评论中点击了“输入新行”,它将其发布,我编辑了我的原始帖子,标题是“计算机”单数还是复数?@WalterMitty-您可以选择,但您需要使用标题中使用的任何一个来引用它。您的示例将标题设置为“computers”,因此您必须在生成的对象中引用该字段。您好,我已查阅了有关创建csv标题的指南,现在我的csv文件如下所示:如果您已更改csv,并且仍然存在问题,更新您的问题以反映这一点-或提出新问题。是的,对不起,我在评论中按enter键输入新行,它发布了它,我编辑了我的原始帖子标题“computer”单数还是复数?@WalterMitty-您可以选择,但您需要使用标题中使用的任何一个来引用它。您的示例使用了标题“computers”,因此您必须在结果对象中引用字段。comp是什么意思?另外,我以后会使用for-each循环,所以我不能引用单个计算机snvm,我想,我的脚本现在运行得很好,谢谢!啊,我的坏!我用$comp作为脚本变量,而不是$computers。为了可读性,我将编辑我的文章。comp是什么意思?另外,我以后会使用for-each循环,所以我不能引用单个计算机snvm,我想,我的脚本现在运行得很好,谢谢!啊,我的坏!我用$comp作为脚本变量,而不是$computers。为了可读性,我将编辑我的文章。