Windows scheduler unable to send command to putty - windows

I am logging into the server as Administrator to winserver2008.
I created a script called: vbscript.vbs
The purpose of this script is to auto login to linux via putty, then perform command line task.
Dim Shell
Set Shell = CreateObject("WScript.Shell")
output = Shell.Run("C:\putty.exe 1.2.3.4 9321")
wscript.sleep(500)
Shell.Sendkeys "root" & VBCrLf
wscript.sleep(30)
Shell.Sendkeys "password" & VBCrLf
wscript.sleep(30)
When I manually click on vbscript.vbs to execute it, vbscript will fill in root and password to putty.
When I use windows scheduler call to vbscript.vbs to execute it, vbscript won't fill in root and password to putty.
I suspect some permission issue.
I already set putty.exe to run as administrator, allow administrator, administrators group permission for it, but still fail to work when call via windows scheduler.
=====
I just tried with the second scenario, send 2 to the windows calculator, fail too..
testcalc.vbs
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
objShell.AppActivate "Calculator"
objShell.SendKeys "2"

Give up on trying to get SendKeys to work from a scheduled task, it's not going to happen. Instead simply pass the login and password on the command line:
output = Shell.Run("C:\putty.exe -l root -pw password 1.2.3.4 9321")
Alternatively do it with a session file and use -load.
If you are then going to execute commands over this connection then I believe you actually want plink rather than putty.

Related

How to launch .exe from vbscript on windows server 2003

I've got a script which needs to run on various different versions of windows server, including 2003. Yeah, I already KNOW it's "unsupported".
My script has to launch an executable, in a hidden window (though the code to do this is not shown below, because I was asked to cut it down to the bare minimum). I'm currently using win32_Process.Create as follows:
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("C:\myprog.exe", null, null, intProcessID)
This works ok on 2008 and 2012, but it is to be failing on 2003 with error code 3 "Insufficient privilege" returned in errReturn. It also works when run through cmd.exe, as an ordinary user, but the parent program is a service, check_mk_agent.exe, and so is not an "ordinary user". This script is run as one of check_mk_agent.exe's plugins.
I'm now going to work out how to use runas to try to simulate running it as the same user as the service runs as.
The two most common ways to launch a .exe from vbscript is WScript.Shell Run and Exec method. The main difference between the two is that you can capture the StdIn/StdOut/StdErr with the Exec method because applications are run in a child command shell.
Exec example:
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("calc")
Run example:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad " & WScript.ScriptFullName

how to create a scheduled task that runs when any user logins to the system

I want to launch an exe file of my product (C:\ClassConnect\class_server.cmd) on user login.
I tried 2 solutions ( but nothing seems to work)
Solution 1 : ( Added Startup Shortcut )
It asks the user for UAC dialog, which obviously my users will not accept as its a spy app.
Solution 2 : ( Added batch to windows scheduler so that it runs for any user)
It runs fine with the administrator account but fails for other users.
Moreover I am not able to view scheduled tasks on other users
Please help. ( I want the batch to run on startup for all users on my machine)
I would recommend you to put your class_server.cmd file in the alluser start-up folder:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
Or call your .cmd file via shortcut and runas in the start-up folder to solve the UAC problem. Follow this documentation: http://www.howtogeek.com/124087/how-to-create-a-shortcut-that-lets-a-standard-user-run-an-application-as-administrator/
After struggling my head for so many days, I finally found the answer for running the program as admin
I wrote the following batch file to run one of my system program in admin mode without UAC Popup( it auto Enters the admin password )
I wrote a batch file run.bat with following content => it then executes a vb script which waits for 5 second and keys in the password.
================run.bat Start========================
set USER_NAME="administrator"
set PASSWORD="test"
set PROGRAM_NAME="C:\\ClassConnect\\class_student.bat"
set "cm=cscript /B /nologo runas4.vbs %PASSWORD%"
%cm%
runas /profile /env /user:%USER_NAME% "%PROGRAM_NAME%"
================run.bat End========================
================runas4.vbs Start========================
Set objArgs = Wscript.Arguments
password=objArgs(0)
set WshShell = WScript.CreateObject("Wscript.Shell")
WScript.Sleep 5000
bWindowFound = WshShell.AppActivate("ClassConnect_Teacher")
WScript.Sleep 500
WshShell.SendKeys password
WshShell.SendKeys "{ENTER}"
set WshShell = nothing
================runas4.vbs End========================
The above script waits for 5 second and then enters the password for runas command thus I am able to run the script in admin mode.
If you are not sure about your access rights, download the isadmin.exe from internet.
if you do not have admin access on the system , activate the default disabled Administrator account. You can activate the account by using
net user administrator /active:yes
For resetting the default administrator password use:
net user administrator *

How can I run the vbscript as the 'Administrator' but access on APPDATA folder of current logon user VBSCRIPT

