Hi,

I need to poll a number of machines on the network to determine if the
remote machine has any "set" files.
These are plain text files that are used by MS Dynamics GP, as an ini file
(structure is a little different though).

I want to read the contents of the file and put it into a database so that I
standardize or customize the GP environment for various users.

I ping the remote machine before I query it using WMI. Some machines
respond, but I am not able to connect to others.
I am logged in as the DomainAdmin, all target machines are part of this
domain.

I will post my entire code if needed. Meanwhile, the relevant snippet is
pasted below.

Thanks in advance for your help.
Regards
Habib

----------------------------------------------------

'**** check if computer is alive ****
Set objshell = CreateObject("WScript.Shell")
Set objScriptExec = objshell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut.ReadAll)


'**** if alive, connect to it ****
If InStr(strPingResults, "reply from") Then
Set objWMIService =
GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &
"\root\cimv2")
'----------Here is the problem: some machines respond, others reply
with err 429 - unable to contact remote server

'**** get a list of .SET files ****'
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile
where Extension= 'set'")

If colFiles.Count > 0 Then ' Yes, files found
For Each objFile In colFiles
FileID = FileID + 1 ' increment file counter
strPath = Left(objFile.Name, InStrRev(objFile.Name, "\") -
1) ' get the path of the file
strFile = Mid(objFile.Name, InStrRev(objFile.Name, "\") + 1,
Len(objFile.Name)) ' and just the filename

'create a temporary share on the remote machine with the
path of the file
Set objNewShare = objWMIService.Get("Win32_Share")
intResponse = objNewShare.Create(strPath, "SetFile",
FILE_SHARE, MAXIMUM_CONNECTIONS, "Temp share to grab set file.")

strRemFile = "\\" & strComputer & "\SetFile\" & strFile
'build the unc filename
Set objSetFile = objFSO.opentextfile(strRemFile, ForReading,
False) 'open file
arrFileData = Split(objSetFile.ReadAll, vbCrLf) 'read file

Re: REading a remote file using WMI; unable to connect to remote machine by Richard

Richard
Mon Mar 03 21:24:25 CST 2008

HSalim wrote:

> I need to poll a number of machines on the network to determine if the
> remote machine has any "set" files.
> These are plain text files that are used by MS Dynamics GP, as an ini file
> (structure is a little different though).
>
> I want to read the contents of the file and put it into a database so that
> I standardize or customize the GP environment for various users.
>
> I ping the remote machine before I query it using WMI. Some machines
> respond, but I am not able to connect to others.
> I am logged in as the DomainAdmin, all target machines are part of this
> domain.
>
> I will post my entire code if needed. Meanwhile, the relevant snippet is
> pasted below.
>
> Thanks in advance for your help.
> Regards
> Habib
>
> ----------------------------------------------------
>
> '**** check if computer is alive ****
> Set objshell = CreateObject("WScript.Shell")
> Set objScriptExec = objshell.Exec("ping -n 2 -w 1000 " & strComputer)
> strPingResults = LCase(objScriptExec.StdOut.ReadAll)
>
>
> '**** if alive, connect to it ****
> If InStr(strPingResults, "reply from") Then
> Set objWMIService =
> GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &
> "\root\cimv2")
> '----------Here is the problem: some machines respond, others reply
> with err 429 - unable to contact remote server
>
> '**** get a list of .SET files ****'
> Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile
> where Extension= 'set'")
>
> If colFiles.Count > 0 Then ' Yes, files found
> For Each objFile In colFiles
> FileID = FileID + 1 ' increment file counter
> strPath = Left(objFile.Name, InStrRev(objFile.Name, "\") -
> 1) ' get the path of the file
> strFile = Mid(objFile.Name, InStrRev(objFile.Name, "\") +
> 1, Len(objFile.Name)) ' and just the filename
>
> 'create a temporary share on the remote machine with the
> path of the file
> Set objNewShare = objWMIService.Get("Win32_Share")
> intResponse = objNewShare.Create(strPath, "SetFile",
> FILE_SHARE, MAXIMUM_CONNECTIONS, "Temp share to grab set file.")
>
> strRemFile = "\\" & strComputer & "\SetFile\" & strFile
> 'build the unc filename
> Set objSetFile = objFSO.opentextfile(strRemFile,
> ForReading, False) 'open file
> arrFileData = Split(objSetFile.ReadAll, vbCrLf) 'read file
>

I would add authenticationLevel=Pkt. For example:

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")

After research I found the following regarding connecting to remote
computers with WMI:

1. You cannot connect to computer running XP Home.
2. An NT computer cannot connect to OS later than W2k.
3. A W2k3 computer cannot connect to Win9x.
4. To connect to W2k Server SP4 you must set impersonation level to
Impersonate.
5. W2k computers must have SP2 to connect to XP or above.
6. W2k3 can only connect to Win9x and NT if credentials supplied.
7. To connect to XP or W2k3 you must set authentication level to Pkt.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



Re: REading a remote file using WMI; unable to connect to remote machine by HSalim[MVP]

HSalim[MVP]
Tue Mar 04 08:58:03 CST 2008

Hi Richard,
Thanks for the quick reply.
I added your suggestion but that did not work.
I'm not entirely sure how this factors into this dscussion - i.e. whether it
is a requirement or a workaround but the i was able to access the remote
machine after I enabled remote administration at the firewall. To do that,
I have to run this command (uning commandline)

netsh firewall set service remoteadmin enable

This worked on one machine at least.

Thanks again for your help.

Regards
Habib

"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:OVNKkdafIHA.3940@TK2MSFTNGP05.phx.gbl...
> HSalim wrote:
>
>> I need to poll a number of machines on the network to determine if the
>> remote machine has any "set" files.
>> These are plain text files that are used by MS Dynamics GP, as an ini
>> file (structure is a little different though).
>>
>> I want to read the contents of the file and put it into a database so
>> that I standardize or customize the GP environment for various users.
>>
>> I ping the remote machine before I query it using WMI. Some machines
>> respond, but I am not able to connect to others.
>> I am logged in as the DomainAdmin, all target machines are part of this
>> domain.
>>
>> I will post my entire code if needed. Meanwhile, the relevant snippet is
>> pasted below.
>>
>> Thanks in advance for your help.
>> Regards
>> Habib
>>
>> ----------------------------------------------------
>>
>> '**** check if computer is alive ****
>> Set objshell = CreateObject("WScript.Shell")
>> Set objScriptExec = objshell.Exec("ping -n 2 -w 1000 " & strComputer)
>> strPingResults = LCase(objScriptExec.StdOut.ReadAll)
>>
>>
>> '**** if alive, connect to it ****
>> If InStr(strPingResults, "reply from") Then
>> Set objWMIService =
>> GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &
>> "\root\cimv2")
>> '----------Here is the problem: some machines respond, others reply
>> with err 429 - unable to contact remote server
>>
>> '**** get a list of .SET files ****'
>> Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile
>> where Extension= 'set'")
>>
>> If colFiles.Count > 0 Then ' Yes, files found
>> For Each objFile In colFiles
>> FileID = FileID + 1 ' increment file counter
>> strPath = Left(objFile.Name, InStrRev(objFile.Name, "\") -
>> 1) ' get the path of the file
>> strFile = Mid(objFile.Name, InStrRev(objFile.Name, "\") +
>> 1, Len(objFile.Name)) ' and just the filename
>>
>> 'create a temporary share on the remote machine with the
>> path of the file
>> Set objNewShare = objWMIService.Get("Win32_Share")
>> intResponse = objNewShare.Create(strPath, "SetFile",
>> FILE_SHARE, MAXIMUM_CONNECTIONS, "Temp share to grab set file.")
>>
>> strRemFile = "\\" & strComputer & "\SetFile\" & strFile
>> 'build the unc filename
>> Set objSetFile = objFSO.opentextfile(strRemFile,
>> ForReading, False) 'open file
>> arrFileData = Split(objSetFile.ReadAll, vbCrLf) 'read file
>>
>
> I would add authenticationLevel=Pkt. For example:
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
> & strComputer & "\root\cimv2")
>
> After research I found the following regarding connecting to remote
> computers with WMI:
>
> 1. You cannot connect to computer running XP Home.
> 2. An NT computer cannot connect to OS later than W2k.
> 3. A W2k3 computer cannot connect to Win9x.
> 4. To connect to W2k Server SP4 you must set impersonation level to
> Impersonate.
> 5. W2k computers must have SP2 to connect to XP or above.
> 6. W2k3 can only connect to Win9x and NT if credentials supplied.
> 7. To connect to XP or W2k3 you must set authentication level to Pkt.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>



RE: REading a remote file using WMI; unable to connect to remote machi by CoreyThomasMCSEMCSAMCDBA

CoreyThomasMCSEMCSAMCDBA
Fri Mar 07 16:39:01 CST 2008

Hey Habib,

Try this function:

Function PingComputer(strComputer)
'Accepts computer name, returns bln = True or False

On Error Resume Next
'PingStatus = False 'Assume Failure
strWorkstation = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strWorkstation &
"\root\cimv2")
Set colPings = objWMIService.ExecQuery _
("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
For Each objPing In colPings
Select Case objPing.StatusCode
Case 0 PingComputer = True
Case Else PingComputer = False
End Select
Next
Set objWMIService = Nothing
Set colPings = Nothing
End Function

'==================================================================


To use:

strComputer = "remoteBox"

If PingComputer(strComputer) then
wscript.echo "Machine is on network"
else
wscript.echo "Machine is not on network"
end if


-Corey Thomas
MCSE/MCSA/MDBA

P.S. If you want more detail on the ping response, use the function below.

'==================================================================

Function PingStatus(strComputer)
'Accepts computer name, returns str = ping status

On Error Resume Next
strWorkstation = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strWorkstation &
"\root\cimv2")
Set colPings = objWMIService.ExecQuery _
("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
For Each objPing in colPings
Select Case objPing.StatusCode
Case 0 PingStatus = "Successful Ping"
Case 11001 PingStatus = "Buffer Too Small"
Case 11002 PingStatus = "Destination Net Unreachable"
Case 11003 PingStatus = "Destination Host Unreachable"
Case 11004 PingStatus = "Destination Protocol Unreachable"
Case 11005 PingStatus = "Destination Port Unreachable"
Case 11006 PingStatus = "No Resources"
Case 11007 PingStatus = "Bad Option"
Case 11008 PingStatus = "Hardware Error"
Case 11009 PingStatus = "Packet Too Big"
Case 11010 PingStatus = "Request Timed Out"
Case 11011 PingStatus = "Bad Request"
Case 11012 PingStatus = "Bad Route"
Case 11013 PingStatus = "TimeToLive Expired Transit"
Case 11014 PingStatus = "TimeToLive Expired Reassembly"
Case 11015 PingStatus = "Parameter Problem"
Case 11016 PingStatus = "Source Quench"
Case 11017 PingStatus = "Option Too Big"
Case 11018 PingStatus = "Bad Destination"
Case 11032 PingStatus = "Negotiating IPSEC"
Case 11050 PingStatus = "General Failure"
Case Else PingStatus = "Unable to determine cause of failure"
End Select
Next
Set objWMIService = Nothing
Set colPings = Nothing

End Function

'==================================================================

"HSalim[MVP]" wrote:

> Hi,
>
> I need to poll a number of machines on the network to determine if the
> remote machine has any "set" files.
> These are plain text files that are used by MS Dynamics GP, as an ini file
> (structure is a little different though).
>
> I want to read the contents of the file and put it into a database so that I
> standardize or customize the GP environment for various users.
>
> I ping the remote machine before I query it using WMI. Some machines
> respond, but I am not able to connect to others.
> I am logged in as the DomainAdmin, all target machines are part of this
> domain.
>
> I will post my entire code if needed. Meanwhile, the relevant snippet is
> pasted below.
>
> Thanks in advance for your help.
> Regards
> Habib
>
> ----------------------------------------------------
>
> '**** check if computer is alive ****
> Set objshell = CreateObject("WScript.Shell")
> Set objScriptExec = objshell.Exec("ping -n 2 -w 1000 " & strComputer)
> strPingResults = LCase(objScriptExec.StdOut.ReadAll)
>
>
> '**** if alive, connect to it ****
> If InStr(strPingResults, "reply from") Then
> Set objWMIService =
> GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &
> "\root\cimv2")
> '----------Here is the problem: some machines respond, others reply
> with err 429 - unable to contact remote server
>
> '**** get a list of .SET files ****'
> Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile
> where Extension= 'set'")
>
> If colFiles.Count > 0 Then ' Yes, files found
> For Each objFile In colFiles
> FileID = FileID + 1 ' increment file counter
> strPath = Left(objFile.Name, InStrRev(objFile.Name, "\") -
> 1) ' get the path of the file
> strFile = Mid(objFile.Name, InStrRev(objFile.Name, "\") + 1,
> Len(objFile.Name)) ' and just the filename
>
> 'create a temporary share on the remote machine with the
> path of the file
> Set objNewShare = objWMIService.Get("Win32_Share")
> intResponse = objNewShare.Create(strPath, "SetFile",
> FILE_SHARE, MAXIMUM_CONNECTIONS, "Temp share to grab set file.")
>
> strRemFile = "\\" & strComputer & "\SetFile\" & strFile
> 'build the unc filename
> Set objSetFile = objFSO.opentextfile(strRemFile, ForReading,
> False) 'open file
> arrFileData = Split(objSetFile.ReadAll, vbCrLf) 'read file
>
>
>
>
>