Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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
Windows 导出CSV-(Powershell)_Windows_Csv_Powershell_Automation - Fatal编程技术网

Windows 导出CSV-(Powershell)

Windows 导出CSV-(Powershell),windows,csv,powershell,automation,Windows,Csv,Powershell,Automation,下面是脚本块: Import-csv C:\file_location.csv | foreach-object { Get-WmiObject -computername $_.computername -class Win32_ComputerSystem | select username} | ` Select-Object computername, username | Export-CSV C:\file_location.csv -notypeinformation 导出的cs

下面是脚本块:

Import-csv C:\file_location.csv | foreach-object {
Get-WmiObject -computername $_.computername -class Win32_ComputerSystem | select username} | `
Select-Object computername, username | Export-CSV C:\file_location.csv -notypeinformation
导出的csv显示计算机名标题,但没有实际的计算机和用户名标题。我在哪里错过了什么

谢谢大家!

select
(它是
select Object
的别名)返回一个仅包含指定属性的对象。所以当你第一次选择用户名时,你得到了一个只有用户名的对象;所有其他属性都被丢弃,因此当第二个
Select对象
调用运行时,它不会返回
computername

第一个
select
在那里似乎完全没有必要;只要把它拿出来,我想一切都会像预期的那样工作

编辑:我现在看到
computername
不是返回的WMI对象的属性;它来自CSV。您的
ForEach对象
仅返回WMI对象,因此将丢弃CSV行对象

您需要做的是将CSV中的
computername
添加到WMI对象,您可以使用
Select object
(带有计算列)或
add Member

Import-csv C:\file_location.csv | 
    ForEach-Object {
        Get-WmiObject -computername $_.computername -class Win32_ComputerSystem |
        Select-Object @{Name='ComputerName';Expression={$_.computername}},username
    } | 
    Export-CSV C:\file_location.csv -notypeinformation
或:

select
(它是
select Object
的别名)返回仅具有指定属性的对象。所以当你第一次选择用户名时,你得到了一个只有用户名的对象;所有其他属性都被丢弃,因此当第二个
Select对象
调用运行时,它不会返回
computername

第一个
select
在那里似乎完全没有必要;只要把它拿出来,我想一切都会像预期的那样工作

编辑:我现在看到
computername
不是返回的WMI对象的属性;它来自CSV。您的
ForEach对象
仅返回WMI对象,因此将丢弃CSV行对象

您需要做的是将CSV中的
computername
添加到WMI对象,您可以使用
Select object
(带有计算列)或
add Member

Import-csv C:\file_location.csv | 
    ForEach-Object {
        Get-WmiObject -computername $_.computername -class Win32_ComputerSystem |
        Select-Object @{Name='ComputerName';Expression={$_.computername}},username
    } | 
    Export-CSV C:\file_location.csv -notypeinformation
或:


谢谢你的回复!我尝试了两种方法,但仍然是一样的。选择对象方法提供相同的结果,即列中没有数据的ComputerName标头。添加成员导出一个空白CSV。请注意!!我发现了我的错误。Add成员工作得很好。谢谢@briantist!!谢谢你的回复!我尝试了两种方法,但仍然是一样的。选择对象方法提供相同的结果,即列中没有数据的ComputerName标头。添加成员导出一个空白CSV。请注意!!我发现了我的错误。Add成员工作得很好。谢谢@briantist!!可能的重复可能的重复