I tried to access on the APPDATA folder, which does function with the following code perfectly (run as the current logon user):
Set objShellApp = CreateObject("Shell.Application")
Const ssfAPPDATA = &H1A
sAppDataPath = objShellApp.NameSpace(ssfAPPDATA).Self.Path
The result of sAppDataPath is: C:\Users\ Peter \AppData\Roaming
Now the problem is, I have to run the Script as the Administrator account. And if I run this script as the Administrator on Clients, than the result is allways following:
C:\Users\ Administrator \AppData\Roaming
How can I change that? I want, that he should take the Appdata folder path of the current logon user. In spite of running the script as the Administrator. And i have to run the script as Administrator because of some permission.
How can I realize that?
Cannot do this with a VBS alone.
You would need to create a batch file to pass this %APPDATA% environment variable into the script as Argument, then modify the VBS script to process this argument(s).
Sample below:
UPDATE:
If you do not mind people being able to open the batch file and know the password of the user account you intend to Run As, get psexec.exe from MS with the updated batch file contents (assuming psexec.exe stored in C:\SysInt\):
VBS File
Dim sTxt
sTxt = WScript.Arguments.Count & " arguments passed into vbs:"
sTxt = sTxt & JoinArgs
wscript.echo sTxt
Function JoinArgs()
Dim sTmp
sTmp = ""
For Each oArg In Wscript.Arguments
sTmp = sTmp & vbCrLf & oArg
Next
JoinArgs = sTmp
End Function
Batch File (UPDATED)
#echo off
C:\SysInt\psexec.exe -u %computername%\administrator -p AccountPassword -e wscript.exe "c:\debug\vbs\test.vbs" %appdata%

Task scheduler and vbs using a user account

I am trying to schedule a vbs script to run with regular user rights. The script runs fine when logged in as the user, but when I try to run the script from the task scheduler as "Run whether user is logged on or not", it gets stuck on the following line:
Set IE = CreateObject("InternetExplorer.Application")
I have tried running it with "Run with highest privileges" checked and unchecked. I am running the program from task scheduler as:
program/script: "c:\windows\system32\cscript.exe"
arguments: "test.vbs"
start in: c:\
Here is the full code:
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
set tfo = fso.createTextFile("c:\123.txt")
tfo.writeline("1")
Set IE = CreateObject("InternetExplorer.Application")
tfo.writeline("2")
tfo.close
output when ran as "Run only when user is logged on":
1
2
output when ran as "Run whether user is logged on or not":
1
additionally, the task will run correctly as "Run whether user is logged on or not" when using an admin account, but I cannot use an admin account as a solution.
You need to grant the user the "Log on as a batch job" privilege. This can be done either via GUI:
start gpedit.msc
navigate to Computer Configuration → Windows Settings → Security Settings → Local Policies → User Righst Assignment
double-click "Log on as a batch job" privilege
add the user account
click "OK" and close gpedit.msc
or on the commandline:
ntrights +r SeBatchLogonRight -u domain\username
ntrights.exe is part of the Windows Server 2003 Resource Kit Tools, but works on Windows 7 too. You don't have to install the whole package. Instead you can use e.g. 7-zip to open/unpack the rktools.msi inside the rktools.exe.
Edit: Since you already did that, the issue is probably that the script can't spawn a GUI application, because you don't have an interactive desktop when the user isn't logged on. Try adding some debugging code to your script:
...
On Error Resume Next
Set IE = CreateObject("InternetExplorer.Application")
If Err Then tfo.writeline Err.Number & vbTab & Err.Description
On Error Goto 0
...
A test-run of this code snippet gave me a "permission denied" error. Apparently limited users cannot create an IE instance in a scheduled task.
That said, what are you trying to achieve with the Internet Explorer object? Using an XMLHttpRequest might be a better approach for background tasks.

How to set System EnvVar when UAC is active (Windows Script Host)

I have to create a script which updates a system environment variable (based on a command line parameter) before launching a program.
In Windows 7, updating the system environment variable is denied. I would like to perform a privilege elevation for just the setting of the env. var. But run the program as a normal user.
How to do it?
Note:
I've tried the following solution:
Using 2 scripts:
1 master which get all information from command line, which call the slave script to change the system env. var., and which finally launch the program
1 slave script that update the system env. var.
the master script tries to call the slave script using privilege elevation, but that does not work
I've try 2 solutions for the privilage elevation:
Using the "runas /User:Administrator ..." command but it ask for the Administrator password: Fail
Using the "ShellExecute ...., "runas"" command but it tells me that my script is not an application: Fail
I found a way that is working at least on Windows 7 (don't know if it will work on the few Windows XP hat we still have around).
I did the following from the main script:
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set UAC = WScript.CreateObject("Shell.Application")
UAC.ShellExecute "wscript.exe", currentDirectory + "my-script.vbs /Param1:Value1 ...", "", "runas", 0
And the my-script is doing the sys var env update.
Note: My fist experience with ShellExecute failed because I was trying to execute the script. Instead of "wscript.exe" I had "my-script.vbs" for the executable name.
IMHO, disable UAC, it's just a pain in the *
But if you can't (like me 8<), you can use
psexec.exe -d -u userid -p password CMD /c program_with_path
You (or the user where the sript runs) will have to confirm the prompt though.

Resources