Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to ping machine names pulled from a datareader

Reply
 
Old 11-28-2008   #1 (permalink)
Jacob Sampson


 
 

How to ping machine names pulled from a datareader

The $CN contains the name of the computername which I verify by printing the
name to the console on line 4. But I never drop down into the success
portion of the if statement. If I replace the $CN variable in the ping
statement with the name of a machine it does work correctly. I can only
assume that $CN is an incorrect type for the ping commnad.

Any help is appreciated.



While($rdr.Read())
{
$CN = $rdr.getstring(0)
$CN
$ping = gwmi win32_pingstatus -f "Address = '$CN'"
if ($ping.StatusCode -eq 0)
{
write-output "Ping Worked"
}
Else
{
write-output "Ping Failed"
}

}

My System SpecsSystem Spec
Old 11-28-2008   #2 (permalink)
Vadims Podans


 
 

Re: How to ping machine names pulled from a datareader

$ping = gwmi win32_pingstatus -f "Address = '$CN'"
if ($ping.StatusCode -eq 0) {
write-output "Ping Worked"
} Else {
write-output "Ping Failed"
}
this code works as expected. However I'm interesting about this:
$rdr.Read()
$rdr.getstring(0)

Can you explain this?

--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv

"Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
"news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
Quote:

> The $CN contains the name of the computername which I verify by printing
> the
> name to the console on line 4. But I never drop down into the success
> portion of the if statement. If I replace the $CN variable in the ping
> statement with the name of a machine it does work correctly. I can only
> assume that $CN is an incorrect type for the ping commnad.
>
> Any help is appreciated.
>
>
>
> While($rdr.Read())
> {
> $CN = $rdr.getstring(0)
> $CN
> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> if ($ping.StatusCode -eq 0)
> {
> write-output "Ping Worked"
> }
> Else
> {
> write-output "Ping Failed"
> }
>
> }
My System SpecsSystem Spec
Old 11-28-2008   #3 (permalink)
Jacob Sampson


 
 

Re: How to ping machine names pulled from a datareader

I appreciate the response. I know how to make it work from a text file. I
have created a db of computers on my network that I get from active
directory. The db is going to contain a bunch of information per machine. I
want to be able to ping the machines in the db. There for I need to be able
to ping based on the response of my sql query.

"OldDog" wrote:
Quote:

> On Nov 28, 11:01 am, Jacob Sampson
> <JacobSamp...@xxxxxx> wrote:
Quote:

> > The $CN contains the name of the computername which I verify by printing the
> > name to the console on line 4. But I never drop down into the success
> > portion of the if statement. If I replace the $CN variable in the ping
> > statement with the name of a machine it does work correctly. I can only
> > assume that $CN is an incorrect type for the ping commnad.
> >
> > Any help is appreciated.
> >
> > While($rdr.Read())
> > {
> > $CN = $rdr.getstring(0)
> > $CN
> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> > if ($ping.StatusCode -eq 0)
> > {
> > write-output "Ping Worked"
> > }
> > Else
> > {
> > write-output "Ping Failed"
> > }
> >
> > }
>
> Give this a try.. Maybe more info than you want, but it works.
>
> #<---------------- Begin
> ----------------------------------------------->
> $ping = New-Object System.Net.NetworkInformation.Ping
> $computerlist = Get-Content 'c:\scripts\Servers.txt'
> foreach ($srv in $computerlist)
> {
> $reply = $ping.Send($srv)
> if ($reply.status -eq "Success")
> {
> if ($reply.Options.ttl -le 64)
> {
> Write-Host $srv " probably is a Unix host"
> }
> Else
> { if ($reply.Options.ttl -ge 65)
> {
> Write-Host $srv " probably is a Windows host"
> }
> }
> }
> Else { Write-Host $srv " Does Not Ping"
> }
>
> }
>
> #<----------------------- End Script----------------------*----->
>
My System SpecsSystem Spec
Old 11-28-2008   #4 (permalink)
Jacob Sampson


 
 

Re: How to ping machine names pulled from a datareader

the sqldatareader grabs data from a database. It is only one way though.
Meaning I can read from the database but I cant write to it like I could in a
dataset. In my case I only need to read the computernames in the table. If
I say $CN = "SomeComputerName" it works fine but the reader must be returning
a type that the ping does not understand.

