Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining Userprofile in script 1

Status
Not open for further replies.

Enkrypted

Technical User
Sep 18, 2002
663
US
I have another question about using the Userprofile variable in a script. The script is to run a certain file on each users desktop. I have the following in my script:

Code:
UserProfilePath = oshell.ExpandEnvironmentStrings("%UserProfile%")

Else Return = WshShell.Run("""C:\Documents and Settings\%UserProfile%\Desktop\file.exe""", 1)

However it doesn't appear to accept the code. If I substitute %userprofile% with the direct userprofile name, the script works great, however this will be for hundreds of users and not feasible to hardcode all those in. What do I need to do in order to be able to use the %userprofile% environment variable within the file path?

Enkrypted
A+
 
Geates,

I tried what you posted but it still didn't appear to work properly. The script will basically open a command prompt and start a ping test. If the labeled window already exists, it will just run the ping test within the same window. Below is the code I have:

Code:
Dim ipaddy
Dim yn
Dim icount

icount=0



	do until IsValidIP(ipaddy)
		ipaddy=InputBox("Enter IP Address")
		if (ipaddy = "") then Cancelbutton
	Loop

          set WshShell = WScript.CreateObject("WScript.Shell")
If WshShell.AppActivate ("Ping") Then
          Set objShell = CreateObject("Wscript.Shell")
	  objShell.AppActivate "Modem"
'          objShell.Run "cmd.exe"
          WScript.Sleep 100
          objShell.SendKeys "^c"
          WScript.Sleep 100
          objShell.SendKeys "ping -t -l 1246 "
          objShell.SendKeys ipaddy
          objShell.SendKeys "~"
          WScript.Sleep 100

Function Cancelbutton
	 yn=MsgBox("Do you really wish to cancel?",4)
		If yn = "6" then wscript.quit
End Function


UserProfilePath = oshell.ExpandEnvironmentStrings("%UserProfile%")

Else Return = WshShell.Run("""C:\Documents and Settings\" & UserProfilePath & "\Desktop\file.exe""", 1)


End If

'This function verfies that a string contains a valid IP address.
Function IsValidIP(strIPAddress)
    Dim aTmp, field
    IsValidIP = False
    aTmp = Split(strIPAddress, ".")
    'There must be 4 fields in a valid IPv4 address
icount = icount + 1 
If icount=1 then exit function
    If UBound(aTmp) <> 3 Then msgbox "Please enter valid IP xxx.xxx.xxx.xxx"
    If UBound(aTmp) <> 3 Then Exit Function

    For Each field In aTmp
	'Verify that each field is numeric.
	If Not IsNumeric(field) Then msgbox "Please enter valid IP xxx.xxx.xxx.xxx"
	If Not IsNumeric(field) Then Exit Function

	'Verify that each field is between 0 and 255.
	If field < 0 OR field > 255 Then msgbox "Please enter valid IP xxx.xxx.xxx.xxx"
	If field < 0 OR field > 255 Then Exit Function

    Next
    IsValidIP = True
End Function

Let me know what changes need to be made to get this to work properly.

Enkrypted
A+
 
Disregard the window title name difference in the coding. I decided to change the title and missed the other one. It should be

Code:
If WshShell.AppActivate ("Ping") Then
          Set objShell = CreateObject("Wscript.Shell")
	  objShell.AppActivate "Ping"
