Disable/Enable Taskmanager with VBS - windows

Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.Run "calc" ,,true
msgbox "Calc finished"
WSHShell.Run "userinit.exe"
I need Taskmanager disabled from the beginning of the script until userinit.exe gets executed. I'm using Win 7 and i don't know how to do it. (I searched a lot but nothing worked)

You can set a registry value, using the registry editor cmdline equivalent: reg.exe. Here's an example:
WSHShell.Run("Reg.exe add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 1 /f", 0, 1)
In order to enable it, reset the same registry value: /d 0.
Details can be found in [TweakAndTrick]: Enable Task Manager disabled by Administrator or Virus in Windows.
Note that depending on your user, some registry key permissions adjustments might be necessary.

Related

How to disable Fast Boot (Fast Startup, Hybrid Boot) only once by the next shutdown?

The problem is;
Our software (setup) detects that the computer (win10), it is running on, needs to be restarted due to file operations, other setups and so on. Then we are asking for restart now or later to continue.
If the user restarts the computer immediately or later, pending operations have been done, and all is ok. But if the user only does shutdown (from start menu) and later starts the computer up again (there is a Fast Boot) pending operations would not been done. It is clear, because of fast boot...
My question is; How can i programatically say windows to disable fast boot only once by the next shutdown? I don't want disable it completely and activate it after next startup. Is it possible?
Thanks to the comment of RbMm on the comments of the answer I did this script which disables the Hybrid boot and adds a command to the RunOnce key in the registry to reenable it on the next boot.
The batch needs to be run with administrator rights:
#ECHO OFF
%COMSPEC% /C reg add "hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /F
%COMSPEC% /C REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v DisableHiberbootOnce /t REG_SZ /d "%COMSPEC% /C reg add """hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Power""" /v HiberbootEnabled /t REG_DWORD /d 1 /F
As RbMm said the HLKM RunOnce key only it's executed if one administrator logins on the computer. To fix this I run on a GPO computer login script the command runonce.exe /explorer which runs the RunOnce scripts on the HKLM registry key without the need to log in as administrator.

How to log into user account?

After someone has logged off (Start button→Logoff) for the night, at a certain time in the morning I want to have the Task Scheduler automatically log into the Windows 7 user account (the opposite of Start button→Logoff) that is password protected. The machine is 64 bit. I am doing this so systems (vbs, vba, etc.) can prep files prior to the user showing up to work.
I was pretty sure this was possible, but I can't find it anywhere online, so I am wondering if it really is possible.
I have to this in a VBScript.
For security reasons I need to log him off at night, but I figured that part out (vbs from SO):
Set = wshell = Wscript.CreateObject(Wscript.Shell")
wschell.exec("shutdown.exe -L -F") 'logoff and force shutdown programs
Edit1:
Using an answer from Sujith, I attempted to set this up. First I must point out that this is NOT a remote computer or connection. I'm trying to get the local computer to log back in to the user account.
I created logon.vbs with this vbs code:
Set = wshell = Wscript.CreateObject(Wscript.Shell")
wschell.exec("shutdown.exe -L -F") 'logoff and force shutdown programs
WScript.Sleep(5000)
computer = "computername"
username = "OTHERDOMAIN\user"
password = "password"
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set wmi = locator.ConnectServer(computer, "root\default", username, password)
wmi.Security_.ImpersonationLevel = 3
In other words, I am logging off and right back on (avoiding the Task Scheduler so things are simplified). I also tried a lot of variations on this trying to get it to work. It logs me off just fine, but nothing happens after that. I have to login manually.
I also tried the Task Scheduler setting mentioned in Sujith's answer, but it never turned on that I could tell.
Edit2: Or please tell me what you need to provide an answer?
Create a task scheduler to run a vbscript, this task can be triggered while the user logs off from his session by selecting the trigger option "On disconnect from user session" & Any user.
The vbscript which is scheduled on trigger may have the code for remote login on the machine. For the code the below links will help you.
connect to Remote server in vb script
Connecting to a Remote Server on a different domain -- how do I enter the username and password?
I don't think what you're trying to do is possible. If you can do a reboot first, you can solve it by using Windows' built-in "Auto Logon" feature.
If so, I've got some suggestions for an alternate solution for you:
Logging off users at a specified time
Follow the instructions for Assigning Logon Hours. Old article, but still valid for newer Windows Server OS.
OR
Create a script that logs off all logged on users, then create a scheduled task running as NT AUTHORITY\SYSTEM to execute it at a specified time.
Automatically rebooting and logging on with a specific username/password
Create a batch file called, say, "Enable_AutoLogon_and_Reboot.cmd" and place it in a folder, e.g. "C:\BatchFiles". Contents:
set sUsername=SOMEDOMAIN\Someusername
set sPassword=somepassword
:: Enable autologon on next boot
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v AutoAdminLogon /t REG_SZ /d 1 /f
:: Set autologon to only log on once
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v AutoLogonCount /t REG_DWORD /d 1 /f
:: Set username
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v DefaultUsername /t REG_SZ /d "%sUsername%" /f
:: Set password
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v DefaultPassword /t REG_SZ /d "%sPassword%" /f
:: Reboot
shutdown /r /t 0 /f
Replace the values of the variables at the start with the actual username and password.
Create a scheduled task running as NT AUTHORITY\SYSTEM (e.g. running daily at 06:00):
schtasks /create /tn "\CustomTasks\Enable AutoLogon and Reboot" /tr "%comspec% /c C:\BatchFiles\Enable_AutoLogon_and_Reboot.cmd" /st 06:00 /sc DAILY /ru "NT AUTHORITY\SYSTEM" /f

WScript v/s CScript

I have query related to this topic. Here is my script. I'm using below script to edit users on HP ILo board, it works fine, no error at all.
BUT here I can see cmd prompt, how do I run it in silent mode, i.e I do not want to see any cmd prompt, because I use BMC, so let it run in background, I will check the output later.
As you said I can simply change WScript to CSrcipt. But that does not work.
Any help please, please let me know where to modify.
Set wshShell = WScript.CreateObject ("WScript.Shell")
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User1.xml /l log1.txt > output1.txt"
WScript.Sleep 1*60*1000
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User2.xml /l log2.txt > output2.txt"
Set wshShell = Nothing
Wscript.quit
Regards
Use the second parameter of the .Run method
intWindowStyle Optional. Integer value indicating the appearance of
the program's window. Note that not all programs make use of this
information.
As I know nothing about BMC, I'd start with minimized (7, 6) before I'd try hidden (0).

Is is possible to have RUN AS prompt for vbscript?

I have this vbscript which changes the registry values and i want it to be able to run as another account(with admin rights) in a standard windows user account. Is it possible to code it such that when u double click on the vbscript, it will ask for yr windows account name and password then u be able to run the script with that account rights?
Thanks!
In the old days you could add a runas key to the respective file type in the registry:
reg add "HKCR\VBSFile\Shell\runas\Command" /ve /t REG_EXPAND_SZ ^
/d "\"%"SystemRoot"%\system32\wscript.exe\" \"%1\" %*" /f
which would add a Run as… entry to the context menu that would prompt you for credentials.
Unfortunately, Microsoft changed the "runas" behavior when they introduced UAC. Now the registry key adds a Run as Administrator entry to the context menu, that will work only with UAC enabled.
Sysinternals to the rescue (as always): you can re-enable the context menu entry for running as a different user with ShellRunas. Download the archive, unzip the executable to a directory in your %PATH% and run ShellRunas.exe /reg to register the program. That will add a Run as different user… context menu entry for executables only, though. To add this entry for VBScript files as well you need to add the relevant registry keys/values yourself, e.g. like this:
reg add "HKCR\VBSFile\Shell\runasuser" /ve /t REG_SZ /d "#shell32.dll,-50944" /f
reg add "HKCR\VBSFile\Shell\runasuser\command" /v DelegateExecute /t REG_SZ ^
/d "{ea72d00e-4960-42fa-ba92-7792a7944c1d}" /f
or by merging a .reg file like this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VBSFile\Shell\runasuser]
#="#shell32.dll,-50944"
[HKEY_CLASSES_ROOT\VBSFile\Shell\runasuser\command]
"DelegateExecute"="{ea72d00e-4960-42fa-ba92-7792a7944c1d}"
There is no elegant way for incorporating this in a VBScript, though. If your system has UAC enabled, you could check if your user already has admin privileges (verification method adopted from here) and otherwise re-launch the script using the ShellExecute method with the "runas" verb:
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
rc = reg.GetStringValue(&h80000003, "S-1-5-19\Environment", "TEMP", val)
If rc = 5 Then
'return code 5 == access denied
're-launch script only when it was run without arguments, so we don't go
'in circles when admin privileges can't be acquired
If WScript.Arguments.Count = 0 Then
're-launch as administrator; the additional argument is a guard to make
'sure the script is re-launched only once
CreateObject("Shell.Application").ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " relaunch", "", "runas", 1
WScript.Quit 0
Else
WScript.Echo "Cannot acquire admin privileges."
WScript.Quit 1
End If
Else
'your code here
End If
With UAC disabled, you'd need to prompt for credentials via InputBox (bad) or with a custom password dialog (better). Either way, you'd need to re-launch the script via runas.exe
Set sh = CreateObject("WScript.Shell")
sh.Run "runas /user:" & username & " cscript """ & WScript.ScriptFullName & """"
and type in the password via SendKeys (which is a bad idea in its own right).

How to make permanent system variable using BATCH or VBS?

I am making an installer which will be 100% depending on system variable such as %TEMP% or %Path%, users first double click windows.bat file.
But, How can i setup permanent system variable in Windows XP, Vista, 7, 8 using VBS or BATCH?
I tried with BATCH but in Windows XP, most users do not have setx by default, so i want to avoid using that technique. Is there better way to do that?
C:\Documents and Settings\sun>REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Ses
sion Manager\Environment" /v MyApplicationWillUsetThis /d "C:\WhatEverPathToIncl
udeHereQuestionMark"
The operation completed successfully
C:\Documents and Settings\sun>REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Ses
sion Manager\Environment" /v MyApplicationWillUsetThis /d "C:\WhatEverPathToIncl
udeHereQuestionMark"
Value MyApplicationWillUsetThis exists, overwrite(Y/N)? Y
The operation completed successfully
You can create a REG_SZ value under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment in the registry. The name of the value specifies the name of the environment variable, the value specifies the value of the environment variable.
You can also modify existing values.
In order to modify the registry you can use the RegRead and RegWrite method of the WScript.Shell object. For an example have a look at Manipulating the System Registry.
Edit: You could at first delete the existing value and recreate it afterwards.
REG DELETE "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyApplicationWillUsetThis /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyApplicationWillUsetThis /d "C:\WhatEverPathToIncludeHereQuestionMark"
How to set environment variables from VBScript
Set WshShell = CreateObject("Wscript.Shell")
' set a permanent environment variable %MyVarName% for all users
WshShell.Environment.item("MyVarName") = "MyVarValue"
If the above setting isn't permanent, try this one. This will set a permanent environment variable for all users. You can try System, User, Volatile, and Process categories with the Environment collection.
With WSHShell.Environment("SYSTEM")
.item("MyVarName") = "MyVarValue"
End With
You can also use ExpandEnvironmentStrings to read environment variables or replace them with their values in long command line type strings.
sValue = WshShell.ExpandEnvironmentStrings("%MyVarName%")
To work with the registry try
sRegKey = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyVarName"
sValue = WshShell.RegRead(sRegKey)
' don't write if no change is required.
If sValue <> sNewValue Then WshShell.RegWrite sRegKey , sNewValue

Resources