Hello. I'm new to WMI, learning as I go. I have a script which is
supposed to check disk space on a number of remote computers.
Sometimes I get an error number back, without a description:
-2147217405. After looking around here, it seems that that means
permission is denied, or it might mean that WMI is not installed on the
remote computer. I'm not sure which. Which brings up the question -
does WMI need to be installed on the remote computer in order for a
script which uses WMI to work? I would have thought it wouldn't since
the script is not being run from the remote computer. Does it?

Another question. If -2147217405 does mean that permission is denied,
I just want to be clear about what exactly that means. Sometimes that
error comes back, but I can log on to that computer and look at the
disk space through a remote desktop connection. So if permission is
denied, how can that be?

My script is below, just in case someone had questions about what I am
doing. And just on a side note, constructive criticism is very
welcome.

Thanks,
Jennifer



On Error Resume Next

Dim CN, RS
Const HARD_DISK = 3
Const CONVERSION_FACTOR = 1048576
Dim
objWMIService,colItems,diskid,freemegabytes,diskspace,totalspace,sInsQry

Dim sErrQry
Dim sErrMsg

Set CN = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.REcordset")

sCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=True;Initial Catalog=Server_Disk_Utilization;Data Source=XXXXX"

CN.ConnectionString = sCon
CN.CommandTimeout = 180
CN.Open

sQry = "Select ServerName from Servers"
RS.Open sQry, CN

RS.MoveFirst

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Loop throug the list of servers. Server names are in the
''' Recordset called RS.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Do Until RS.EOF
StrComputer = RS("ServerName").Value

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' First try to ping the server. Server name is in strComputer.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colPings = objWMIService.ExecQuery _
("Select * From Win32_PingStatus where Address = '" &
StrComputer & "'")

For Each objStatus in colPings

If IsNull(objStatus.StatusCode) _
or objStatus.StatusCode <> 0 Then

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' If PING does not work, then log it to the errors table.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


If Err.Number <> 0 Then
Call LogError ("DriveSpace.VBS", "Try Ping", strComputer, Err.Number,
Err.Source, Err.Description)
End If
Else

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' If PING does work, then try to create a connection with
''' WMI to the Server.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' If the connection does not work, log it to the errors table.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


