Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Powershell Arrays and Export-CSV

376 views
Skip to first unread message

Graham Gold (PGDS)

unread,
Jun 11, 2012, 9:01:19 AM6/11/12
to
Hi,
I'm fairly new to Powershell, and writing a script with various
functions to automate reconciliation of config management data.
I've already written a function to query SQL database to get current
data and writing a script to compare CSV files (which will become a
function when finished).

I've opted not to compare-object as it will only highlight sets in the
source file not in the target and vice versa, but doesn't flag
individual differences in values in a set.

The code I've written does what I want right up until I want to write
out the arrays containing the New entries, Deleted entries and Amended
entries to CSV files. I've tried e.g. $NewCIs | Select * | Export-CSV -
notypeinformation -path .\NewCIs.CSV however I don't get the values
from the array but instead get PSPath and other properties.

If I use the same syntax but pass in $f1 or $f2 (the source and target
file as read in by get-content) then it works.

The Arrays I created using $array1 = @() syntax and a comparison
of .GetType on that versus those created by Get-Content show they are
the same type so I don't understand the difference in behaviour.

CSV format in these test files is as below, but different reports will
have different column headers:-
id,Parent,parenttochild,Child,childtoparent
400240,Z196SYSC,hosts,LPA1,is hosted by
400241,Z196SYSD,hosts,LPA2,is hosted by
400242,Z196SYSC,hosts,LPA3,is hosted by
400246,Z196SYSE,hosts,LPA6,is hosted by

Code is as below:-

# compare the objects
$f1 = (get-content .\IBM_Relationships.csv)
$f2 = @(get-content .\GG.csv)

$tempnew = @()
foreach ($CI in $f2) {
if ($f1 -notcontains $CI) {
$tempnew += $CI}}

$tempdeleted = @()
foreach ($CI in $f1) {
if ($f2 -notcontains $CI) {
$tempdeleted += $CI}}

$CINew = @()
$CIOld = @()

foreach ($line in $tempnew) {
$ID = $line.split(",")[0]
foreach ($line2 in $tempdeleted) {
if ($line2.StartsWith($ID)) {
$CINew += ($line)
$CIOld += ($line2)
}
}
}

$NewCIs = @()
$NewCIs += (get-content .\GG.csv -totalcount 1)

$DeletedCIs = @()
$DeletedCIs += (get-content .\GG.csv -totalcount 1)

foreach ($line in $CINew) {
$ID = $line.split(",")[0]
foreach ($line2 in $tempnew) {
if (-not $line2.StartsWith($ID)) {
$NewCIs += $line2
}
}
}

foreach ($line in $CIOld) {
$ID = $line.split(",")[0]
foreach ($line2 in $tempdeleted) {
if (-not $line2.StartsWith($ID)) {
$DeletedCIs += $line2
}
}
}

$CINew = (get-content .\GG.csv -totalcount 1),$CINew
$CIOld = (get-content .\GG.csv -totalcount 1),$CIOld


write ("Found " + ($NewCIs.count -1) + " new CI(s)")
$NewCIs;write "`n"
write ("Found " + ($DeletedCIs.count -1) + " Deleted CI(s)")
$DeletedCIs;write "`n"
write ("Found " + ($CIOld.count -1) + " amended CI(s)")
write "The following CI(s) in the baseline: `n"
$CIOld;write "`n"
write "`n now have the following value(s) `n"
$CINew

#$NewCIs#.GetType()
#$f1#.GetType()

$NewCIs | select * | export-csv -notypeinformation -path NewCIs.csv
0 new messages