Windows Vista Forums

Need help with Array and Export-CSV

  1. #1


    jsimpson2000 Guest

    Need help with Array and Export-CSV

    Hi all,

    I am a system admin and I am a new to PowerShell and scripting in
    general but I am getting to know it pretty quick. I have been
    messing with a piece of code to get a list of all the computers is the
    AD, then check those names and return the current logged on user.
    here is the code I have so far:

    #gets all computers in AD using the Quest AD cmdlet
    $comp = get-qadcomputer | sort-object

    #counts the total computers in AD
    $count = ($comp | measure-object).count

    #creates a 2 dimensional array
    $table = new-object "string[,]" $count,3

    #counting variable
    $inc = 0



    #screen display
    "AD Name, Network Name, User Name"

    #Foreach loop to check logged in users in domain.
    #Displays results on screen and adds results to the array
    foreach ($i in $comp)`
    {
    $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    $I.name + ", " + $CS.Name + ", " + $CS.UserName

    $table[$inc,0] = $I.name
    $table[$inc,1] = $CS.Name
    $table[$inc,2] = $CS.UserName

    $inc++
    }


    So I have two problems: First, When I display the array to the screen,
    is comes out like this -

    ComputerName 1
    ComputerName 1
    UserName 1
    ComputerName 2
    ComputerName 2
    UserName 2
    ComputerName 3
    ComputerName 3
    UserName 3

    Instead of like this -

    ComputerName 1 ComputerName 1 UserName 1
    ComputerName 2 ComputerName 2 UserName 2
    ComputerName 3 ComputerName 3 UserName 3

    Also, I can not figure out how to pass this to Export-CSV. when I do I
    get -

    Length
    0
    0
    0
    0
    0
    0
    12
    12
    11
    14
    18
    15
    9
    10

    Hope someone can help.

    Thanks
    Jim


      My System SpecsSystem Spec

  2. #2


    Brandon Shell Guest

    Re: Need help with Array and Export-CSV

    Maybe a better way to to do it is to create a custom object
    http://bsonposh.com/modules/wordpress/?p=25

    Everything I added has ############ around it

    #gets all computers in AD using the Quest AD cmdlet
    $comp = get-qadcomputer | sort-object

    ##################
    #Create a generic Collection Obj
    $myCollection = @()
    ##################

    #counts the total computers in AD
    $count = ($comp | measure-object).count

    #creates a 2 dimensional array
    $table = new-object "string[,]" $count,3

    #screen display
    "AD Name, Network Name, User Name"

    #Foreach loop to check logged in users in domain.
    #Displays results on screen and adds results to the array
    foreach ($i in $comp)`
    {
    $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    $I.name + ", " + $CS.Name + ", " + $CS.UserName

    #######################################
    # Create Custom Object
    $myobj = "" | select ADName,NetworkName,User
    #######################################

    $table[$inc,0] = $I.name
    $table[$inc,1] = $CS.Name
    $table[$inc,2] = $CS.UserName

    ##################
    $myobj.ADName = $I.Name
    $myobj.NetworkName = $CS.Name
    $myobj.User = $CS.UserName
    $myCollection += $myobj
    ##################
    }

    #################
    $myColleciton | Export-Csv C:\testfile.csv
    #################

    <jsimpson2000@xxxxxx> wrote in message
    news:1188503042.238763.257570@xxxxxx

    > Hi all,
    >
    > I am a system admin and I am a new to PowerShell and scripting in
    > general but I am getting to know it pretty quick. I have been
    > messing with a piece of code to get a list of all the computers is the
    > AD, then check those names and return the current logged on user.
    > here is the code I have so far:
    >
    > #gets all computers in AD using the Quest AD cmdlet
    > $comp = get-qadcomputer | sort-object
    >
    > #counts the total computers in AD
    > $count = ($comp | measure-object).count
    >
    > #creates a 2 dimensional array
    > $table = new-object "string[,]" $count,3
    >
    > #counting variable
    > $inc = 0
    >
    > #screen display
    > "AD Name, Network Name, User Name"
    >
    > #Foreach loop to check logged in users in domain.
    > #Displays results on screen and adds results to the array
    > foreach ($i in $comp)`
    > {
    > $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >
    > $table[$inc,0] = $I.name
    > $table[$inc,1] = $CS.Name
    > $table[$inc,2] = $CS.UserName
    >
    > $inc++
    > }
    >
    >
    > So I have two problems: First, When I display the array to the screen,
    > is comes out like this -
    >
    > ComputerName 1
    > ComputerName 1
    > UserName 1
    > ComputerName 2
    > ComputerName 2
    > UserName 2
    > ComputerName 3
    > ComputerName 3
    > UserName 3
    >
    > Instead of like this -
    >
    > ComputerName 1 ComputerName 1 UserName 1
    > ComputerName 2 ComputerName 2 UserName 2
    > ComputerName 3 ComputerName 3 UserName 3
    >
    > Also, I can not figure out how to pass this to Export-CSV. when I do I
    > get -
    >
    > Length
    > 0
    > 0
    > 0
    > 0
    > 0
    > 0
    > 12
    > 12
    > 11
    > 14
    > 18
    > 15
    > 9
    > 10
    >
    > Hope someone can help.
    >
    > Thanks
    > Jim
    >

      My System SpecsSystem Spec

  3. #3


    Hal Rottenberg Guest

    Re: Need help with Array and Export-CSV

    On Aug 30, 3:44 pm, jsimpson2...@xxxxxx wrote:

    > #counts the total computers in AD
    > $count = ($comp | measure-object).count
    Few general comments:
    - Almost everything which goes through a pipeline ends up being an
    array or collection of objects. These all have a .length property.
    You don't need extra processing to find this. '$comp.count' would do
    it.

    > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >
    > $table[$inc,0] = $I.name
    > So I have two problems: First, When I display the array to the screen,
    > is comes out like this -
    >
    > ComputerName 1
    > ComputerName 1
    Once you learn more about Powershell, you'll see that whenever you
    output objects (can be just strings or any of a million other types),
    they go through format-list or format-table automatically, and if you
    don't like how it turns out, you can do this manually and specify tons
    of settings. So what I'm saying is you do not need to build tables by
    fudging with lining up strings just right anymore. Just treat things
    like objects whenever you can, and the magic behind the scenes will
    take care of your output. And this applies to the screen or a plain
    text file or a CSV file or a SQL query etc, etc, etc.

    This is why Brandon's suggesting you use a custom object.


      My System SpecsSystem Spec

  4. #4


    jsimpson2000 Guest

    Re: Need help with Array and Export-CSV

    Wow, that is so cool. Thank you so much for the help. I was on the
    right track, just didn't know I could do that.

    Jim

    On Aug 30, 4:01 pm, "Brandon Shell" <tshell.m...@xxxxxx> wrote:

    > Maybe a better way to to do it is to create a custom objecthttp://bsonposh.com/modules/wordpress/?p=25
    >
    > Everything I added has ############ around it
    >
    > #gets all computers in AD using the Quest AD cmdlet
    > $comp = get-qadcomputer | sort-object
    >
    > ##################
    > #Create a generic Collection Obj
    > $myCollection = @()
    > ##################
    >
    > #counts the total computers in AD
    > $count = ($comp | measure-object).count
    >
    > #creates a 2 dimensional array
    > $table = new-object "string[,]" $count,3
    >
    > #screen display
    > "AD Name, Network Name, User Name"
    >
    > #Foreach loop to check logged in users in domain.
    > #Displays results on screen and adds results to the array
    > foreach ($i in $comp)`
    > {
    > $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >
    > #######################################
    > # Create Custom Object
    > $myobj = "" | select ADName,NetworkName,User
    > #######################################
    >
    > $table[$inc,0] = $I.name
    > $table[$inc,1] = $CS.Name
    > $table[$inc,2] = $CS.UserName
    >
    > ##################
    > $myobj.ADName = $I.Name
    > $myobj.NetworkName = $CS.Name
    > $myobj.User = $CS.UserName
    > $myCollection += $myobj
    > ##################
    >
    > }
    >
    > #################
    > $myColleciton | Export-Csv C:\testfile.csv
    > #################
    >
    > <jsimpson2...@xxxxxx> wrote in message
    >
    > news:1188503042.238763.257570@xxxxxx
    >
    >
    >

    > > Hi all,
    >

    > > I am a system admin and I am a new to PowerShell and scripting in
    > > general but I am getting to know it pretty quick. I have been
    > > messing with a piece of code to get a list of all the computers is the
    > > AD, then check those names and return the current logged on user.
    > > here is the code I have so far:
    >

    > > #gets all computers in AD using the Quest AD cmdlet
    > > $comp = get-qadcomputer | sort-object
    >

    > > #counts the total computers in AD
    > > $count = ($comp | measure-object).count
    >

    > > #creates a 2 dimensional array
    > > $table = new-object "string[,]" $count,3
    >

    > > #counting variable
    > > $inc = 0
    >

    > > #screen display
    > > "AD Name, Network Name, User Name"
    >

    > > #Foreach loop to check logged in users in domain.
    > > #Displays results on screen and adds results to the array
    > > foreach ($i in $comp)`
    > > {
    > > $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    > > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >

    > > $table[$inc,0] = $I.name
    > > $table[$inc,1] = $CS.Name
    > > $table[$inc,2] = $CS.UserName
    >

    > > $inc++
    > > }
    >

    > > So I have two problems: First, When I display the array to the screen,
    > > is comes out like this -
    >

    > > ComputerName 1
    > > ComputerName 1
    > > UserName 1
    > > ComputerName 2
    > > ComputerName 2
    > > UserName 2
    > > ComputerName 3
    > > ComputerName 3
    > > UserName 3
    >

    > > Instead of like this -
    >

    > > ComputerName 1 ComputerName 1 UserName 1
    > > ComputerName 2 ComputerName 2 UserName 2
    > > ComputerName 3 ComputerName 3 UserName 3
    >

    > > Also, I can not figure out how to pass this to Export-CSV. when I do I
    > > get -
    >

    > > Length
    > > 0
    > > 0
    > > 0
    > > 0
    > > 0
    > > 0
    > > 12
    > > 12
    > > 11
    > > 14
    > > 18
    > > 15
    > > 9
    > > 10
    >

    > > Hope someone can help.
    >

    > > Thanks
    > > Jim- Hide quoted text -
    >
    > - Show quoted text -


      My System SpecsSystem Spec

  5. #5


    jsimpson2000 Guest

    Re: Need help with Array and Export-CSV

    Ahh, another lightbulb. Thank you.



    Jim

    On Aug 30, 9:54 pm, Hal Rottenberg <halr9...@xxxxxx> wrote:

    > On Aug 30, 3:44 pm, jsimpson2...@xxxxxx wrote:
    >

    > > #counts the total computers in AD
    > > $count = ($comp | measure-object).count
    >
    > Few general comments:
    > - Almost everything which goes through a pipeline ends up being an
    > array or collection of objects. These all have a .length property.
    > You don't need extra processing to find this. '$comp.count' would do
    > it.
    >

    > > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >

    > > $table[$inc,0] = $I.name
    > > So I have two problems: First, When I display the array to the screen,
    > > is comes out like this -
    >

    > > ComputerName 1
    > > ComputerName 1
    >
    > Once you learn more about Powershell, you'll see that whenever you
    > output objects (can be just strings or any of a million other types),
    > they go through format-list or format-table automatically, and if you
    > don't like how it turns out, you can do this manually and specify tons
    > of settings. So what I'm saying is you do not need to build tables by
    > fudging with lining up strings just right anymore. Just treat things
    > like objects whenever you can, and the magic behind the scenes will
    > take care of your output. And this applies to the screen or a plain
    > text file or a CSV file or a SQL query etc, etc, etc.
    >
    > This is why Brandon's suggesting you use a custom object.


      My System SpecsSystem Spec

  6. #6


    jsimpson2000 Guest

    Re: Need help with Array and Export-CSV

    Brandon,

    I get 2 strange results when I run the script at the ps command line
    as ". .\names.ps1". First, after the script stops the last message
    from it is:

    Export-Csv : Cannot bind argument to parameter 'InputObject' because
    it is null.
    At C:\scripts\names6.ps1:44 char:27
    + $myColleciton | Export-Csv <<<< C:\scripts\testfile.csv

    And second when I check the the contents of the variable $mycollection
    I get the last computer checked over and over... not the whole
    collection as expected:

    ADName
    NetworkName User
    ------
    ----------- ----
    RIGHTFAX RIGHTFAX
    RIGHTFAX RIGHTFAX
    RIGHTFAX RIGHTFAX
    RIGHTFAX RIGHTFAX

    Hope you can help, I know I am missing something obvious. Here is
    the script in it's current form:

    #gets all computers in AD using the Quest AD cmdlet
    $comp = get-qadcomputer | sort-object


    #counts the total computers in AD
    $count = $comp.count

    ##################
    #Create a generic Collection Obj
    $myCollection = @()
    ##################


    ############
    # Create Custom Object
    $myobj = "" | select ADName,NetworkName,User
    ############


    #screen display
    "Computes in Active Directory = " + $count
    "AD Name, Network Name, User Name"

    #Foreach loop to check logged in users in domain.
    #Displays results on screen and adds results to the array
    foreach ($i in $comp)`
    {
    $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    $I.name + ", " + $CS.Name + ", " + $CS.UserName

    ##################
    $myobj.ADName = $I.Name
    $myobj.NetworkName = $CS.Name
    $myobj.User = $CS.UserName
    $myCollection += $myobj
    ##################

    }

    #################
    $myColleciton | Export-Csv C:\scripts\testfile.csv
    #################



    On Aug 30, 4:01 pm, "Brandon Shell" <tshell.m...@xxxxxx> wrote:

    > Maybe a better way to to do it is to create a custom objecthttp://bsonposh.com/modules/wordpress/?p=25
    >
    > Everything I added has ############ around it
    >
    > #gets all computers in AD using the Quest AD cmdlet
    > $comp = get-qadcomputer | sort-object
    >
    > ##################
    > #Create a generic Collection Obj
    > $myCollection = @()
    > ##################
    >
    > #counts the total computers in AD
    > $count = ($comp | measure-object).count
    >
    > #creates a 2 dimensional array
    > $table = new-object "string[,]" $count,3
    >
    > #screen display
    > "AD Name, Network Name, User Name"
    >
    > #Foreach loop to check logged in users in domain.
    > #Displays results on screen and adds results to the array
    > foreach ($i in $comp)`
    > {
    > $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >
    > #######################################
    > # Create Custom Object
    > $myobj = "" | select ADName,NetworkName,User
    > #######################################
    >
    > $table[$inc,0] = $I.name
    > $table[$inc,1] = $CS.Name
    > $table[$inc,2] = $CS.UserName
    >
    > ##################
    > $myobj.ADName = $I.Name
    > $myobj.NetworkName = $CS.Name
    > $myobj.User = $CS.UserName
    > $myCollection += $myobj
    > ##################
    >
    > }
    >
    > #################
    > $myColleciton | Export-Csv C:\testfile.csv
    > #################
    >
    > <jsimpson2...@xxxxxx> wrote in message
    >
    > news:1188503042.238763.257570@xxxxxx
    >
    >
    >

    > > Hi all,
    >

    > > I am a system admin and I am a new to PowerShell and scripting in
    > > general but I am getting to know it pretty quick. I have been
    > > messing with a piece of code to get a list of all the computers is the
    > > AD, then check those names and return the current logged on user.
    > > here is the code I have so far:
    >

    > > #gets all computers in AD using the Quest AD cmdlet
    > > $comp = get-qadcomputer | sort-object
    >

    > > #counts the total computers in AD
    > > $count = ($comp | measure-object).count
    >

    > > #creates a 2 dimensional array
    > > $table = new-object "string[,]" $count,3
    >

    > > #counting variable
    > > $inc = 0
    >

    > > #screen display
    > > "AD Name, Network Name, User Name"
    >

    > > #Foreach loop to check logged in users in domain.
    > > #Displays results on screen and adds results to the array
    > > foreach ($i in $comp)`
    > > {
    > > $CS = Gwmi Win32_ComputerSystem -Comp $I.name
    > > $I.name + ", " + $CS.Name + ", " + $CS.UserName
    >

    > > $table[$inc,0] = $I.name
    > > $table[$inc,1] = $CS.Name
    > > $table[$inc,2] = $CS.UserName
    >

    > > $inc++
    > > }
    >

    > > So I have two problems: First, When I display the array to the screen,
    > > is comes out like this -
    >

    > > ComputerName 1
    > > ComputerName 1
    > > UserName 1
    > > ComputerName 2
    > > ComputerName 2
    > > UserName 2
    > > ComputerName 3
    > > ComputerName 3
    > > UserName 3
    >

    > > Instead of like this -
    >

    > > ComputerName 1 ComputerName 1 UserName 1
    > > ComputerName 2 ComputerName 2 UserName 2
    > > ComputerName 3 ComputerName 3 UserName 3
    >

    > > Also, I can not figure out how to pass this to Export-CSV. when I do I
    > > get -
    >

    > > Length
    > > 0
    > > 0
    > > 0
    > > 0
    > > 0
    > > 0
    > > 12
    > > 12
    > > 11
    > > 14
    > > 18
    > > 15
    > > 9
    > > 10
    >

    > > Hope someone can help.
    >

    > > Thanks
    > > Jim- Hide quoted text -
    >
    > - Show quoted text -


      My System SpecsSystem Spec

  7. #7


    jsimpson2000 Guest

    Re: Need help with Array and Export-CSV

    OK,

    I experimented on the command line and I got the following:

    $col = @()
    $a = 1
    $b = 2
    $c = 3
    $myobj = "" | select A,B,C
    $myobj.a = $a
    $myobj.b = $b
    $myobj.c = $c
    $col += $myobj

    Here is the value of collection after first pass:
    PS> $col
    A
    B C
    -
    - -
    1
    2 3


    $a = 4
    $b = 5
    $c = 6
    $myobj.a = $a
    $myobj.b = $b
    $myobj.c = $c
    $col += $myobj

    And here ist is after the second.... Greek to me...

    PS>$col

    A
    B C
    -
    - -
    4
    5 6
    4
    5 6



      My System SpecsSystem Spec

  8. #8


    Hal Rottenberg Guest

    Re: Need help with Array and Export-CSV

    jsimpson2000@xxxxxx wrote:

    > $col = @()
    > $a = 1
    > $b = 2
    > $c = 3
    > $myobj = "" | select A,B,C
    > $myobj.a = $a
    > $myobj.b = $b
    > $myobj.c = $c
    > $col += $myobj

    >
    > And here ist is after the second.... Greek to me...
    >
    > PS>$col
    >
    > A
    > B C
    > -
    > - -
    > 4
    > 5 6
    > 4
    > 5 6
    1. created an array
    2. created 3 scalar objects containing integers
    3. created a custom object with 3 properties
    4. assigned step 2 to step 3
    5. placed the contents of the object with 3 properties into the array

    So far so good, right? At the second run what you did was

    6. change the three values
    7. assigned them to the custom object again, in the same scope, overwriting what
    was previously there
    8. put a second copy in the array

    So the output shows two of the same thing. That's how I read it anyway.



    --

    Hal Rottenberg
    blog: http://halr9000.com
    powershell category:
    http://halr9000.com/article/category...ng/powershell/

      My System SpecsSystem Spec

  9. #9


    jsimpson2000 Guest

    Re: Need help with Array and Export-CSV

    On Aug 31, 10:42 am, Hal Rottenberg <h...@xxxxxx> wrote:

    >
    > 1. created an array
    > 2. created 3 scalar objects containing integers
    > 3. created a custom object with 3 properties
    > 4. assigned step 2 to step 3
    > 5. placed the contents of the object with 3 properties into the array
    >
    > So far so good, right? At the second run what you did was
    >
    > 6. change the three values
    > 7. assigned them to the custom object again, in the same scope, overwriting what
    > was previously there
    > 8. put a second copy in the array
    >
    > So the output shows two of the same thing. That's how I read it anyway.
    >
    > --
    >
    > Hal Rottenberg
    > blog:http://halr9000.com
    > powershell category:http://halr9000.com/article/category...g/powershell/- Hide quoted text -
    >
    > - Show quoted text -
    It seams like it is creating an array of variables instead of an array
    of the current value of variables. I just cant figure out how to just
    pass the value.

    Jim


      My System SpecsSystem Spec

  10. #10


    Kiron Guest

    Re: Need help with Array and Export-CSV

    You can use calculated properties/fields
    '@{name = <string>; expression = {<scriptblock>}}'

    For more on calculated properties/fields:

    (get-help select-object).description
    ((get-help select-object).examples).example[3]

    Try this:

    $computers = get-qadcomputer | foreach {
    $comp = $_
    gwmi Win32_ComputerSystem -Comp $comp} |
    select @{name = 'ADName'; expression = {$comp}},
    @{name = 'NetworkName'; expression = {$_.name}},
    @{name = 'UserName'; expression = {$_.userName}} |
    sort-object ADName

    $count = $computers.count

    # export data to csv file
    $computers | export-csv Computers.txt -noType

    # FYI:
    # $computers is an object array [object[]]
    # of [PSCustomObject]
    $computers.GetType().name

    # you can get a complete element from the array:
    $computers[2]

    # or just an element's property
    $computers[2].userName

    --
    Kiron

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Need help with Array and Export-CSV

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast copy method of sub array (=array range) possible? Thomas Lebrecht VB Script 6 19 Mar 2009
How to create array without quotes? $array = (a,b,c) BenConrad PowerShell 5 04 Feb 2009
Stupid Array Tricks: Initializing an Array to a Certain Size tojo2000 PowerShell 2 09 Sep 2008
how to assign values to array and how to create array via variable Frank PowerShell 1 13 Mar 2007
Export-CliXml/Export-Csv: Change to Export-Object? Alex K. Angelopoulos [MVP] PowerShell 3 04 Jun 2006