header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
read to the comma
Last Post 11 Aug 2010 07:20 AM by Joel "Jaykul" Bennett. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SylnoisUser is Offline
New Member
New Member
Posts:11
Avatar

--
10 Aug 2010 07:27 AM
    Hey guys
    Have a little problem.
    Here's my code:

    $datei = Get-content C:\Users\Dario\Desktop\Powershell\bla.csv
    $Date = get-date -format d
    $Time = get-date -format T
    $a = 0
    $datei | Foreach-Object {
    $a++;
    $ip = $_
    $qry = ('select statuscode from win32_pingstatus where
    address="' + $ip + '"')
    $rslt = gwmi –query “$qry”
    if ($rslt.StatusCode –eq 0) {
    write-host "+" $ip
    }
    else {
    $datei2 = "C:\Users\Dario\Desktop\Powershell
    \FAIL\$Date.txt"
    $Time + " | "+$ip | Add-content $datei2
    write-host "-" $ip
    }
    $rslt = $null
    }

    My CSV-File looks so:
    192.168.1.1
    192.168.1.2
    192.168.1.3

    With this two files my programm works, but i want that my CSV-File looks like this:
    192.168.1.1, Text1
    192.168.1.2, Text2
    192.168.1.3, Text3

    How i must change my source, that when an ip isn't accessible, he only print the text out.

    PS: Sry for my very bad english. I'm from switzerland and my notes in english are fucked up.

    Thanks
    Sylnois


    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    10 Aug 2010 07:38 AM
    # powershell 2.0

    import-csv bla.csv -Header ip | select ip,@{n='alive';e={test-connection $_.ip -quiet -count 1}}

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    SylnoisUser is Offline
    New Member
    New Member
    Posts:11
    Avatar

    --
    10 Aug 2010 07:44 AM
    Thanks for your reply.
    Can you update my source?
    I dont unterstand your source.
    That would be very, very nice of you.
    SylnoisUser is Offline
    New Member
    New Member
    Posts:11
    Avatar

    --
    11 Aug 2010 12:02 AM
    You misunderstood me.
    The print must be like this:

    + Text1
    - Text2
    - Text2
    Joel "Jaykul" BennettUser is Offline
    Basic Member
    Basic Member
    Posts:112
    Avatar

    --
    11 Aug 2010 07:20 AM
    The basics:
    $Date = get-date -format yyyy-mm-dd\.\t\x\t
    $Time = get-date -format T
    # Use Import-CSV to read in the CSV. If the csv doesn't have a header row, you should specify headers when you call import-csv:
    foreach($address in import-csv bla.csv -header "IP","Text") {
    # Now you can use the .IP and .Text properties on the imported objects (as specified in the header):
    # If you want to use WMI directly, that's fine, but really you should use: test-connection $address.IP -quiet -count 1
    if( (gwmi -class win32_pingstatus -property StatusCode -filter "address='$($address.IP)'").StatusCode -eq 0) {
    "+ $($Address.Text)" | Add-Content $Date
    } else {
    "- $($Address.Text)" | Add-Content $Date
    }
    }
    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