问题与export-csv没有数据?

在我的报告中,当我删除export-csv部分时,数据在屏幕上正确显示,当我添加export-csv时,没有数据被导出,但文件被创build。 下面是我的脚本以及脚本运行(修改)数据外观时的数据。

#Get all DHCP Servers $ServerList = (Get-Content -Path "\\termserv\DHCPServers.txt") foreach ($server in $serverlist) { #Get the scopes from each serve Get-DHCPServerv4Scope -ComputerName $server | select ScopeID | #Get the lease information for each scope ForEach-Object {Get-DHCPServerv4Lease -ScopeId $_.ScopeId -ComputerName $server -AllLeases | where {$_.AddressState -like "*Reservation"} | Select-Object $server,ScopeId,IPAddress,HostName,ClientID,AddressState | Export- Csv "\\termserv\d$\term\User\Reservations1.csv" } } 

在没有export-csv的情况下导出数据的样子

NOPEDH01:ScopeId:000.11.2.3 IPAddress:111.22.3.444 HostName:NOPE00112233 ClientID:00-11-22-33-44-55 AddressState:ActiveReservation

NOPEDH01:ScopeId:000.11.2.3 IPAddress:111.22.3.445 HostName:NOPE0011223344 ClientID:00-11-22-33-44-56 AddressState:ActiveReservation

更新:尝试,但仍然没有什么,当我在我的DH服务器本地运行脚本的修改版本,它的function正常,但我看我的环境中近100 DH服务器,请参阅下面

 Get-DHCPServerV4Scope | ForEach { Get-DHCPServerv4Lease -ScopeID $_.ScopeID -AllLeases | where {$_.AddressState -like '*Reservation'} } | Select-Object ScopeId,IPAddress,HostName,ClientID,AddressState | Export- Csv "\\termserv\d$\term\$($env:COMPUTERNAME)-Reservations1.csv" - NoTypeInformation 

根据你的代码,你在一个foreach-object中使用Export-Csv 。 通常, Export-csv会用新数据创build一个csv文件。 如果数据已经存在于csv文件中,那么它将用新的数据覆盖现有的数据。 所以,而不是使用

 Export-Csv "\\termserv\d$\term\User\Reservations1.csv" 

你使用

 Export-Csv "\\termserv\d$\term\User\Reservations1.csv" -Force -Append -NoTypeInformation 

这里-Append将把数据追加到csv文件中。 不会覆盖。

这是你在做什么的表示:

 Foreach ($Server in $ServerList) { Foreach ($Scope in $ScopeList) { $Data | Export-csv -Path FileName.csv } } 

这样做时,您正在导出数据[(Total Servers) x (Total Scopes per server)]次,这是很多I / O操作。 这只是一个后勤问题。 在导出之前,您可以将整个信息收集到表格对象中。 但是,这个决定取决于您和您的特定业务需求。

然而,真正的问题是,你正在做的导出到相同的文件,基本上写在你之前写的任何东西,而不会告诉它不要。 所以只有最后的出口仍然是我怀疑是空白的。

在导出时请尝试使用-Append开关。

 $Data | Export-csv -Path FileName.csv -NoTypeInformation -Append 

另外我注意到你在select-object cmdlet中使用$servervariables,你应该只使用对象的属性名而不是variables名。 我不知道是否返回任何内容,因为cmdlet不知道如何处理它,这也可能导致问题。