If Err.Number <> 0 Then
Call LogError ("DriveSpace.VBS", "Connect to Server", strComputer,
Err.Number, Err.Source, Err.Description)
End If

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' If the connection does work, get a list of drives from
''' the server.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



Set colItems = objWMIService.ExecQuery("Select * from
Win32_logicalDisk " & _
"where drivetype = " & HARD_DISK & "")

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' If getting the list of drives fails, log it to the errors table.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


If Err.Number <> 0 Then
Call LogError ("DriveSpace.VBS", "Get Drive Letter", strComputer,
Err.Number, Err.Source, Err.Description)
End If

For Each objItem In colItems

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' If getting the list of drives works, then get the drive space
''' for each drive. Write the drive space to the Server_DiskUsage
''' table.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


diskid = objitem.deviceid
freemegabytes = objitem.freespace / conversion_factor
diskspace = round(freemegabytes,0)
totalspace = round(objitem.size/ conversion_factor)

sInsQry = "Insert Into Server_DiskUsage (" & _
" CollectedDateTime, " & _
" ServerName, " & _
" DriveLetter, " & _
" Capacity, " & _
" Used) " & _
"Values ('" & _
Now & "','" & strComputer & "','" & _
diskID & "'," & TotalSpace & "," &
TotalSpace - FreeMegabytes & ")"

CN.Execute sInsQry
If Err.Number <> 0 Then
Call LogError ("DriveSpace.VBS", "Insert Into Server_DiskUsage",
strComputer, Err.Number, Err.Source, Err.Description & " " & DiskID &
" " & Now )
End If
DiskID = ""
TotalSpace = 0
FreeMegaBytes = 0
Next
End If
Next


Set objWMIService = Nothing
Set colItems = Nothing
RS.MoveNext
Loop





Sub LogError (ScriptName, Location, Server, N, S, Desc)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' LogError Subroutine - logs the errors to the Server_ErrorLog
table.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim sCon, CN
sCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=True;Initial Catalog=Server_Disk_Utilization;Data
Source=XXXXX"
Set CN = CreateObject("ADODB.Connection")
cN.ConnectionString = sCon
cN.CommandTimeout = 180
CN.Open

If Err.Number = -2147217405 Then
sErrMsg = "Permission Denied"
Else
sErrMsg = Desc
End If

sErrQry = "Insert Into Server_ErrorLog (" & _
" ErrDateTime, " & _
" Script_Name, " & _
" Script_Location, " & _
" ServerName, " & _
" ErrNumber, " & _
" ErrSource, " & _
" ErrDescription) " & _
" Values ('" & _
Now & "','" & ScriptName & "','" & Location & "','" &
Server& "','" & N & "','" & _
S & "','" & sErrMsg & "')"
CN.Execute sErrQry
Err.Clear
CN.Close
Set CN = Nothing
End Sub

RE: Does WMI have to be installed on Remote Computer? by mark

mark
Wed Mar 15 16:06:29 CST 2006

IMHO this looks like a well constructed and very vell commented script. The
wmi statements look ok, although you could benefit from creating some
variables for those long select statements in the same way as you have done
for the oledb connection string.

You may want to ech some response to screen at various stages through the
script to identify where the errors are occuring and try agains a sample
machine rather than work through a recordset before evaluting the results.

It may be that the target machines are locked down to prevent remote
quieries. Although I forget where this is set.

Are you running this from an account with administrative rights? You may
have TS access to the remote machines bu this does not imply admin access.

WMI comes as standard on w2k w2k3 and xp, addon for for NT4

Mark


"Jennifer" wrote:

> Hello. I'm new to WMI, learning as I go. I have a script which is
> supposed to check disk space on a number of remote computers.
> Sometimes I get an error number back, without a description:
> -2147217405. After looking around here, it seems that that means
> permission is denied, or it might mean that WMI is not installed on the
> remote computer. I'm not sure which. Which brings up the question -
> does WMI need to be installed on the remote computer in order for a
> script which uses WMI to work? I would have thought it wouldn't since
> the script is not being run from the remote computer. Does it?
>
> Another question. If -2147217405 does mean that permission is denied,
> I just want to be clear about what exactly that means. Sometimes that
> error comes back, but I can log on to that computer and look at the
> disk space through a remote desktop connection. So if permission is
> denied, how can that be?
>
> My script is below, just in case someone had questions about what I am
> doing. And just on a side note, constructive criticism is very
> welcome.
>
> Thanks,
> Jennifer
>
>
>
> On Error Resume Next
>
> Dim CN, RS
> Const HARD_DISK = 3
> Const CONVERSION_FACTOR = 1048576
> Dim
> objWMIService,colItems,diskid,freemegabytes,diskspace,totalspace,sInsQry
>
> Dim sErrQry
> Dim sErrMsg
>
> Set CN = CreateObject("ADODB.Connection")
> Set RS = CreateObject("ADODB.REcordset")
>
> sCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
> Info=True;Initial Catalog=Server_Disk_Utilization;Data Source=XXXXX"
>
> CN.ConnectionString = sCon
> CN.CommandTimeout = 180
> CN.Open
>
> sQry = "Select ServerName from Servers"
> RS.Open sQry, CN
>
> RS.MoveFirst
>
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' Loop throug the list of servers. Server names are in the
> ''' Recordset called RS.
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
> Do Until RS.EOF
> StrComputer = RS("ServerName").Value
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' First try to ping the server. Server name is in strComputer.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
> Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
> Set colPings = objWMIService.ExecQuery _
> ("Select * From Win32_PingStatus where Address = '" &
> StrComputer & "'")
>
> For Each objStatus in colPings
>
> If IsNull(objStatus.StatusCode) _
> or objStatus.StatusCode <> 0 Then
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' If PING does not work, then log it to the errors table.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
>
> If Err.Number <> 0 Then
> Call LogError ("DriveSpace.VBS", "Try Ping", strComputer, Err.Number,
> Err.Source, Err.Description)
> End If
> Else
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' If PING does work, then try to create a connection with
> ''' WMI to the Server.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\cimv2")
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' If the connection does not work, log it to the errors table.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
>
> If Err.Number <> 0 Then
> Call LogError ("DriveSpace.VBS", "Connect to Server", strComputer,
> Err.Number, Err.Source, Err.Description)
> End If
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' If the connection does work, get a list of drives from
> ''' the server.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
>
>
> Set colItems = objWMIService.ExecQuery("Select * from
> Win32_logicalDisk " & _
> "where drivetype = " & HARD_DISK & "")
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' If getting the list of drives fails, log it to the errors table.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
>
> If Err.Number <> 0 Then
> Call LogError ("DriveSpace.VBS", "Get Drive Letter", strComputer,
> Err.Number, Err.Source, Err.Description)
> End If
>
> For Each objItem In colItems
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' If getting the list of drives works, then get the drive space
> ''' for each drive. Write the drive space to the Server_DiskUsage
> ''' table.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
>
> diskid = objitem.deviceid
> freemegabytes = objitem.freespace / conversion_factor
> diskspace = round(freemegabytes,0)
> totalspace = round(objitem.size/ conversion_factor)
>
> sInsQry = "Insert Into Server_DiskUsage (" & _
> " CollectedDateTime, " & _
> " ServerName, " & _
> " DriveLetter, " & _
> " Capacity, " & _
> " Used) " & _
> "Values ('" & _
> Now & "','" & strComputer & "','" & _
> diskID & "'," & TotalSpace & "," &
> TotalSpace - FreeMegabytes & ")"
>
> CN.Execute sInsQry
> If Err.Number <> 0 Then
> Call LogError ("DriveSpace.VBS", "Insert Into Server_DiskUsage",
> strComputer, Err.Number, Err.Source, Err.Description & " " & DiskID &
> " " & Now )
> End If
> DiskID = ""
> TotalSpace = 0
> FreeMegaBytes = 0
> Next
> End If
> Next
>
>
> Set objWMIService = Nothing
> Set colItems = Nothing
> RS.MoveNext
> Loop
>
>
>
>
>
> Sub LogError (ScriptName, Location, Server, N, S, Desc)
>
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''' LogError Subroutine - logs the errors to the Server_ErrorLog
> table.
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
> Dim sCon, CN
> sCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
> Security Info=True;Initial Catalog=Server_Disk_Utilization;Data
> Source=XXXXX"
> Set CN = CreateObject("ADODB.Connection")
> cN.ConnectionString = sCon
> cN.CommandTimeout = 180
> CN.Open
>
> If Err.Number = -2147217405 Then
> sErrMsg = "Permission Denied"
> Else
> sErrMsg = Desc
> End If
>
> sErrQry = "Insert Into Server_ErrorLog (" & _
> " ErrDateTime, " & _
> " Script_Name, " & _
> " Script_Location, " & _
> " ServerName, " & _
> " ErrNumber, " & _
> " ErrSource, " & _
> " ErrDescription) " & _
> " Values ('" & _
> Now & "','" & ScriptName & "','" & Location & "','" &
> Server& "','" & N & "','" & _
> S & "','" & sErrMsg & "')"
> CN.Execute sErrQry
> Err.Clear
> CN.Close
> Set CN = Nothing
> End Sub
>
>

Re: Does WMI have to be installed on Remote Computer? by Jennifer

Jennifer
Thu Mar 16 10:45:19 CST 2006

Thank you for your response. It is appreciated. Just a couple of
questions to clarify things for me.

What exactly does -2147217405 mean when it is returned as an error
code?
And this is a weird thing for me to understand... I've been keeping a
close eye on results from one particular server. When I run my script
from my computer I only get results for Drive C on this server. And
what's more, the size of Drive C comes back as 76254. I'm thinking,
okay - I just don't have rights to see the other drives. But then if I
can't see drives D, E, H and Q, why can I see C? And what's more, when
someone else runs the script who does have admin rights, they get
results back for ALL the drives and yet the size for drive C comes
back at 10001. So what's up with that? Why would the size change? I
could run the script 2 minutes later and it is 76254 again.

Any thoughts on that?

Thanks again!


Re: Does WMI have to be installed on Remote Computer? by Mark

Mark
Thu Mar 16 18:21:46 CST 2006

Try capturing err.description as well as err.number. Also, you can use MSDN
to lookup error codes.

Mark

"Jennifer" <jennifer1970@hotmail.com> wrote in message
news:1142527519.364530.141520@p10g2000cwp.googlegroups.com...
> Thank you for your response. It is appreciated. Just a couple of
> questions to clarify things for me.
>
> What exactly does -2147217405 mean when it is returned as an error
> code?
> And this is a weird thing for me to understand... I've been keeping a
> close eye on results from one particular server. When I run my script
> from my computer I only get results for Drive C on this server. And
> what's more, the size of Drive C comes back as 76254. I'm thinking,
> okay - I just don't have rights to see the other drives. But then if I
> can't see drives D, E, H and Q, why can I see C? And what's more, when
> someone else runs the script who does have admin rights, they get
> results back for ALL the drives and yet the size for drive C comes
> back at 10001. So what's up with that? Why would the size change? I
> could run the script 2 minutes later and it is 76254 again.
>
> Any thoughts on that?
>
> Thanks again!
>