Array Question Part 2

  • Follow


Some of this is extremely new to me.  Code below is info I have been piecing 
together.

From what I can determine the line below doesn't appear to be 
saving/appending my object
$myCollection += new-object PSObject -properties $myServerObject

If I use write-output on any of the select-strings commands it is properly 
updating $myServerObject with the expected value.  Does any one see what is 
wrong with this code?


$Servers = Import-CSV -Path $fileServers

# Create an array to store objects
$myCollection = @()

# Parse all the servers
foreach($Server in $Servers)
{
 $myServerObject = @{}
 $myServerObject.Name    = $Server.name
 if(Select-String -pattern $Server.name -path 
$filePatchlink){$myServerObject.Patch = "x"}
 if(Select-String -pattern $Server.name -path 
$fileSolarwinds){$myServerObject.Monitor = "x"}
 if(Select-String -pattern $Server.name -path 
$fileNBU){$myServerObject.Backup = "x"}
 if(Select-String -pattern $Server.name -path 
$fileHardware){$myServerObject.Physical= "x"}
 if(Select-String -pattern $Server.name -path 
$fileVMWare){$myServerObject.Virtual = "x"}
 if(Select-String -pattern $Server.name -path $fileTrend){$myServerObject.AV 
= "x"}
 if(Select-String -pattern $Server.name -path $fileAD){$myServerObject.AD = 
"x"}

$myCollection += new-object PSObject -properties $myServerObject

}

# Export to CSV
$myCollection | Export-Csv $fileProcessed


-- 
Paul Bergson
MVP - Directory Services
MCTS, MCT, MCSE, MCSA, Security+, BS CSci
2008, 2003, 2000 (Early Achiever), NT4
Microsoft's Thrive IT Pro of the Month - June 2009

http://www.pbbergs.com

Please no e-mails, any questions should be posted in the NewsGroup This
posting is provided "AS IS" with no warranties, and confers no rights.


0
Reply Paul 11/30/2009 9:59:08 PM

Hello Paul Bergson [MVP-DS],

> Some of this is extremely new to me.  Code below is info I have been
> piecing together.
> 
> From what I can determine the line below doesn't appear to be
> saving/appending my object
> $myCollection += new-object PSObject -properties $myServerObject
> If I use write-output on any of the select-strings commands it is
> properly updating $myServerObject with the expected value.  Does any
> one see what is wrong with this code?
> 
> $Servers = Import-CSV -Path $fileServers
> 
> # Create an array to store objects
> $myCollection = @()
> # Parse all the servers
> foreach($Server in $Servers)
> {
> $myServerObject = @{}
> $myServerObject.Name    = $Server.name
> if(Select-String -pattern $Server.name -path
> $filePatchlink){$myServerObject.Patch = "x"}
> if(Select-String -pattern $Server.name -path
> $fileSolarwinds){$myServerObject.Monitor = "x"}
> if(Select-String -pattern $Server.name -path
> $fileNBU){$myServerObject.Backup = "x"}
> if(Select-String -pattern $Server.name -path
> $fileHardware){$myServerObject.Physical= "x"}
> if(Select-String -pattern $Server.name -path
> $fileVMWare){$myServerObject.Virtual = "x"}
> if(Select-String -pattern $Server.name -path
> $fileTrend){$myServerObject.AV
> = "x"}
> if(Select-String -pattern $Server.name -path
> $fileAD){$myServerObject.AD =
> "x"}
> $myCollection += new-object PSObject -properties $myServerObject
> 
> }
> 
> # Export to CSV
> $myCollection | Export-Csv $fileProcessed
> http://www.pbbergs.com
> 
> Please no e-mails, any questions should be posted in the NewsGroup
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> 

Try:

$myCollection += new-object psobject -Property $myServerObject

Karl
http://unlockpowershell.wordpress.com/


0
Reply Karl 11/30/2009 10:25:59 PM


