我会尽力向大家解释我的问题。 目前,我正在尝试获取有关我的域中计算机的序列号和制造商名称的一些信息。
以下是我目前正在使用的代码差距:
$computername = import-csv "C:\Daten\pc.csv" | select -ExpandProperty Computername
ForEach ($computer in $computername)
{
$Monitors = Get-WmiObject -ComputerName $computer -Namespace root\wmi -Class wmiMonitorID
$i = 1
$obj = new-object -type psobject
Foreach ($Monitor in $Monitors)
{
$obj | add-member -Name ("Monitor$i" +" Modell") -Value ([char[]]$Monitor.UserFriendlyName -join '') -MemberType NoteProperty -Force
$obj | add-member -Name ("Monitor$i" +" Serial") -Value ([char[]]$Monitor.SerialNumberID -join '') -MemberType NoteProperty -Force
$obj | add-member -Name ("MAC-Addresse") -value $mac -MemberType NoteProperty -Force
$obj | add-member -Name ("Seriennummer PC") -value $seriennummer_pc -MemberType NoteProperty -Force
$obj | add-member -Name ("Modell PC") -value $modell_pc -MemberType NoteProperty -Force
$obj | Add-Member -Name ("Computername") -value $computer -MemberType NoteProperty -Force
$i++
$obj | export-csv -path C:\Daten\test.csv
}}
我的问题现在是导出工作,但仅适用于我的imported-csv中的最后一行。我也已经使用Get-Content命令尝试了它。
这是exported-csv:
#TYPE System.Management.Automation.PSCustomObject
"Monitor1 Modell","Monitor1 Serial","Monitor2 Modell","Monitor2 Serial","MAC-Addresse","Seriennummer PC","Modell PC","Computername"
B22W-7 LED,"XYZ123459","B22W-7 LED","XYZ123457","00:00:00:00:00:00","XYZ123456","LIFE1337","COMPUTER2"
如果你可以帮助我的话会很棒。
提前感谢, 罗宾
答案 0 :(得分:0)
我看到的问题...... 在for循环中创建新对象。 在for循环之外导出为CSV。 把所有$ obj放在一个数组中,导出它。
可能的解决方案:
$computername = import-csv "C:\Daten\pc.csv" | select -ExpandProperty Computername
$items = @()
ForEach ($computer in $computername)
{
$Monitors = Get-WmiObject -ComputerName $computer -Namespace root\wmi -Class wmiMonitorID
$i = 1
Foreach ($Monitor in $Monitors)
{
$obj = new-object -type psobject
$obj | add-member -Name ("Monitor$i" +" Modell") -Value ([char[]]$Monitor.UserFriendlyName -join '') -MemberType NoteProperty -Force
$obj | add-member -Name ("Monitor$i" +" Serial") -Value ([char[]]$Monitor.SerialNumberID -join '') -MemberType NoteProperty -Force
$obj | add-member -Name ("MAC-Addresse") -value $mac -MemberType NoteProperty -Force
$obj | add-member -Name ("Seriennummer PC") -value $seriennummer_pc -MemberType NoteProperty -Force
$obj | add-member -Name ("Modell PC") -value $modell_pc -MemberType NoteProperty -Force
$obj | Add-Member -Name ("Computername") -value $computer -MemberType NoteProperty -Force
$i++
$items += $obj
}
}
$items | export-csv -path C:\Daten\test.csv
答案 1 :(得分:0)
使用以下测试代码,您可以更轻松地解决它。
try:
Left_position = (int(input("How many places would you like to move left")))
except ValueError:
print('Error is handled here')
if args:
....
答案 2 :(得分:0)
我认为您可以使用-Append
参数解决您的问题
因此出口将是这样的:
$obj | export-csv -path C:\Daten\test.csv -Append
这样您就不必将导出移出for循环。
答案 3 :(得分:0)
感谢目前为止的帮助。
我已将输出分成两个CSV。这对我来说更容易。
我现在遇到的问题是每台计算机只显示一台显示器,但每台计算机上至少连接了两台显示器......
请查看代码:
$items = @()
ForEach ($computer in $computername)
{
$Monitors = Get-WmiObject -ComputerName $computer -Namespace root\wmi -Class wmiMonitorID
$i = 1
Foreach ($Monitor in $Monitors)
{
$obj = new-object -type psobject
$obj | Add-Member -Name ("Computername") -value $computer -MemberType NoteProperty -Force
$obj | add-member -Name ("Monitor$i" +" Modell") -Value ([char[]]$Monitor.UserFriendlyName -join '') -MemberType NoteProperty -Force
$obj | add-member -Name ("Monitor$i" +" Serial") -Value ([char[]]$Monitor.SerialNumberID -join '') -MemberType NoteProperty -Force
$i++
$items += $obj
}
$obj2 = New-Object -type psobject
$obj2 | Add-Member -Name ("Computername") -value $computer -MemberType NoteProperty -Force
$obj2 | add-member -Name ("MAC-Addresse") -value $mac -MemberType NoteProperty -Force
$obj2 | add-member -Name ("Seriennummer PC") -value $seriennummer_pc -MemberType NoteProperty -Force
$obj2 | add-member -Name ("Modell PC") -value $modell_pc -MemberType NoteProperty -Force
$obj2 | add-member -Name ("IP Addresse") -value $ipv4 -MemberType NoteProperty -Force
}
$items | export-csv -path C:\Daten\Inventur\Bildschirm.csv -Append
$obj2 | export-csv -path C:\Daten\Inventur\Computer.csv -Append