[/code[

Enkrypted
A+
 
Changed oshell to WshShell and am getting:

Line: 35
Char: 6
Error: The system cannot find the file specified
Code: 80070002
Source: (null)

I believe it has to do with it still not understand where to seek that variable at. As mentioned, if I replace the & UserProfilePath & variable with the actual path, it works.

Enkrypted
A+
 
I ran the following debug and it correctly identified the userprofile path

Code:
Set WshShell = CreateObject("wscript.Shell") 
UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%") 
MsgBox UserProfilePath 
MsgBox UserProfilePath & "\Desktop\file.exe"

Somehow I think the code I have for the following is not in the correct syntax. If anyone can assist in correcting it, it would be greatly appreciated!

Code:
UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%")  
[b]Else Return = WshShell.Run("""C:\Documents and Settings\"" & UserProfilePath & "\Desktop\file.exe""", 1)[/b]

Enkrypted
A+
 
Code:
Else Return = WshShell.Run("[COLOR=#CC0000][highlight #FCE94F]""[/highlight][/color]" & UserProfilePath & "\Desktop\file.exe[COLOR=#CC0000][highlight #FCE94F]""[/highlight][/color]", 1)
 
Changing the line to that provides the following:

Line:29
Char: 6
Error: The system cannot find the file specified.
Code: 80070002

Enkrypted
A+
 
And what about this ?
Code:
Set WshShell = CreateObject("wscript.Shell")
UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%")
PathFile = UserProfilePath & "\Desktop\file.exe"
MsgBox qq(PathFile)
Return = WshShell.Run(qq(PathFile),1)

Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
End Function
 
@Crackoo

I copied your code and saved it as TestCode.vbs. I changed the filename from file.exe to test.vbs (it's the script I have been trying to get to work). I ran the file and it pulled up a MsgBox which displayed the full path with current user

C:\Users\Mike\Desktop\test.vbs

When I click OK on the MsgBox, it pulls up the start of the script. I enter the IP address (as dictated in the code) and when I click OK I get:

Script: C:\Users\Mike\Desktop\test.vbs
Line: 29
Char: 6
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)

According to the code, line 29 - char 6 is the following line:

Else Return = WshShell.Run("""" & UserProfilePath & "\Desktop\debug.vbs""", 1)

@Geates

If I substitute your line of code for the existing one, the script pulls up. I enter the IP address (as dictated in the code) and when I click OK I get:

Script: C:\Users\Mike\Desktop\test.vbs
Line: 30
Char: 6
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)

Enkrypted
A+
 
You mention "TestCode.vbs" and "Test.vbs", yet your error messages refer to "debug.vbs"???
 
Anyway, why a Else ?
Why define a function inside an If ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
@PHV,

I am still learning VBscript, so any input on better methods would be appreciated. I am not sure of what the best way to code that would be to get it to work properly.

Enkrypted
A+
 
Put the Else keyword before the line UserProfile = ...
Code:
...
Else
   UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%")  
   Return = WshShell.Run("""" & UserProfilePath & "\Desktop\file.exe""", 1)
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV. That worked great! Thank you all for the help!

Enkrypted
A+
 
Just a quick question about this particular script...when it's run on our Windows 2003 server it works perfectly. When it's run on a client machine with Windows 7/Windows 8, it seems to load a duplicate window. It should detect if there is an existing instance of the cmd.exe. If it doesn't detect a cmd window labeled "Modem", it opens a new cmd prompt and continues the script. If it does detect the cmd.exe process labeled modem, it should start a new ping request in the existing window. As mentioned, on Windows 7/Windows 8 based clients, it doesn't start the new ping request in the existing window. It pulls up a new cmd prompt labeled Modem and starts the ping in it instead. Any insight as to why that happens would be appreciated. The code is listed below

Code:
Dim ipaddy
Dim yn
Dim icount

icount=0



	do until IsValidIP(ipaddy)
		ipaddy=InputBox("Enter IP Address")
		if (ipaddy = "") then Cancelbutton
	Loop

          set WshShell = WScript.CreateObject("WScript.Shell")
If WshShell.AppActivate ("Modem") Then
          Set objShell = CreateObject("Wscript.Shell")
	  objShell.AppActivate "Modem"
'          objShell.Run "cmd.exe"
          WScript.Sleep 100
          objShell.SendKeys "^c"
          WScript.Sleep 100
          objShell.SendKeys "ping -t -l 1246 "
          objShell.SendKeys ipaddy
          objShell.SendKeys "~"
          WScript.Sleep 100


Else
   UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%")  
   Return = WshShell.Run("""" & UserProfilePath & "\Desktop\Modem.lnk""", 1)

          Set objShell = CreateObject("Wscript.Shell")
          WScript.Sleep 100
          objShell.SendKeys "ping -t -l 1246 "
          objShell.SendKeys ipaddy
          objShell.SendKeys "~"
          WScript.Sleep 100
End If 

'This function verfies that a string contains a valid IP address.
Function IsValidIP(strIPAddress)
    Dim aTmp, field
    IsValidIP = False
    aTmp = Split(strIPAddress, ".")
    'There must be 4 fields in a valid IPv4 address
icount = icount + 1 
If icount=1 then exit function
    If UBound(aTmp) <> 3 Then msgbox "Please enter valid IP xxx.xxx.xxx.xxx"
    If UBound(aTmp) <> 3 Then Exit Function

    For Each field In aTmp
	'Verify that each field is numeric.
	If Not IsNumeric(field) Then msgbox "Please enter valid IP xxx.xxx.xxx.xxx"
	If Not IsNumeric(field) Then Exit Function

	'Verify that each field is between 0 and 255.
	If field < 0 OR field > 255 Then msgbox "Please enter valid IP xxx.xxx.xxx.xxx"
	If field < 0 OR field > 255 Then Exit Function

    Next
    IsValidIP = True
End Function

Function Cancelbutton
	 yn=MsgBox("Do you really wish to cancel?",4)
		If yn = "6" then wscript.quit
End Function

Enkrypted
A+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top