VB Using "Runas" - vbscript

I am having a heck of a time getting some syntax correct here.
I have a small VB Script which prompts for credentials and then uses those credentials to run another vb script:
set objShell = WScript.CreateObject("WScript.Shell")
strAdminName = inputBox("What is your username (Domain\Username)")
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs"" "
When I run the script everything works, except it can't find the script I am calling. I get an error stating "There is no file extension in "\xxx.xx.xxx\dfs\tumw-is\Juniper"
Obviously the problem is in the fact that there are spaces in the file location and also that I am using a DFS link. When I run this and target a VBS file with no spaces in the location it works fine.
I just can't get the syntax down to handle the DFS link or the spaces in the name. I assume it's pretty straightforward, but I just can't get it to work.
Any ideas?
Thanks
-John

Try this:
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \""\\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs\"""""

Related

Failure to run VBS script with task scheduler

we have this vbs script we use to update certain documents with SyncToy.
This is the script as it is currently written:
'--------------------------------------------------
Set oShell = CreateObject("WScript.Shell")
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" &
chr(34) & "-R"
iRC = oShell.Run(sFilePath, 0, True)
' Return with the same errorlevel as the batch file had
Wscript.Quit iRC
'---------------------------------------------------
I didn't write this script, and I have very little experience with scripting.
I have a task set up in task scheduler that runs this script anytime the device connects to a network. The script should run SyncToy and then synchronize the folder pair that is set up. I have tried running the script through command prompt with the cscript command but nothing happens as far as I can tell. At least the folders aren't syncing.
The script is running on a Windows 10 pro tablet
I have verified that the task is indeed running when it is supposed to. I'm just not sure if it is an issue with the way the script is written or if the task settings need to be changed. Is there anything wrong with the script as far as you can tell?
I was unsure whether to post this here or over in serverfault. If this doesn't belong here please move the question over to serverfault
Update: I've verified that this isn't a problem with the script. This problem apparently arose only after the update from SyncToy 2.0 to 2.1.
Thanks Guys.
There is a error with the sFilePath lines.
First, I don't know if this was originaly on a single line but it should (or add "_" before changing line).
Then, this (...)& >"-R" would not work. The ">" symbole is outside the quotes and generate a error.
If you want to execute this command: "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R,
this is the way to do this:
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" & chr(34) & " -R"
You can also add msgbox sFilePath to show a popup with the value of sFilePath.
To test/run the script, you just need to double-click on it.

VBS CurrentDirectory trouble

I have a very simple script, which only prints current directory. That's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (WshShell.CurrentDirectory)
This script is called from .exe file. It works fine until the calling executable was run directly. If I create a link to exe-file and launch it, then it runs my .vbs and it prints the directory of link, not the .exe itself! How can I fix this?
Get help from FileSystemObject, (vbscript example) :
scriptdir=CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Wscript.Echo scriptdir
OK, maybe it's somehow clumsy, but I've discovered a workable solution. The idea is simple: get full script name and a short one. Then subtract the second from the first.
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)))

Run a vbscript as an admin from itself (Nagios / NSclient)

I'll detail as much as I can so you can understand in which environment I'm working on and what I am trying to do.
I am using Nagios at work to monitor our servers. Each one of our Windows servers has NSclient++ installed on it. One of the many scripts Nagios' calling is check_updates.vbs; here 's how it goes if someone need to know:
On the Nagios' server side, we execute the following command:
/usr/lib/nagios/plugins/check_nrpe -H WindowsServerIpAddress -p 5666 -t 120 -c check_updates
When we execute that command, here is what's happening on the Windows Server:
By using the nscp service, it calls the following command, defined in the nsclient.ini file (into NSclient++ folder):
check_updates=cscript.exe //T:120 //NoLogo scripts\\check_updates.vbs
Then, it calls the check_updates.vbs script.
The local account on the Windows Server is the administrator one. We have changed nscp service's properties so that this service is called with another account, created especially for monitoring.
So, when we call the check_updates.vbs script directly on the Windows server (i.e locally), everything goes well, the script is working perfectly. But if we call it remotely, on the Nagios server, we have a simple (but deadly) error that says Permission denied.
That is why we are focusing on giving enough permissions to the monitoring-user.
After searching more and more and trying everything we could, I must say we're kinda lost right now.
The last solution I tried was to add these lines at the beginning of the script, to give the monitoring-user enough permissions to execute the script properly:
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
End if
(I found this here)
When I use this, locally I get an [error notice*] but the script is working anyway. However, when I call the script remotely, I just have a timeout after 120s.
More info: UAC is disabled on the Windows server, and the monitoring-user has full access control on the NSclient++\scripts folder. Using the nscp service with the admin account is not the solution we are seeking for this matter.
So, am I missing something here ? Do you have any idea about this ?
Thanks for the help ! :)
[*error notice]: Invalid arguments, check help with cscript.exe check_available_updates.vbs -h
If UAC is off then it's not a UAC permission error therefore don't worry about runas.
It's also unlikely to be a file permissions problem (all admins are equal unless someone made it different).
What is likely is the different environments. You have an assumption that is true for the interactive user only. You need to log what is happening in your script (see wshshell.LogEvent(intType, strMessage [,strTarget])). If your script has
on error remove next
remove it.
Mapped drives are a problem. As are environmental variables and special folders.
Try using Runas command line command with various options (eg /env) to see if you can duplicate the behaviour.
Also run
cmd /c set > c:\set.log
and compare the output from the two ways of running it (ie with your client and direct).
Thanks for your answer Tony, I've tried your suggestions, but eventually we ended up getting around the problem.
Despite we gave the monitoring-user all permissions needed to execute this script, it was not working. So here's what we've done:
We created a bat file which contains the command-line to call check_update.vbs and to write its output into a new file:
cscript.exe //T:120 //NoLogo "C:\Program Files\NSClient++\scripts\check_updates.vbs" > "C:\Program Files\NSClient++\check_update.log"
Then, we've created a scheduled task (using the TaskScheduler) which calls the bat file every day to check for new updates.
To have the correct output in Nagios, we created another vbscript which only has to read the check_update.log file and return the appropriate value to Nagios:
Const ForReading = 1
Const rOK = 0
Const rWarning = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Program Files\NSClient++\check_update.log", ForReading)
FirstChar = objFile.Read(1)
Content = objFile.ReadAll
If FirstChar = "O" Then
Wscript.Echo FirstChar + Content
Wscript.Quit(rOK)
Else
WScript.Echo FirstChar + Content
End If
Wscript.Quit(rWarning)
This vbscript is really basic, since it only reads the first letter of the file to decide which value to return. Indeed, if there are no updates available, the message will always be "OK - No patches missing".
So, by calling the bat file using the SchedulerTask, we get around the fact that the monitoring-user has not enough permissions to execute the script.

Get full path of VBScript script file when WScript not available

I'd like to get the full path to the script file I am executing. A quick google search shows me that WScript.ScriptFullName will do the trick. The problem is that I am executing this script outside of the Windows Script Host (I'm using ScriptUnit) and so if I try using that code I get the error:
Variable is undefined: 'wscript'
How do I get the full path to the current script file if I can't use WScript?
Can't you shell out, start a small script with cscript and pass the value to the parentscript throug an environmentvariable ? Can't test this since i don't have or use ScriptUnit.
Obviously the following would not work sincve you don't have Wscript with you but has ScriptUnit a similar feature to read environmentvars ?
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CScript.exe " & """" & ScriptName & """" 'this would set your environmentvar scriptpath
Set objProcessEnv = WshShell.Environment("Process")
path = objProcessEnv("scriptpath")
Or start the script from a batch or other script, record the path in a text-file and read the contents of that file in your mainscript.

Wscript.Shell Run doesn't work consistently

I'm trying to run the following bit of code in a vb6 dll:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True
The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True
It works fine. I can't figure out why exe files work and bat files don't. Any ideas?
You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:
shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus
Ditto for:
shell "calc", vbNormalFocus
You need to run cmd.exe and pass your BAT file to it.
objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True
I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.
While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.

Resources