Slyons
Slyons

Reputation: 117

Issue with export-csv no data?

In my report when I remove the export-csv portion the data is presented correctly onscreen, when I add in the export-csv no data is exported but the file is created. Below is my script along with what the data looks like when script is running (modified) of what the data looks like.

#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"
   }
  }

What the data looks like when exported without 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

Update: Tried that and still nothing, when I run a modified version of the script locally on my DH servers it functions correctly, but I'm looking at almost 100 DH servers in my environment, see below

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

Upvotes: 0

Views: 4143

Answers (2)

Harsh Jaswal
Harsh Jaswal

Reputation: 447

According to your code you are using Export-Csv within a foreach-object. Generally Export-csv creates a csv file with new data. If data is already present in the csv file then it will overwrite the existing data with new one. So instead of using

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

You cand use

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

Here -Append will append the data in the csv file. Not overwrites.

Upvotes: 0

Sid
Sid

Reputation: 2676

This is a representation of what you are doing:

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

In doing so you are exporting data [(Total Servers) x (Total Scopes per server)] times which is a lot of I/O operations. It is just a logistics issue. You could collect the entire information into a table object before exporting. But that decision is up to you and your particular business needs.

The real issue, however, is that you are doing the export into the same file which essentially over writes whatever you have written before without telling it not to. So only the last export remains which I suspect is somehow blank.

Try using the -Append switch when you export.

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

Also I noticed you are using $server variable in the select-object cmdlet where you should only be using the object's property names and not variable names. I do not know if that returns anything as the cmdlet would not know what to do with it, which could also be contributing to the problem.

Upvotes: 1

Related Questions