header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Creating a live server inventory
Last Post 14 Apr 2009 09:32 AM by EBGreen. 11 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
edm365f31User is Offline
Basic Member
Basic Member
Posts:100
Avatar

--
13 Apr 2009 11:26 AM
    I am trying to create an Alive Server Inventory that will reference a types in a text file
    example Zebr5%xx%
    it will iterate these type zebr501 to zebr599, by method insert, or maybe replace.
    it will then do a pingit and save the result to a text file with corresponding IP when its alive.

    This will allow me to know which boxes are alive based on types everyday, it will also help if this could be done multi-threaded.

    We have a person who did this in vbscript and I've been trying to convert it but I am hitting a wall.

    Any help to get this going will be appreciated, I think this is great in a big environment were you have over 500 boxes of windows servers, our case are all windows 2003.
    Thanks
     
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1092
    Avatar

    --
    13 Apr 2009 11:25 AM
    I would start by breaking the task into parts then work your way through implementing the parts. Fo instance, start by making a loop that just generates the strings:

    Zebr501
    .
    .
    .
    Zebr599
    "Look Ma...no strings!"
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    13 Apr 2009 11:57 AM
    foreach ($strhost in gc \\server\share\hosttype.txt # i.e. hostype maybe zebr5%xx% for windows and maybe zebr3%xx% for Linux boxes, how do I generate a for loop and repl zebr5%xx% 1 to 99. need dynamic because its boxes and agent are being rebuilt and re-profiled.
    ::For Loop to generate zebra501 to zebr599 then zebr301 zebr399
    then
    ::Pingit
    $query = "select * from win32_pingstatus where address = '$strhostr'"
    $result = gwmi -query $query
    if ($result.protocoladdress)
    ::Here add the pingable server to a list maybe a separate list windows will be zebr501 to 599 then Linux list Zebr301 to 399

    any ideas


    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1092
    Avatar

    --
    13 Apr 2009 12:02 PM
    Ok, here is a start for you. I'm doing this off the cuff so there may be issues, but it should get you started:
    foreach ($host in (gc \\server\share\hosttype.txt))
    {
        foreach ($number in (1..99))
        {
            $padded = "{0:0#}" -f $number
            $specificServer = $host.Replace('%xx%', $padded)
            Write-Host $specificServer
        }
    }

    "Look Ma...no strings!"
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    13 Apr 2009 12:20 PM
    Eb Trying to figure what this does, or what method is this?
    $padded = "{0:0#}" -f $number
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1092
    Avatar

    --
    13 Apr 2009 12:25 PM
    It is there so that 1 gets formatted as 01 and 2 as 02, etc.
    "Look Ma...no strings!"
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1092
    Avatar

    --
    13 Apr 2009 12:28 PM
    I think what I would do is write a ping function that you can pass the server name to in the loop.
    "Look Ma...no strings!"
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    13 Apr 2009 12:30 PM
    This works and generates the list..will it better for this to be a function so I can maybe pass it to a pingit routine.
    Foreach ($hosttype in (gc C:\Powershell\Study\HostList\hosttypes.txt))
    {
    foreach ($number in (1..99))
    {
    $itr = "{0:0#}" -f $number
    $strServer = $hosttype.Replace('%xx%', $itr)
    Write-Host $strServer
    }
    }
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    14 Apr 2009 04:19 AM
    this is what I have right now still need to add a logic that will create different group of host names csv, by All Servers, then separate them with windows and Linux. 500 600 3000 boxes are windows while the 700 and 800 boxes are Linux.

    Also the 3000 box will be 1...199, need to translate this
    in $itr = "{0:0#}" -f $number
    ******************************
    foreach ($hosttype in (gc C:\Powershell\Study\HostList\hosttypes.txt))
    {
    foreach ($number in (1..99))
    {
    $itr = "{0:0#}" -f $number
    $strServer = $hosttype.Replace('%xx%', $itr)

    $query = "select * from win32_pingstatus where address = '$strserver'"
    $result = gwmi -query $query

    if ($result.protocoladdress)

    {Write-Host $strServer" --"($result.protocoladdress)is Alive}
    Else
    {Write-Host $strServer is Down}
    }

    }
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    14 Apr 2009 04:38 AM
    this works for 3000 type box
    foreach ($hosttype in (gc C:\Powershell\Study\HostList\hosttypes.txt))
    {
    foreach ($number in (1..199))
    {
    $itr = "{0:00#}" -f $number
    $strServer = $hosttype.Replace('%xx%', $itr)
    Write-Host $strServer
    }
    }
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    14 Apr 2009 08:38 AM
    This is how I got the script right now. EB Thanks for the lead
    It still need some work but will serve as it is. I still need to grep out (all windows 500 600's) (All Linux 700 800's)
    I also thought about just creating a hash array? (dictionary) - not really sure how to do it
    I also need to eventually save this to a csv and create a Diff between today and yesterday so I know which box was removed or added. EB or anybody can give me a start on this.  I have an excel Macro that does this and need to do it in PS against CSV or Excel file.
    We have over a 1200 servers Linux and Windows and this will be a great help.
    Also need to have a way of multi-threading it so it does not take that long, not sure if PS in W2k3 is capable, I read that Vista can.  My solution right now maybe is break the hosttypes.txt by OS then run them at the same time and at the end just combine the whole output into one file..any thoughts. 
    If there's anything else that will improve this, I will appreciate it.
    *************************************************************

    $erroractionpreference = "SilentlyContinue"
    $VerbosePreference = "Continue"

    $Share = "\\network\shares"
    # Just my odd way of backing up the file
    copy-Item ("$Share\allpts.txt") ("$Share\allpts2.txt") -Force -Verbose
    copy-Item ("$Share\allpts2.txt") ("$Share\allpts3.txt") -Force -Verbose
    Remove-Item -Path ("$Share\allpts.txt") -Force -verbose

    foreach ($hosttype in (gc $Share\hosttypes.txt))
    {
    If ($hosttype -like "*3*")
    {
    foreach ($number in (1..199))
    {
    $itr = "{0:00#}" -f $number
    $strServer = $hosttype.Replace('%xx%', $itr)
    $query = "select * from win32_pingstatus where address = '$strserver'"
    $result = gwmi -query $query

    if ($result.protocoladdress)

    {Write-Host $strServer" --"($result.protocoladdress)is Alive
    Write-Output $strServer | Out-File -filepath $Share\allpts.txt -append -force -verbose
    }
    Else
    {Write-Host $strServer is Down}
    }
    }
    Else
    {
    foreach ($number in (1..99))
    {
    $itr = "{0:0#}" -f $number
    $strServer = $hosttype.Replace('%xx%', $itr)

    $query = "select * from win32_pingstatus where address = '$strserver'"
    $result = gwmi -query $query

    if ($result.protocoladdress)

    {Write-Host $strServer" --"($result.protocoladdress)is Alive
    Write-Output $strServer | Out-File -filepath $Share\allpts.txt -append -force -verbose }
    Else
    {Write-Host $strServer is Down}
    }
    }

    }
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1092
    Avatar

    --
    14 Apr 2009 09:32 AM
    Here are some pointers to get you started. For saving it out to a CSV:

    Get-Help Export-CSV

    For Comparing the CSV from one day to the next:

    Get-Help Compare-Object
    "Look Ma...no strings!"
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer