none
Power ping and get computer, username

    Question

  • Dear Exprt,

    I am not getting result, can u check below code whats wrong in


    function Ping-Host {
    BEGIN {}
    PROCESS {


    $results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$computer'"
    $wmiobject = Get-WmiObject -ComputerName $computer win32_ComputerSystem

    if ($results.StatusCode -eq 0) { 

     $wmiobject.name + $wmiobject.username

    #Write-Host "$computer. is Pingable"
    } else {

    Write-Host "$computer is not Pingable" -BackgroundColor red 
    }
    }
    END {}

    $computers = Get-Content g:\script\ipaddress.txt
    foreach ($computer in $computers) {
    if (Ping-Host $computer) {
    }



    Support@Mytechnet.me

    Sunday, January 13, 2013 9:22 AM

Answers

  • In that case it will require remote WMI query. You just modify script as below. We will still use System.Net.Dns class for IP resolution and WMI query to grab logged on UserName.

    $ErrorActionPreference = 'SilentlyContinue'
    $computers = Get-Content g:\script\ipaddress.txt
    foreach($computer in $computers) {
    $ping = Test-Connection -ComputerName $computer -Count 1 -Quiet
    $computerName = [system.net.dns]::Resolve("$computer")
    #$hostname = $computerName.HostName
    $shortHostname = $computerName.HostName.Split(".")[0]
    if ($ping -eq 'True')
        {
            $UserName = (Get-WmiObject -ComputerName $computer win32_ComputerSystem).UserName
            Write-Host "$shortHostname  $UserName" -ForegroundColor Green
            #Write-Host "$hostname is pingable" -ForegroundColor Green
        }
    else
        {
            Write-Host "$computer is not pingable" -ForegroundColor Red
        }
    }


    MCP, MCTS, MCSE 2003, MCITP 2008, MCSA 2012
    LinkedIn: http://www.linkedin.com/pub/jatin-patel/25/90b/2a/

    This posting is provided 'AS IS' with no warranties or guarantees and confers no rights.
    Please help and appreciate others by using these features: "Propose As Answer", "Vote As Helpful" and "Mark As Answer"



    Sunday, January 13, 2013 6:59 PM

All replies

  • I don't know what kind of result you're expecting but if you just want to see whether a machine is ping-able you can write something like:

    $computer = "TestMachine"
    
    if (Test-Connection -ComputerName $computer) {
        Write-Output $true
    }
    else {
        Write-Output $false
    }

    Also please in the future remember that Technet isn't here for debugging purposes, it is here for questions and answers. Providing more information about your expected output will allow people to help you more easily.

    Sunday, January 13, 2013 10:47 AM
  • I think, your function is lacking a param part, so you can use your $computer variable. Try the following:

    function Ping-Host {
    param (
            [string]$computer
          )
    ...
    }

    By the way you might better modify your line "$wmiobject.name + $wmiobject.username" as follows:  

    "$wmiobject.name + " " + $wmiobject.username"

    Kind Regards,

    wizend


    • Edited by Wizend Sunday, January 13, 2013 3:17 PM
    Sunday, January 13, 2013 3:12 PM
  • Agree to Benjamin,

    You can use Test-Connection cmdlet and I would also use System.Net.DNS class to convert IP to hostname instead WMI query. It will be more faster and less prone to errors.

    $ErrorActionPreference = 'SilentlyContinue'
    $computers = Get-Content C:\Users\Jatin.GOJALI\Contacts\ip.txt
    foreach($computer in $computers) {
    $ping = Test-Connection -ComputerName $computer -Count 1 -Quiet
    $computerName = [system.net.dns]::Resolve("$computer")
    $hostname = $computerName.HostName
    #$shortHostname = $computerName.HostName.Split(".")[0]
    if ($ping -eq 'True')
        {
            Write-Host "$hostname is pingable" -ForegroundColor Green
        }
    else
        {
            Write-Host "$computer is not pingable" -ForegroundColor Red
        }
    }



    MCP, MCTS, MCSE 2003, MCITP 2008, MCSA 2012
    LinkedIn: http://www.linkedin.com/pub/jatin-patel/25/90b/2a/

    This posting is provided 'AS IS' with no warranties or guarantees and confers no rights.
    Please help and appreciate others by using these features: "Propose As Answer", "Vote As Helpful" and "Mark As Answer"




    • Edited by pateljy Sunday, January 13, 2013 6:02 PM
    Sunday, January 13, 2013 5:32 PM
  • Dear,

    My concerned is when ip pingable i want to run another command to get computer name and username logn for this IP address and i have almost 100 ipaddress to completed this task


    Support@Mytechnet.me

    Sunday, January 13, 2013 6:41 PM
  • In that case it will require remote WMI query. You just modify script as below. We will still use System.Net.Dns class for IP resolution and WMI query to grab logged on UserName.

    $ErrorActionPreference = 'SilentlyContinue'
    $computers = Get-Content g:\script\ipaddress.txt
    foreach($computer in $computers) {
    $ping = Test-Connection -ComputerName $computer -Count 1 -Quiet
    $computerName = [system.net.dns]::Resolve("$computer")
    #$hostname = $computerName.HostName
    $shortHostname = $computerName.HostName.Split(".")[0]
    if ($ping -eq 'True')
        {
            $UserName = (Get-WmiObject -ComputerName $computer win32_ComputerSystem).UserName
            Write-Host "$shortHostname  $UserName" -ForegroundColor Green
            #Write-Host "$hostname is pingable" -ForegroundColor Green
        }
    else
        {
            Write-Host "$computer is not pingable" -ForegroundColor Red
        }
    }


    MCP, MCTS, MCSE 2003, MCITP 2008, MCSA 2012
    LinkedIn: http://www.linkedin.com/pub/jatin-patel/25/90b/2a/

    This posting is provided 'AS IS' with no warranties or guarantees and confers no rights.
    Please help and appreciate others by using these features: "Propose As Answer", "Vote As Helpful" and "Mark As Answer"



    Sunday, January 13, 2013 6:59 PM
  • Thx, its worked how i want.

    how do i export, shell i use pipe export


    Support@Mytechnet.me

    Sunday, January 13, 2013 8:05 PM
  • You can out-file to text file. Just add following lines for "if" and "else" statement after Write-Host. Write-Host will output to PowerShell Console host and Out-File will output to specified txt file.

    Out-File -InputObject "$shortHostName $username" -FilePath g:\script\outfile.txt -Append
    Out-File -InputObject "$Computer is not pingable" -FilePath g:\script\outfile.txt -Append


    MCP, MCTS, MCSE 2003, MCITP 2008, MCSA 2012
    LinkedIn: http://www.linkedin.com/pub/jatin-patel/25/90b/2a/

    This posting is provided 'AS IS' with no warranties or guarantees and confers no rights.
    Please help and appreciate others by using these features: "Propose As Answer", "Vote As Helpful" and "Mark As Answer"






    • Edited by pateljy Sunday, January 13, 2013 8:32 PM
    Sunday, January 13, 2013 8:22 PM