none
Powershell Creating Array of Objects based on one column RRS feed

  • Question

  • Hi,

    I am relatively new at powershell and am attempting to write a script where I am importing a CSV file and based on the hostname on the server that I am running this on it would return to me in which office this server is located and all the other servers that are in the same office.  MY CSV looks like this

    Column 1(Site)                                      Column 2(ServerName)

    FL                                                                         a1,a3

    NC                                                                         a2

    ME                                                                          a4,a5,a7


    • Edited by ogratwick Saturday, February 11, 2017 11:16 PM
    Saturday, February 11, 2017 11:16 PM

Answers

  • Appreciate the quick response.  What I am trying to do though is less for formatting and more for using the objects in the list to get further information.  I could find all the different locations but what I need is to be able to do is to input a hostname, and from there the script should be able to understand which location the server correlated to and provide me the list of all the other servers in that location.  Basically what I am trying to achieve down the line is to be able to display Server information like CPU and RAM for all the servers in a certain location.

    I did a basic modification to your script, and after recreating your CSV file and testing it, I think this makes the few changes yours needed.  Quick rundown on the changes so you know why I changed what I did:

    1. Stripped off the variable assignment from the import-csv statement. Since we're passing straight into a ForEach loop, you don't need it.

    2. Split the server names by commas so it returns the sites with multiple servers as an array instead of one string.

    3.  When it finds the site you gave it, it outputs the contents of the array ($SNames) instead of ($Name) which was how yours was setup. 

    4. End in a Return statement so you can pipe this command to another cmdlet instead of having it just write-host and leave it on the screen.

    function GetServer ($serverName)
    {
        import-csv C:\users\testUser\Documents\ServerTest.csv | % {
            $SNames = $_.Servername.split(",") 
            $Site = $_.Site
            foreach ($name in $SNames)
            {
                if ($serverName -eq $name)
                {
                    return $SNames
                }
            }
        }
    }

    You were most of the way there. Stick with it, man.


    Monday, February 13, 2017 1:33 PM

All replies

  • $csv = @"
    "Column 1(Site)","Column 2(ServerName)"
    "FL","a1,a3"
    "NC","a2"
    "ME","a4,a5,a7"
    "@
    
    ConvertFrom-Csv $csv | where {($_."Column 2(ServerName)" -split ',') -Contains $env:computername}

    Saturday, February 11, 2017 11:33 PM
  • I think you need to clarify what you are trying to do.  Are you starting with a CSV?  How are you getting this file.  If it is a csv then jsut add a column with the file name the value:

    Import-Csv $file | Select *, @{n='Server';e={$file.BaseName}}


    \_(ツ)_/

    Sunday, February 12, 2017 12:30 AM
  • A stated you request us very confusing.


    \_(ツ)_/

    Sunday, February 12, 2017 12:31 AM
  • I think you need to clarify what you are trying to do.  Are you starting with a CSV?  How are you getting this file.  If it is a csv then jsut add a column with the file name the value:

    Import-Csv $file | Select *, @{n='Server';e={$file.BaseName}}


    \_(ツ)_/

    We currently have a CSV with all the servers listed for each location.  We have 2 columns in this CSV, Location and Server Name.  So as you would expect we have multiple servers for each location.  What I am trying to create is a script that would get the hostname of the server and identify which location it is in.  From there I would want it to return back to me all the servers that are also in that location.  Hopefully that clears it up a bit.
    Sunday, February 12, 2017 6:23 PM
  • This is what I have so far.  From this I can figure out which location the server belongs to.  But have not been successfully able to return the rest of the servers that belong to that location.

    function GetServer {            
     [cmdletbinding()]            
     param(            
     [Parameter(Mandatory=$true)]            
     [string]$Hostname                    
     )

     $csv = Import-Csv "C:\Users\Desktop\ServerList.csv" | % {
            

            $Name=$_.Name
            $site=$_.Site

                foreach($Names in $Name){

                    if ($Hostname -eq $Name){

                        Write-Host "$Site"
                 
                     }               
            }
        }

    }





     getserver 

     
    Sunday, February 12, 2017 6:33 PM
  • Why not let PowerShell fo it for you:

    Import-Csv C:\Users\Desktop\ServerList.csv | Format-Table -GroupBy Site

    OR:

    Import-Csv C:\Users\Desktop\ServerList.csv | 
    	Group-Object Site |
    	ForEach-Object{
    		Write-Host $_.Name -NoNewline -fore red
    		Write-Host ($_.Group.Site -join '|') -fore green
    	}


    \_(ツ)_/


    • Edited by jrv Sunday, February 12, 2017 9:53 PM
    Sunday, February 12, 2017 9:49 PM
  • Appreciate the quick response.  What I am trying to do though is less for formatting and more for using the objects in the list to get further information.  I could find all the different locations but what I need is to be able to do is to input a hostname, and from there the script should be able to understand which location the server correlated to and provide me the list of all the other servers in that location.  Basically what I am trying to achieve down the line is to be able to display Server information like CPU and RAM for all the servers in a certain location.
    Sunday, February 12, 2017 10:06 PM
  • I recommend that you start by learning PowerShell.  Once you have a bit more understanding of what a script is and how it works you will be able to write a better question.


    \_(ツ)_/

    Sunday, February 12, 2017 10:16 PM
  • Appreciate the quick response.  What I am trying to do though is less for formatting and more for using the objects in the list to get further information.  I could find all the different locations but what I need is to be able to do is to input a hostname, and from there the script should be able to understand which location the server correlated to and provide me the list of all the other servers in that location.  Basically what I am trying to achieve down the line is to be able to display Server information like CPU and RAM for all the servers in a certain location.

    I did a basic modification to your script, and after recreating your CSV file and testing it, I think this makes the few changes yours needed.  Quick rundown on the changes so you know why I changed what I did:

    1. Stripped off the variable assignment from the import-csv statement. Since we're passing straight into a ForEach loop, you don't need it.

    2. Split the server names by commas so it returns the sites with multiple servers as an array instead of one string.

    3.  When it finds the site you gave it, it outputs the contents of the array ($SNames) instead of ($Name) which was how yours was setup. 

    4. End in a Return statement so you can pipe this command to another cmdlet instead of having it just write-host and leave it on the screen.

    function GetServer ($serverName)
    {
        import-csv C:\users\testUser\Documents\ServerTest.csv | % {
            $SNames = $_.Servername.split(",") 
            $Site = $_.Site
            foreach ($name in $SNames)
            {
                if ($serverName -eq $name)
                {
                    return $SNames
                }
            }
        }
    }

    You were most of the way there. Stick with it, man.


    Monday, February 13, 2017 1:33 PM