VGhlcmUgaXMgYSBidWcvdHlwbyBpbiB0aGlzIGxpbmU6DQoNCiRteUNvbGxlY3Rpb24gKz0gbmV3
LW9iamVjdCBQU09iamVjdCAtcHJvcGVydGllcyAkbXlTZXJ2ZXJPYmplY3QNCg0KIyB0aGUgcGFy
YW1ldGVyIG5hbWUgaXMgUHJvcGVydHkNCg0KJG15Q29sbGVjdGlvbiArPSBuZXctb2JqZWN0IFBT
T2JqZWN0IC1wcm9wZXJ0eSAkbXlTZXJ2ZXJPYmplY3QNCg0KLi4udGhlIG90aGVyIHByb2JsZW0g
SSBzZWUgcyB0aGF0IHlvdSdyZSBub3QgYWRkaW5nIHRoZSByZXN0IG9mIHRoZSBmaWVsZHMgdG8g
dGhlIFBTT2JqZWN0LCBldmVuIGlmIHRoZXkncmUgYmxhbmsuDQoNClRoaXMgZGVtb25zdHJhdGVz
IHRoZSBidWc6DQoNCiRhcnIgPSBAKCkNCiRodCA9IEB7fQ0KJGh0Lm5hbWUgPSAnSm9obicNCiRh
cnIgKz0gbmV3LW9iamVjdCBQU09iamVjdCAtUHJvcGVydHkgJGh0DQoNCiRodC5hZ2UgPSAyMw0K
JGFyciArPSBuZXctb2JqZWN0IFBTT2JqZWN0IC1Qcm9wZXJ0eSAkaHQNCiRodC5jb3VudHJ5ID0g
J1VTQScNCiRhcnIgKz0gbmV3LW9iamVjdCBQU09iamVjdCAtUHJvcGVydHkgJGh0DQokYXJyDQoN
ClRoaXMgd29ya3MgYXMgZXhwZWN0ZWQ6DQoNCiRhcnIgPSBAKCkNCiRodCA9IEB7fQ0KJGh0Lm5h
bWUgPSAnSm9obicNCiRodC5hZ2UgPSAnJw0KJGh0LmNvdW50cnkgPSAnJw0KJGFyciArPSBuZXct
b2JqZWN0IFBTT2JqZWN0IC1Qcm9wZXJ0eSAkaHQNCiRodC5uYW1lID0gJycNCiRodC5hZ2UgPSAy
Mw0KJGh0LmNvdW50cnkgPSAnJw0KJGFyciArPSBuZXctb2JqZWN0IFBTT2JqZWN0IC1Qcm9wZXJ0
eSAkaHQNCiRodC5uYW1lID0gJycNCiRodC5hZ2UgPSAnJw0KJGh0LmNvdW50cnkgPSAnVVNBJw0K
JGFyciArPSBuZXctb2JqZWN0IFBTT2JqZWN0IC1Qcm9wZXJ0eSAkaHQNCiRhcnINCg0KDQotLSAN
ClJvYmVydA==

0
Reply Robert 11/30/2009 10:36:52 PM

Yes that was it.  I also needed to prepopulate all fields otherwise the 
object creation didn't work correctly, as Robert pointed out.

Can I ask two followup questions?  The updated code is at the bottom of this 
new inquiry
1) Is there a way to order the rows on the csv output?  I had hoped to get 
the output with the name as the first column.

2) The following line is the first line listed in my csv output file.  Is 
there a way to prevent that from being placed in the csv file?
#TYPE System.Management.Automation.PSCustomObject





$Servers = Import-CSV -Path $fileServers

# Create an array to store objects
$myCollection = @()

# Parse all the servers
foreach($Server in $Servers)
{

 $myServerObject = @{}
 $myServerObject.Name     = $Server.name
    $myServerObject.Patch    = " "
    $myServerObject.Monitor  = " "
    $myServerObject.Backup   = " "
    $myServerObject.Physical = " "
    $myServerObject.Virtual  = " "
    $myServerObject.AV       = " "
    $myServerObject.AD       = " "
 if(Select-String -pattern $Server.name -path 
$filePatchlink){$myServerObject.Patch = "x"}
 if(Select-String -pattern $Server.name -path 
$fileSolarwinds){$myServerObject.Monitor = "x"}
 if(Select-String -pattern $Server.name -path 
$fileNBU){$myServerObject.Backup = "x"}
    if(Select-String -pattern $Server.name -path 
$fileHardware){$myServerObject.Physical= "x"}
 if(Select-String -pattern $Server.name -path 
$fileVMWare){$myServerObject.Virtual = "x"}
 if(Select-String -pattern $Server.name -path $fileTrend){$myServerObject.AV 
= "x"}
 if(Select-String -pattern $Server.name -path $fileAD){$myServerObject.AD = 
"x"}

    $myCollection += new-object PSObject -property $myServerObject

}

