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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2?,s - CPAU, RunAs and Reading registry

Status
Not open for further replies.

exman2k5

Technical User
May 6, 2005
34
US
Hi, i have two things i need help with.

1. Can cpau be run from within a vb file? I run this command

Set WsShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cpau -u username -p password -ex " & """wscript.exe file.vbs " & """ -profile".

I have set the file.vbs script to echo the currently logged on user and it runs fine but only reports the non-admin user. If i run from a dos batch file it reports the supplied admin as the logged on user so the script is working. Any thoughts on where i have gone wrong? I cant use a batch file for this as i am restricted to scripts that have to be encoded.

I came across this script

' Explicit variable declaration and standard globals
Option Explicit
Dim g_sComputerName
Dim g_oShell, g_oFSO, g_oNet

' Set standard globals and create global objects
Set g_oShell = CreateObject("Wscript.Shell")
Set g_oFSO = CreateObject("Scripting.FileSystemObject")
Set g_oNet = CreateObject("WScript.Network")

g_sComputerName = g_oNet.ComputerName

' in this example the command prompt is being run as the administrator
' on the local computer.
g_oShell.Run "runas /user:" & g_sComputerName & "\administrator cmd.exe"
Wscript.Sleep 500

' change YourPasswordHere to the local computer administrator account password
' the password must be in the quotation marks
g_oShell.SendKeys "YourPasswordHere" & "{ENTER}"
g_oShell.AppActivate "cmd (running as local administrator)"

This works as is but i cant get it to work with a vbs script. It seems I have a problem with this line

g_oShell.Run """runas /user:" & g_sComputerName & "\administrator & "wscript.exe c:test.vbs " """.

I get either a "specified file cannot be found" error or "expected end of statement" error.

2. My second question is it is possible to check the registry for the existence of a specified key and then terminate the script if the key is present? I cant seem to find the way to do this.

Thanks in advance for any replies.
 
strX = WshShell.RegRead("...")
If strX = "y" Then
Wscript.Quit 5
End If

there have been some post recently about WMI and EnumKey
For Each aSubKey In arrKeys
If LCase(aSubKey) = "sss" Then
Wscript.Quit 8
End If
Next

i have to say i hate exiting a script in this manor, bad style if you ask me.,,,

 
mrmovie, thanks for your reply. I cant seem to get that to work (im still learning so pls bear with me). this is what i have

Set WshShell = WScript.CreateObject("WScript.Shell")
strX = WshShell.RegRead("HKCU\Test\Patch Exists")
If strX = "y" Then
wscript.echo "test ok"
Else
wscript.echo "test not ok"
End If

The registry key is there but i only get the "test not ok" message which tells me its not seeing the key? . What have i done wrong?
 
in order for the If statement to return true then you need to have the following in the registry

Key call 'Test'
Value entry in the 'Test' key called 'Patch Exists'
The value of 'Patch Exists' must be lowercase 'y'

If you are trying to read/determine if a key exists then you should use check out the following syntax...note the additional "\"

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.RegRead("HKCU\ScriptEngine\Val") ' Read from value "Val".
WScript.Echo WshShell.RegRead("HKCU\ScriptEngine\Key\") ' Read from key "Key".

in order to return something you need the (Default Value) to be set to something i think.

 
Thanks a lot mrmovie. I've got it working now.
 
Here's another newbie question: how do i get one script to start another after it has finished? And does anyone have any solution to my cpau problem posted above? Thanks
 
at the end of the first script you could do something like

WshShell.Run "%systemdrive%\secondscript.vbs", 0, True

or you could have a controlling service that calls the scripts in order
 
Thanks again mrmovie ill try that. Do you have any thoughts on my cpau question?
 
With the RegRead script above, i get an error if the key does not exist already, it works if i create the key. Is it possible to get the script to just exit if the registry entry isnt there? I am trying to use the presence or not of a specified reg key as a control to determine if the script will run or exit with a message.
 
Have a look at the On Error instruction and the Err object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you PHV, that works. You guys are really a great help. Much appreciated.
 
Re: cpau problem. I found this script but i cant get it to work.

set objectShell = CreateObject("WScript.Shell")

program = "Cpau.exe"
params = "-u ""username"" -p ""password"""
cmd = """" & program & """" & " " & params

oShell.Run cmd, 1, true

I get either "expected end of statement" or "File not found" errors even with the file in a specified area. Can anyone help? Thanks
 
What is the point running cpau without knowing what to execute for that purpose? For instance if you want to open up a command prompt window (cmd).
[tt]
cmd=cmd & " -ex cmd"
[/tt]
Then run the line.
 
I have another question regarding running this script on startup.

wscript.echo "hello"

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName
wscript.echo struser

' Tell the user to wait

MsgBox "Text." & _
vbCrLf & vbCrLf & _
"Text." & _
"Text. " & vbCrLf & vbCrLf & "Text" & _
"click ok.", _
64, "Text"

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName

sFlagFile = "usertag.txt"
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile(sFlagFile)
sUser = oTS.ReadLine
oTS.close

My script will sucessfully run the first commands but will fail when trying to read the file "usertag.txt"; i get an error file not found and the script exits. The file in question will have been copied to the same location as the script and at the same time the script was copied. If the desktop loads and i run the script directly it sees the file and executes with no problem. I think i have to wait for the desktop to load completly but how do i do that? I have tried to add a sleep command but that doesnt work.
 
how about looping until the file is found?
Do While x = 0
If FSO.FileExists(sFlagFile) Then
x = 1
End If
Wscript.Sleep intSleep
Loop

personally i hate these types of loops from the bottom of my boots.
if the txt file is not there but the script file is there then there must be something wrong with the process which creates the txt file.
cant you copy that file locally from within the script?
 
Thanks, a previous script is copying both the script file and the text file to the c: drive and then logging off the logged on user. Here is the script that copies the files

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.GetFile("file.vbs")
MyFile.Copy ("c:\file.vbs")

If i set the script to do nothing when the user logs back on both files are where they should be. I only get an error if i try to find the text file from the script when logging on. I will give your suggestion a try, thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top