"Vadims Podans" wrote:
Quote:

> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> if ($ping.StatusCode -eq 0) {
> write-output "Ping Worked"
> } Else {
> write-output "Ping Failed"
> }
> this code works as expected. However I'm interesting about this:
> $rdr.Read()
> $rdr.getstring(0)
>
> Can you explain this?
>
> --
> WBR, Vadims Podans
> PowerShell blog - www.sysadmins.lv
>
> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
> "news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
Quote:

> > The $CN contains the name of the computername which I verify by printing
> > the
> > name to the console on line 4. But I never drop down into the success
> > portion of the if statement. If I replace the $CN variable in the ping
> > statement with the name of a machine it does work correctly. I can only
> > assume that $CN is an incorrect type for the ping commnad.
> >
> > Any help is appreciated.
> >
> >
> >
> > While($rdr.Read())
> > {
> > $CN = $rdr.getstring(0)
> > $CN
> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> > if ($ping.StatusCode -eq 0)
> > {
> > write-output "Ping Worked"
> > }
> > Else
> > {
> > write-output "Ping Failed"
> > }
> >
> > }
>
>
My System SpecsSystem Spec
Old 11-28-2008   #5 (permalink)
Vadims Podans


 
 

Re: How to ping machine names pulled from a datareader

what type? Please, show:
($rdr.getstring(0))gettype().fullname

in this command return should be something like System.String

for workaround, try this:
[string]$CN = $rdr.getstring(0)
this tries to convert right side result to single string format.

--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv

"Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
"news:EB0DD698-F6C4-4ADE-9ECF-72CF98DA2881@xxxxxx"...
Quote:

> the sqldatareader grabs data from a database. It is only one way though.
> Meaning I can read from the database but I cant write to it like I could
> in a
> dataset. In my case I only need to read the computernames in the table.
> If
> I say $CN = "SomeComputerName" it works fine but the reader must be
> returning
> a type that the ping does not understand.
>
> "Vadims Podans" wrote:
>
Quote:

>> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
>> if ($ping.StatusCode -eq 0) {
>> write-output "Ping Worked"
>> } Else {
>> write-output "Ping Failed"
>> }
>> this code works as expected. However I'm interesting about this:
>> $rdr.Read()
>> $rdr.getstring(0)
>>
>> Can you explain this?
>>
>> --
>> WBR, Vadims Podans
>> PowerShell blog - www.sysadmins.lv
>>
>> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja
>> ziņojumā
>> "news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
Quote:

>> > The $CN contains the name of the computername which I verify by
>> > printing
>> > the
>> > name to the console on line 4. But I never drop down into the success
>> > portion of the if statement. If I replace the $CN variable in the ping
>> > statement with the name of a machine it does work correctly. I can
>> > only
>> > assume that $CN is an incorrect type for the ping commnad.
>> >
>> > Any help is appreciated.
>> >
>> >
>> >
>> > While($rdr.Read())
>> > {
>> > $CN = $rdr.getstring(0)
>> > $CN
>> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
>> > if ($ping.StatusCode -eq 0)
>> > {
>> > write-output "Ping Worked"
>> > }
>> > Else
>> > {
>> > write-output "Ping Failed"
>> > }
>> >
>> > }
>>
>>
My System SpecsSystem Spec
Old 11-28-2008   #6 (permalink)
Vadims Podans


 
 

Re: How to ping machine names pulled from a datareader

ooops, I'm missing a DOT:
($rdr.getstring(0)).gettype().fullname

--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv

"Vadims Podans" <vpodans> rakstīja ziņojumā
"news:#9aogfYUJHA.3668@xxxxxx"...
Quote:

> what type? Please, show:
> ($rdr.getstring(0))gettype().fullname
>
> in this command return should be something like System.String
>
> for workaround, try this:
> [string]$CN = $rdr.getstring(0)
> this tries to convert right side result to single string format.
>
> --
> WBR, Vadims Podans
> PowerShell blog - www.sysadmins.lv
>
> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
> "news:EB0DD698-F6C4-4ADE-9ECF-72CF98DA2881@xxxxxx"...
Quote:

>> the sqldatareader grabs data from a database. It is only one way though.
>> Meaning I can read from the database but I cant write to it like I could
>> in a
>> dataset. In my case I only need to read the computernames in the table.
>> If
>> I say $CN = "SomeComputerName" it works fine but the reader must be
>> returning
>> a type that the ping does not understand.
>>
>> "Vadims Podans" wrote:
>>
Quote:

>>> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
>>> if ($ping.StatusCode -eq 0) {
>>> write-output "Ping Worked"
>>> } Else {
>>> write-output "Ping Failed"
>>> }
>>> this code works as expected. However I'm interesting about this:
>>> $rdr.Read()
>>> $rdr.getstring(0)
>>>
>>> Can you explain this?
>>>
>>> --
>>> WBR, Vadims Podans
>>> PowerShell blog - www.sysadmins.lv
>>>
>>> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja
>>> ziņojumā
>>> "news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
>>> > The $CN contains the name of the computername which I verify by
>>> > printing
>>> > the
>>> > name to the console on line 4. But I never drop down into the success
>>> > portion of the if statement. If I replace the $CN variable in the
>>> > ping
>>> > statement with the name of a machine it does work correctly. I can
>>> > only
>>> > assume that $CN is an incorrect type for the ping commnad.
>>> >
>>> > Any help is appreciated.
>>> >
>>> >
>>> >
>>> > While($rdr.Read())
>>> > {
>>> > $CN = $rdr.getstring(0)
>>> > $CN
>>> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
>>> > if ($ping.StatusCode -eq 0)
>>> > {
>>> > write-output "Ping Worked"
>>> > }
>>> > Else
>>> > {
>>> > write-output "Ping Failed"
>>> > }
>>> >
>>> > }
>>>
>>>
My System SpecsSystem Spec
Old 11-28-2008   #7 (permalink)
Jacob Sampson


 
 

Re: How to ping machine names pulled from a datareader

The return is indeed system.string. So now I am confused why the ping
statement would not work.

"Vadims Podans" wrote:
Quote:

> ooops, I'm missing a DOT:
> ($rdr.getstring(0)).gettype().fullname
>
> --
> WBR, Vadims Podans
> PowerShell blog - www.sysadmins.lv
>
> "Vadims Podans" <vpodans> rakstīja ziņojumā
> "news:#9aogfYUJHA.3668@xxxxxx"...
Quote:

> > what type? Please, show:
> > ($rdr.getstring(0))gettype().fullname
> >
> > in this command return should be something like System.String
> >
> > for workaround, try this:
> > [string]$CN = $rdr.getstring(0)
> > this tries to convert right side result to single string format.
> >
> > --
> > WBR, Vadims Podans
> > PowerShell blog - www.sysadmins.lv
> >
> > "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
> > "news:EB0DD698-F6C4-4ADE-9ECF-72CF98DA2881@xxxxxx"...
Quote:

> >> the sqldatareader grabs data from a database. It is only one way though.
> >> Meaning I can read from the database but I cant write to it like I could
> >> in a
> >> dataset. In my case I only need to read the computernames in the table.
> >> If
> >> I say $CN = "SomeComputerName" it works fine but the reader must be
> >> returning
> >> a type that the ping does not understand.
> >>
> >> "Vadims Podans" wrote:
> >>
> >>> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> >>> if ($ping.StatusCode -eq 0) {
> >>> write-output "Ping Worked"
> >>> } Else {
> >>> write-output "Ping Failed"
> >>> }
> >>> this code works as expected. However I'm interesting about this:
> >>> $rdr.Read()
> >>> $rdr.getstring(0)
> >>>
> >>> Can you explain this?
> >>>
> >>> --
> >>> WBR, Vadims Podans
> >>> PowerShell blog - www.sysadmins.lv
> >>>
> >>> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja
> >>> ziņojumā
> >>> "news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
> >>> > The $CN contains the name of the computername which I verify by
> >>> > printing
> >>> > the
> >>> > name to the console on line 4. But I never drop down into the success
> >>> > portion of the if statement. If I replace the $CN variable in the
> >>> > ping
> >>> > statement with the name of a machine it does work correctly. I can
> >>> > only
> >>> > assume that $CN is an incorrect type for the ping commnad.
> >>> >
> >>> > Any help is appreciated.
> >>> >
> >>> >
> >>> >
> >>> > While($rdr.Read())
> >>> > {
> >>> > $CN = $rdr.getstring(0)
> >>> > $CN
> >>> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> >>> > if ($ping.StatusCode -eq 0)
> >>> > {
> >>> > write-output "Ping Worked"
> >>> > }
> >>> > Else
> >>> > {
> >>> > write-output "Ping Failed"
> >>> > }
> >>> >
> >>> > }
> >>>
> >>>
>
My System SpecsSystem Spec
Old 11-28-2008   #8 (permalink)
Jacob Sampson


 
 