# Export to CSV
$myCollection | Export-Csv $fileProcessed


-- 
Paul Bergson
MVP - Directory Services
MCTS, MCT, MCSE, MCSA, Security+, BS CSci
2008, 2003, 2000 (Early Achiever), NT4
Microsoft's Thrive IT Pro of the Month - June 2009

http://www.pbbergs.com

Please no e-mails, any questions should be posted in the NewsGroup This
posting is provided "AS IS" with no warranties, and confers no rights.

"Karl Mitschke" <karlmitschke@somestate.gov> wrote in message 
news:d66cd4c2186bb8cc3fe88dca5c10@msnews.microsoft.com...
> Hello Paul Bergson [MVP-DS],
>
>> Some of this is extremely new to me.  Code below is info I have been
>> piecing together.
>>
>> From what I can determine the line below doesn't appear to be
>> saving/appending my object
>> $myCollection += new-object PSObject -properties $myServerObject
>> If I use write-output on any of the select-strings commands it is
>> properly updating $myServerObject with the expected value.  Does any
>> one see what is wrong with this code?
>>
>> $Servers = Import-CSV -Path $fileServers
>>
>> # Create an array to store objects
>> $myCollection = @()
>> # Parse all the servers
>> foreach($Server in $Servers)
>> {
>> $myServerObject = @{}
>> $myServerObject.Name    = $Server.name
>> if(Select-String -pattern $Server.name -path
>> $filePatchlink){$myServerObject.Patch = "x"}
>> if(Select-String -pattern $Server.name -path
>> $fileSolarwinds){$myServerObject.Monitor = "x"}
>> if(Select-String -pattern $Server.name -path
>> $fileNBU){$myServerObject.Backup = "x"}
>> if(Select-String -pattern $Server.name -path
>> $fileHardware){$myServerObject.Physical= "x"}
>> if(Select-String -pattern $Server.name -path
>> $fileVMWare){$myServerObject.Virtual = "x"}
>> if(Select-String -pattern $Server.name -path
>> $fileTrend){$myServerObject.AV
>> = "x"}
>> if(Select-String -pattern $Server.name -path
>> $fileAD){$myServerObject.AD =
>> "x"}
>> $myCollection += new-object PSObject -properties $myServerObject
>>
>> }
>>
>> # Export to CSV
>> $myCollection | Export-Csv $fileProcessed
>> http://www.pbbergs.com
>>
>> Please no e-mails, any questions should be posted in the NewsGroup
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>
> Try:
>
> $myCollection += new-object psobject -Property $myServerObject
>
> Karl
> http://unlockpowershell.wordpress.com/
>
> 


0
Reply Paul 12/1/2009 2:12:48 PM

Yes that was it.  I also needed to prepopulate all fields otherwise the 
object creation didn't work correctly, as you pointed out.

Can I ask two followup questions?  The updated code is at the bottom of this 
new inquiry
1) Is there a way to order the rows on the csv output?  I had hoped to get 
the output with the name as the first column.

2) The following line is the first line listed in my csv output file.  Is 
there a way to prevent that from being placed in the csv file?
#TYPE System.Management.Automation.PSCustomObject





$Servers = Import-CSV -Path $fileServers

# Create an array to store objects
$myCollection = @()

