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 > VB Script

Vista - Variable not working

Reply
 
Old 04-22-2009   #1 (permalink)
NickGage


 
 

Variable not working

I have an odd one. I am trying to get a file version for a service that is
running on a remote server. What I am doing is mapping a drive to a remote
server then pulling the path of the service from the system. I am modifying
the path to work with the mapped drive. Here's the script.

strComputer = "."
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objnetwork.MapNetworkDrive "Z:", "\\"& strComputer &"\c$"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE
Name = 'PatchLink Update' ")

For Each objItem In colItems
temp_path = replace (objitem.pathname,
"C:","Z:")
next

patch_ver = objFSO.GetFileVersion(temp_path)

wscript.echo patch_ver

Here's the problem: If I echo the temp_path variable the correct string is
there. (in this case: "Z:\Program Files (x86)\PatchLink\Update
Agent\GravitixService.exe") When I pass that variable off to the
GetFileVersion call, I get a null back. Here's the kicker. If I take the
output from the echo ("Z:\Program Files (x86)\PatchLink\Update
Agent\GravitixService.exe") and hard code it in, I get the file version back.

Anyone have any idea of what is going on here?

Thanks!

Nick


My System SpecsSystem Spec
Old 04-22-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Variable not working


"NickGage" <NickGage@xxxxxx> wrote in message
news:16AB0C8C-CE1C-4712-B9F2-02A8F090ECFE@xxxxxx
Quote:

>I have an odd one. I am trying to get a file version for a service that is
> running on a remote server. What I am doing is mapping a drive to a
> remote
> server then pulling the path of the service from the system. I am
> modifying
> the path to work with the mapped drive. Here's the script.
>
> strComputer = "."
> Set objNetwork = CreateObject("Wscript.Network")
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> objnetwork.MapNetworkDrive "Z:", "\\"& strComputer &"\c$"
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\CIMV2")
> Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service
> WHERE
> Name = 'PatchLink Update' ")
>
> For Each objItem In colItems
> temp_path = replace
> (objitem.pathname,
> "C:","Z:")
> next
>
> patch_ver = objFSO.GetFileVersion(temp_path)
>
> wscript.echo patch_ver
>
> Here's the problem: If I echo the temp_path variable the correct string
> is
> there. (in this case: "Z:\Program Files (x86)\PatchLink\Update
> Agent\GravitixService.exe") When I pass that variable off to the
> GetFileVersion call, I get a null back. Here's the kicker. If I take the
> output from the echo ("Z:\Program Files (x86)\PatchLink\Update
> Agent\GravitixService.exe") and hard code it in, I get the file version
> back.
>
> Anyone have any idea of what is going on here?
>
> Thanks!
>
> Nick
>
You need to strip off the double quotes that enclose the value. For example
you can use:

temp_path = Replace(temp_path, """", "")

When you hard code a value, you enclose the string in double quotes, but
they are not part of the string. Also, depending on the service,
objItem.pathname may not be just a file name, but may include parameters.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 04-22-2009   #3 (permalink)
Jon Wallace


 
 

Re: Variable not working


Hi Nick,

OK the first thing I'm unsure of is whether you can pass . as a computer
name to the MapNetworkDrive call - I did try it and it seemed like you
couldn't. I don't believe by looking at the API call you can either -
http://msdn.microsoft.com/en-us/libr...h6(VS.85).aspx. With that in
mind, the first section of your script should probably look something like
this -

--
Dim oWshNetwork
Set oWshNetwork = CreateObject("Wscript.Network")
oWshNetwork.MapNetworkDrive "Z:", "\\" & oWshNetwork.ComputerName & "\c$"
--

The next part of your script is correct and you can indeed use . to indicate
the local machine - for testing I hard-coded it, feel free to break it
apart. I had to use the ClipBook service as I didn't have your service to
test...

--
Dim oWMIService
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set oServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE
Name = 'ClipSrv ")

Dim sTempPath
For Each oService in oServices
sTempPath = Replace(oService.PathName, "C:", "Z:")
Next
--

And then the final part of your script looks fine too - I used MsgBox
instead of ECHO so I could use WSCRIPT.EXE

--
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
MsgBox oFSO.GetFileVersion(sTempPath)
--

Here is what I think is happening. When you pass an invalid path to
GetFileVersion it gives you an invalid (or null) value back - I was having
this problem when I had an incorrect service name. The ClipBook service for
example is actually ClipSrv. In your service properties, check your service
name is correct - it's not the display name, it's the service name (above
it). It's possible that your WMI call is not actually finding the service
and thus passing a null path.

You can also try seeing what (in my name) sTempPath is before you pass it to
the GetFileVersion call - just put a message box in there or something.

Like I said, I ran your script and had no real problems other than the
slight changes above, the only time I saw similar behavior is when I had the
wrong service name.

Hope this helps,
Jon

www.insidetheregistry.com

---

"NickGage" <NickGage@xxxxxx> wrote in message
news:16AB0C8C-CE1C-4712-B9F2-02A8F090ECFE@xxxxxx
Quote:

>I have an odd one. I am trying to get a file version for a service that is
> running on a remote server. What I am doing is mapping a drive to a
> remote
> server then pulling the path of the service from the system. I am
> modifying
> the path to work with the mapped drive. Here's the script.
>
> strComputer = "."
> Set objNetwork = CreateObject("Wscript.Network")
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> objnetwork.MapNetworkDrive "Z:", "\\"& strComputer &"\c$"
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\CIMV2")
> Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service
> WHERE
> Name = 'PatchLink Update' ")
>
> For Each objItem In colItems
> temp_path = replace
> (objitem.pathname,
> "C:","Z:")
> next
>
> patch_ver = objFSO.GetFileVersion(temp_path)
>
> wscript.echo patch_ver
>
> Here's the problem: If I echo the temp_path variable the correct string
> is
> there. (in this case: "Z:\Program Files (x86)\PatchLink\Update
> Agent\GravitixService.exe") When I pass that variable off to the
> GetFileVersion call, I get a null back. Here's the kicker. If I take the
> output from the echo ("Z:\Program Files (x86)\PatchLink\Update
> Agent\GravitixService.exe") and hard code it in, I get the file version
> back.
>
> Anyone have any idea of what is going on here?
>
> Thanks!
>
> Nick
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
$Variable Is In {1,2,3,4} PowerShell
Set-Variable PowerShell
tab and variable PowerShell
Env. PATH variable not working sometimes... Vista General
How can I ensure that a variable is a built-in powershell variable? PowerShell


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