Re: How to ping machine names pulled from a datareader

I am not sure if it is possible but what seems to be happening is that the
script moves past the ping statement before it gets a ping response. The
machine names just fly by and the $ping.statuscode is null.

"Vadims Podans" wrote:
Quote:

> ooops, I'm missing a DOT:
> ($rdr.getstring(0)).gettype().fullname
>
> --
> WBR, Vadims Podans
> PowerShell blog - www.sysadmins.lv
>
> "Vadims Podans" <vpodans> rakstīja ziņojumā
> "news:#9aogfYUJHA.3668@xxxxxx"...
Quote:

> > what type? Please, show:
> > ($rdr.getstring(0))gettype().fullname
> >
> > in this command return should be something like System.String
> >
> > for workaround, try this:
> > [string]$CN = $rdr.getstring(0)
> > this tries to convert right side result to single string format.
> >
> > --
> > WBR, Vadims Podans
> > PowerShell blog - www.sysadmins.lv
> >
> > "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
> > "news:EB0DD698-F6C4-4ADE-9ECF-72CF98DA2881@xxxxxx"...
Quote:

> >> the sqldatareader grabs data from a database. It is only one way though.
> >> Meaning I can read from the database but I cant write to it like I could
> >> in a
> >> dataset. In my case I only need to read the computernames in the table.
> >> If
> >> I say $CN = "SomeComputerName" it works fine but the reader must be
> >> returning
> >> a type that the ping does not understand.
> >>
> >> "Vadims Podans" wrote:
> >>
> >>> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> >>> if ($ping.StatusCode -eq 0) {
> >>> write-output "Ping Worked"
> >>> } Else {
> >>> write-output "Ping Failed"
> >>> }
> >>> this code works as expected. However I'm interesting about this:
> >>> $rdr.Read()
> >>> $rdr.getstring(0)
> >>>
> >>> Can you explain this?
> >>>
> >>> --
> >>> WBR, Vadims Podans
> >>> PowerShell blog - www.sysadmins.lv
> >>>
> >>> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja
> >>> ziņojumā
> >>> "news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
> >>> > The $CN contains the name of the computername which I verify by
> >>> > printing
> >>> > the
> >>> > name to the console on line 4. But I never drop down into the success
> >>> > portion of the if statement. If I replace the $CN variable in the
> >>> > ping
> >>> > statement with the name of a machine it does work correctly. I can
> >>> > only
> >>> > assume that $CN is an incorrect type for the ping commnad.
> >>> >
> >>> > Any help is appreciated.
> >>> >
> >>> >
> >>> >
> >>> > While($rdr.Read())
> >>> > {
> >>> > $CN = $rdr.getstring(0)
> >>> > $CN
> >>> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> >>> > if ($ping.StatusCode -eq 0)
> >>> > {
> >>> > write-output "Ping Worked"
> >>> > }
> >>> > Else
> >>> > {
> >>> > write-output "Ping Failed"
> >>> > }
> >>> >
> >>> > }
> >>>
> >>>
>
My System SpecsSystem Spec
Old 11-28-2008   #9 (permalink)
Vadims Podans


 
 

Re: How to ping machine names pulled from a datareader

ok. Check, what returns $ping variable.

--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv

"Jacob Sampson" <JacobSampson@xxxxxx> rakstīja ziņojumā
"news:1D5A66BC-6EC8-46B9-B159-4DA9EE1284E1@xxxxxx"...
Quote:

> I am not sure if it is possible but what seems to be happening is that the
> script moves past the ping statement before it gets a ping response. The
> machine names just fly by and the $ping.statuscode is null.
>
> "Vadims Podans" wrote:
>
Quote:

>> ooops, I'm missing a DOT:
>> ($rdr.getstring(0)).gettype().fullname
>>
>> --
>> WBR, Vadims Podans
>> PowerShell blog - www.sysadmins.lv
>>
>> "Vadims Podans" <vpodans> rakstīja ziņojumā
>> "news:#9aogfYUJHA.3668@xxxxxx"...
Quote:

>> > what type? Please, show:
>> > ($rdr.getstring(0))gettype().fullname
>> >
>> > in this command return should be something like System.String
>> >
>> > for workaround, try this:
>> > [string]$CN = $rdr.getstring(0)
>> > this tries to convert right side result to single string format.
>> >
>> > --
>> > WBR, Vadims Podans
>> > PowerShell blog - www.sysadmins.lv
>> >
>> > "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja
>> > ziņojumā
>> > "news:EB0DD698-F6C4-4ADE-9ECF-72CF98DA2881@xxxxxx"...
>> >> the sqldatareader grabs data from a database. It is only one way
>> >> though.
>> >> Meaning I can read from the database but I cant write to it like I
>> >> could
>> >> in a
>> >> dataset. In my case I only need to read the computernames in the
>> >> table.
>> >> If
>> >> I say $CN = "SomeComputerName" it works fine but the reader must be
>> >> returning
>> >> a type that the ping does not understand.
>> >>
>> >> "Vadims Podans" wrote:
>> >>
>> >>> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
>> >>> if ($ping.StatusCode -eq 0) {
>> >>> write-output "Ping Worked"
>> >>> } Else {
>> >>> write-output "Ping Failed"
>> >>> }
>> >>> this code works as expected. However I'm interesting about this:
>> >>> $rdr.Read()
>> >>> $rdr.getstring(0)
>> >>>
>> >>> Can you explain this?
>> >>>
>> >>> --
>> >>> WBR, Vadims Podans
>> >>> PowerShell blog - www.sysadmins.lv
>> >>>
>> >>> "Jacob Sampson" <JacobSampson@xxxxxx> rakstīja
>> >>> ziņojumā
>> >>> "news:362C672B-C019-41F1-8174-0FFAB110AFDC@xxxxxx"...
>> >>> > The $CN contains the name of the computername which I verify by
>> >>> > printing
>> >>> > the
>> >>> > name to the console on line 4. But I never drop down into the
>> >>> > success
>> >>> > portion of the if statement. If I replace the $CN variable in the
>> >>> > ping
>> >>> > statement with the name of a machine it does work correctly. I can
>> >>> > only
>> >>> > assume that $CN is an incorrect type for the ping commnad.
>> >>> >
>> >>> > Any help is appreciated.
>> >>> >
>> >>> >
>> >>> >
>> >>> > While($rdr.Read())
>> >>> > {
>> >>> > $CN = $rdr.getstring(0)
>> >>> > $CN
>> >>> > $ping = gwmi win32_pingstatus -f "Address = '$CN'"
>> >>> > if ($ping.StatusCode -eq 0)
>> >>> > {
>> >>> > write-output "Ping Worked"
>> >>> > }
>> >>> > Else
>> >>> > {
>> >>> > write-output "Ping Failed"
>> >>> > }
>> >>> >
>> >>> > }
>> >>>
>> >>>
>>
My System SpecsSystem Spec
Old 11-28-2008   #10 (permalink)
Jason Archer


 
 

RE: How to ping machine names pulled from a datareader

Could you try adding this line before the ping and tell us what the result is:

write-host `"Address = '$CN'`"

"Jacob Sampson" wrote:
Quote:

> The $CN contains the name of the computername which I verify by printing the
> name to the console on line 4. But I never drop down into the success
> portion of the if statement. If I replace the $CN variable in the ping
> statement with the name of a machine it does work correctly. I can only
> assume that $CN is an incorrect type for the ping commnad.
>
> Any help is appreciated.
>
>
>
> While($rdr.Read())
> {
> $CN = $rdr.getstring(0)
> $CN
> $ping = gwmi win32_pingstatus -f "Address = '$CN'"
> if ($ping.StatusCode -eq 0)
> {
> write-output "Ping Worked"
> }
> Else
> {
> write-output "Ping Failed"
> }
>
> }
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Cannot Ping Vista Machine Vista networking & sharing
Cannot Ping XP Machine Vista networking & sharing
Cannot even Ping my own machine. Vista networking & sharing
Can't ping Vista machine from XP machine? Vista networking & sharing
Cannot Ping XP-Pro machine Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46