# Parse all the servers
foreach($Server in $Servers)
{

 $myServerObject = @{}
 $myServerObject.Name     = $Server.name
    $myServerObject.Patch    = " "
    $myServerObject.Monitor  = " "
    $myServerObject.Backup   = " "
    $myServerObject.Physical = " "
    $myServerObject.Virtual  = " "
    $myServerObject.AV       = " "
    $myServerObject.AD       = " "
 if(Select-String -pattern $Server.name -path 
$filePatchlink){$myServerObject.Patch = "x"}
 if(Select-String -pattern $Server.name -path 
$fileSolarwinds){$myServerObject.Monitor = "x"}
 if(Select-String -pattern $Server.name -path 
$fileNBU){$myServerObject.Backup = "x"}
    if(Select-String -pattern $Server.name -path 
$fileHardware){$myServerObject.Physical= "x"}
 if(Select-String -pattern $Server.name -path 
$fileVMWare){$myServerObject.Virtual = "x"}
 if(Select-String -pattern $Server.name -path $fileTrend){$myServerObject.AV 
= "x"}
 if(Select-String -pattern $Server.name -path $fileAD){$myServerObject.AD = 
"x"}

    $myCollection += new-object PSObject -property $myServerObject

}

# Export to CSV
$myCollection | Export-Csv $fileProcessed


-- 
Paul Bergson
MVP - Directory Services
MCTS, MCT, MCSE, MCSA, Security+, BS CSci
2008, 2003, 2000 (Early Achiever), NT4
Microsoft's Thrive IT Pro of the Month - June 2009

http://www.pbbergs.com

Please no e-mails, any questions should be posted in the NewsGroup This
posting is provided "AS IS" with no warranties, and confers no rights.

"Robert Robelo" <Kiron@HighPlainsDrifter.com> wrote in message 
news:u7Qeh2gcKHA.808@TK2MSFTNGP02.phx.gbl...
> There is a bug/typo in this line:
>
> $myCollection += new-object PSObject -properties $myServerObject
>
> # the parameter name is Property
>
> $myCollection += new-object PSObject -property $myServerObject
>
> ...the other problem I see s that you're not adding the rest of the fields 
> to the PSObject, even if they're blank.
>
> This demonstrates the bug:
>
> $arr = @()
> $ht = @{}
> $ht.name = 'John'
> $arr += new-object PSObject -Property $ht
>
> $ht.age = 23
> $arr += new-object PSObject -Property $ht
> $ht.country = 'USA'
> $arr += new-object PSObject -Property $ht
> $arr
>
> This works as expected:
>
> $arr = @()
> $ht = @{}
> $ht.name = 'John'
> $ht.age = ''
> $ht.country = ''
> $arr += new-object PSObject -Property $ht
> $ht.name = ''
> $ht.age = 23
> $ht.country = ''
> $arr += new-object PSObject -Property $ht
> $ht.name = ''
> $ht.age = ''
> $ht.country = 'USA'
> $arr += new-object PSObject -Property $ht
> $arr
>
>
> -- 
> Robert 


0
Reply Paul 12/1/2009 2:13:12 PM

MS4gUGlwZSB0aGUgY29sbGVjdGlvbiB0byBTZWxlY3QtT2JqZWN0IHRvIHNldCB0aGUgb3JkZXIg
b2YgdGhlIGZpZWxkcywgSGFzaHRhYmxlcyBkb24ndCBrZWVwIHRoZSBvcmRlciwgYmVmb3JlIGV4
cG9ydGluZy4NCjIuIFVzZSBFeHBvcnQtQ1NWJ3MgLU5vVHBlSW5mb3JtYXRpb24gc3dpdGNoICgt
TlRJKSBhcyBLYXJsIGFscmVhZHkgcG9pbnRlZCBvdXQuDQoNCiMgY2hhbmdlIHRoaXMgbGluZToN
CiRteUNvbGxlY3Rpb24gfCBFeHBvcnQtQ3N2ICRmaWxlUHJvY2Vzc2VkDQoNCiMgdG8uLi4NCiRt
eUNvbGxlY3Rpb24gfA0KIFNlbGVjdC1PYmplY3QgTmFtZSwgUGF0Y2gsIE1vbml0b3IsIEJhY2t1
cCwgUGh5c2ljYWwsIFZpcnR1YWwsIEFWLCBBRCB8DQogRXhwb3J0LUNzdiAkZmlsZVByb2Nlc3Nl
ZCAtTlRJDQoNCkkgd291bGQgc3VnZ2VzdCB0byB1c2UgU2VsZWN0LVN0cmluZydzIC1RdWlldCBz
d2l0Y2ggYWxzbywgdGhpcyByZXR1cm5zIGEgW0Jvb2xlYW5dIGluc3RlYWQgb2YgW01pY3Jvc29m
dC5Qb3dlclNoZWxsLkNvbW1hbmRzLk1hdGNoSW5mb10uDQoNCi0tIA0KUm9iZXJ0

0
Reply Robert 12/1/2009 4:06:13 PM

1.) there is a way....

$Hashtable.GetEnumerator() | Sort

Martin

"Robert Robelo" <Kiron@HighPlainsDrifter.com> wrote in message 
news:uy5z3AqcKHA.612@TK2MSFTNGP06.phx.gbl...
> 1. Pipe the collection to Select-Object to set the order of the fields, 
> Hashtables don't keep the order, before exporting.
> 2. Use Export-CSV's -NoTpeInformation switch (-NTI) as Karl already 
> pointed out.
>
> # change this line:
> $myCollection | Export-Csv $fileProcessed
>
> # to...
> $myCollection |
> Select-Object Name, Patch, Monitor, Backup, Physical, Virtual, AV, AD |
> Export-Csv $fileProcessed -NTI
>
> I would suggest to use Select-String's -Quiet switch also, this returns a 
> [Boolean] instead of [Microsoft.PowerShell.Commands.MatchInfo].
>
> -- 
> Robert 

0
Reply Martin 12/1/2009 5:35:25 PM

You can simplify the object creation like so:

$props = @"
Name=$Server.name
Patch = 
monitor=
Backup= 
Physical= 
Virtual = 
AV= 
AD=
"@

 $myServerObject = new-object psobjec -property (convertfrom-stringdata 
$props)

or:

$myServerObject = "" | select Name,patch,monitor,backup,physical,virtual,AV,AD
$myServerObject.name = $server.name



$myServerObject = @{}
"Paul Bergson [MVP-DS]" wrote:

> Yes that was it.  I also needed to prepopulate all fields otherwise the 
> object creation didn't work correctly, as Robert pointed out.
> 
> Can I ask two followup questions?  The updated code is at the bottom of this 
> new inquiry
> 1) Is there a way to order the rows on the csv output?  I had hoped to get 
> the output with the name as the first column.
> 
> 2) The following line is the first line listed in my csv output file.  Is 
> there a way to prevent that from being placed in the csv file?
> #TYPE System.Management.Automation.PSCustomObject
> 
> 
> 
> 
> 
> $Servers = Import-CSV -Path $fileServers
> 
> # Create an array to store objects
> $myCollection = @()
> 
> # Parse all the servers
> foreach($Server in $Servers)
> {
> 
>  $myServerObject = @{}
>  $myServerObject.Name     = $Server.name
>     $myServerObject.Patch    = " "
>     $myServerObject.Monitor  = " "
>     $myServerObject.Backup   = " "
>     $myServerObject.Physical = " "
>     $myServerObject.Virtual  = " "
>     $myServerObject.AV       = " "
>     $myServerObject.AD       = " "
>  if(Select-String -pattern $Server.name -path 
> $filePatchlink){$myServerObject.Patch = "x"}
>  if(Select-String -pattern $Server.name -path 
> $fileSolarwinds){$myServerObject.Monitor = "x"}
>  if(Select-String -pattern $Server.name -path 
> $fileNBU){$myServerObject.Backup = "x"}
>     if(Select-String -pattern $Server.name -path 
> $fileHardware){$myServerObject.Physical= "x"}
>  if(Select-String -pattern $Server.name -path 
> $fileVMWare){$myServerObject.Virtual = "x"}
>  if(Select-String -pattern $Server.name -path $fileTrend){$myServerObject.AV 
> = "x"}
>  if(Select-String -pattern $Server.name -path $fileAD){$myServerObject.AD = 
> "x"}
> 
>     $myCollection += new-object PSObject -property $myServerObject
> 
> }
> 
> # Export to CSV
> $myCollection | Export-Csv $fileProcessed
> 
> 
> -- 
> Paul Bergson
> MVP - Directory Services
> MCTS, MCT, MCSE, MCSA, Security+, BS CSci
> 2008, 2003, 2000 (Early Achiever), NT4
> Microsoft's Thrive IT Pro of the Month - June 2009
> 
> http://www.pbbergs.com
> 
> Please no e-mails, any questions should be posted in the NewsGroup This
> posting is provided "AS IS" with no warranties, and confers no rights.
> 
> "Karl Mitschke" <karlmitschke@somestate.gov> wrote in message 
> news:d66cd4c2186bb8cc3fe88dca5c10@msnews.microsoft.com...
> > Hello Paul Bergson [MVP-DS],
> >
> >> Some of this is extremely new to me.  Code below is info I have been
> >> piecing together.
> >>
> >> From what I can determine the line below doesn't appear to be
> >> saving/appending my object
> >> $myCollection += new-object PSObject -properties $myServerObject
> >> If I use write-output on any of the select-strings commands it is
> >> properly updating $myServerObject with the expected value.  Does any
> >> one see what is wrong with this code?
> >>
> >> $Servers = Import-CSV -Path $fileServers
> >>
> >> # Create an array to store objects
> >> $myCollection = @()
> >> # Parse all the servers
> >> foreach($Server in $Servers)
> >> {
> >> $myServerObject = @{}
> >> $myServerObject.Name    = $Server.name
> >> if(Select-String -pattern $Server.name -path
> >> $filePatchlink){$myServerObject.Patch = "x"}
> >> if(Select-String -pattern $Server.name -path
> >> $fileSolarwinds){$myServerObject.Monitor = "x"}
> >> if(Select-String -pattern $Server.name -path
> >> $fileNBU){$myServerObject.Backup = "x"}
> >> if(Select-String -pattern $Server.name -path
> >> $fileHardware){$myServerObject.Physical= "x"}
> >> if(Select-String -pattern $Server.name -path
> >> $fileVMWare){$myServerObject.Virtual = "x"}
> >> if(Select-String -pattern $Server.name -path
> >> $fileTrend){$myServerObject.AV
> >> = "x"}
> >> if(Select-String -pattern $Server.name -path
> >> $fileAD){$myServerObject.AD =
> >> "x"}
> >> $myCollection += new-object PSObject -properties $myServerObject
> >>
> >> }
> >>
> >> # Export to CSV
> >> $myCollection | Export-Csv $fileProcessed
> >> http://www.pbbergs.com
> >>
> >> Please no e-mails, any questions should be posted in the NewsGroup
> >> This posting is provided "AS IS" with no warranties, and confers no
> >> rights.
> >>
> >
> > Try:
> >
> > $myCollection += new-object psobject -Property $myServerObject
> >
> > Karl
> > http://unlockpowershell.wordpress.com/
> >
> > 
> 
> 
> .
> 
0
Reply Utf 12/1/2009 6:44:01 PM

Got it all working.  Thx!!!

-- 
Paul Bergson
MVP - Directory Services
MCTS, MCT, MCSE, MCSA, Security+, BS CSci
2008, 2003, 2000 (Early Achiever), NT4
Microsoft's Thrive IT Pro of the Month - June 2009

http://www.pbbergs.com

Please no e-mails, any questions should be posted in the NewsGroup This
posting is provided "AS IS" with no warranties, and confers no rights.

"Robert Robelo" <Kiron@HighPlainsDrifter.com> wrote in message 
news:uy5z3AqcKHA.612@TK2MSFTNGP06.phx.gbl...
> 1. Pipe the collection to Select-Object to set the order of the fields, 
> Hashtables don't keep the order, before exporting.
> 2. Use Export-CSV's -NoTpeInformation switch (-NTI) as Karl already 
> pointed out.
>
> # change this line:
> $myCollection | Export-Csv $fileProcessed
>
> # to...
> $myCollection |
> Select-Object Name, Patch, Monitor, Backup, Physical, Virtual, AV, AD |
> Export-Csv $fileProcessed -NTI
>
> I would suggest to use Select-String's -Quiet switch also, this returns a 
> [Boolean] instead of [Microsoft.PowerShell.Commands.MatchInfo].
>
> -- 
> Robert 


0
Reply Paul 12/2/2009 1:08:30 PM

8 Replies
300 Views

(page loaded in 0.144 seconds)

9/3/2012 8:54:13 AM


Reply: