Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

Friday, April 25, 2014

Windows Script Host


Microsoft Windows Script Host (WSH) is a language-independent scripting host for Windows Script compatible scripting engines. It brings simple, powerful, and flexible scripting to the Windows 32-bit platform, allowing you to run scripts from both the Windows desktop and the command prompt.

Using Windows Scripting Host, VBScript performed tasks that the Windows shell (batch scripts) simply could not handle. Windows Script Host is ideal for non-interactive scripting needs, such as logon scripting, administrative scripting, and machine automation.

This is the link for Windows Script Host
http://msdn.microsoft.com/en-us/library/9bbdkx3k(v=vs.84).aspx


In VBScript, we can create Windows Script Host (WSH) Objects and automate manual tasks.

These are the 14 WSH Objects:

Scripting.Signer Object  
Enables an author to sign a script with a digital signature and a recipient to verify the signature's authenticity and trustworthiness.

WScript Object 
Provides access to most of the objects, methods, and properties in the WSH object model.

WshArguments Object
 Gives you access to the entire collection of command-line parameters — in the order in which they were originally entered.

WshController Object
 Exposes the method CreateScript() that creates a remote script process.

WshEnvironment Object
 Gives you access to the collection of Microsoft Windows system environment variables.

WshNamed Object
 Provides access to the named command-line script arguments within the WshArguments object.

WshNetwork Object
 Gives you access to the shared resources on the network to which your computer is connected.

WshRemote Object
 Provides access to the remote script process.

WshRemoteError Object
 Exposes the error information available when a remote script (a WshRemote object) terminates as a result of a script error.

WshScriptExec Object
 Provides status and error information about a script run with Exec, along with access to the stdIn, stdOut, and stdErr channels.

WshShell Object
 Gives you access to the native Windows shell functionality.

WshShortcut Object
 Allows you to create a shortcut programmatically.

WshSpecialFolders Object
 Allows you to access the Windows Special Folders.

WshUnnamed Object
 Provides access to the unnamed command-line script arguments within the WshArguments object.

WshUrlShortcut Object
 Allows you to create a shortcut to an Internet resource, programmatically.
 
Link: http://msdn.microsoft.com/en-us/library/a74hyyw0(v=vs.84).aspx


This is the reference page:
http://msdn.microsoft.com/en-us/library/98591fh7(v=vs.84).aspx


Wednesday, April 2, 2014

Script to load program in administrator elevated mode

One of our users needs to run a program in elevated mode. I added her in the local admin group, but she still have to right click the shortcut and select "Run as Administrator" and then click OK in the pop up UAC window. The user complained about all the clicks.

I googled it and found this script to avoid all these steps. User just double click the shortcut to the batch file and the batch file runs the program in elevated mode.


https://sites.google.com/site/eneerge/scripts/batchgotadmin

Basically this just creates a VBS Script on the fly and invokes the batch script using it. It checks to see if the current window is running as administrator by attempting to create a folder that requires administrative access. If the directory can not be created, then it invokes the UAC dialog, then closes the non-admin window. The script can also be executed from an already open administrative CLI.
 
 
========== Batch file: RunProgram.bat =================================

@echo off

:------------------------:
: BatchGotAdmin     :
:------------------------:

REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )


:UACPrompt

    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1  >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin

    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"

:--------------------------------------
REM  creat a temp batch file to run the real program
    
echo net use p: /delete /y >%temp%\runProgram-temp.bat
echo net use p: "\\Server1\program files" >>%temp%\runProgram-temp.bat
echo net use n: /delete /y >>%temp%\runProgram-temp.bat
echo net use n: \\Server1\groups >>%temp%\runProgram-temp.bat
echo p:\myProgram\myProgram.exe >>%temp%\runProgram-temp.bat
echo exit >>%temp%\runProgram-temp.bat
start /min %temp%\runProgram-temp.bat

======================= End of Batch file =======================

Thursday, March 14, 2013

Force reboot of a remote server that has hung shutting down

I had a problem to reboot a remote server. I googled it and found this article. The PSKILL command solved the issue.
********************************************

I had an issue last night when a remote server that I was applying windows updates to, hung while it was shutting down. I will still able to ping the server, and access its file shares, but was unable to get RDP access. I was cursing at this point, with the prospect of a long journey the following day to investigate and bring the server back online. I then considered what other steps I could take to try and force the server to reboot remotely.

 I used the PSTools command psexec to see if I could still get command line access to the remote server, and fortunately I could. Next I tried to force a reboot of the remote server using psexec and the shutdown command as follows, where REMOTE_SERVER_NAME is the name of the remote server that I was trying to reboot:

psexec \\REMOTE_SERVER_NAME shutdown /r /t 01

alternatively you could use:
shutdown /m \\REMOTE_SERVERNAME /r /t 01

This returned the following error:
1115 A system shutdown is in progress

This basically meant that a system shutdown was already in progress, and therefore the command was unable to force a reboot. In the end I used the pskill command to stop the winlogon service on the remote server to try and release whichever process wass causing the server to hang on shutdown. I should stress that this was a last resort, and not something that I would recommend doing unless essential:

pskill \\REMOTE_SERVER_NAME winlogon

Anyway, after another few minutes the remote server did finally restart, although there are a few other things that I should mention that happened in the process. The operating system on this machine was Windows Server 2008 R2. After the server came back up (verified by ping -t REMOTE_SERVER_NAME) I tried to RDP the box again. I was able to enter my credentials and the logon process appeared to start, but after a few seconds the following message appeared on the screen:
Please wait for the Windows Modules Installer
The machine sat like that for quite some time, and then started ‘Configuring Updates’. My RDP session then abruptly ended and the server restarted itself again. Again, when it was back up I tried to RDP the server again and received the ‘Please wait for the windows modules installer message’ for a second time. Thankfully, after a few minutes and another ‘configuring updates’ message, logon continued and ther server was back up and running. On checking the event log and windows update log I was able to verify that all the updates had installed OK, and there were no other errors worthy of note. So in summary, if you want to save yourself a long trip, to most likely press a power or reset switch, you may want to try the above first.

http://oasysadmin.com/2012/03/02/force-reboot-of-a-remote-server-that-has-hung-shutting-down/  

Wednesday, February 13, 2013

Fix Windows 7 Desktop Icons Disappearing


If you run Windows 7 you might have noticed that desktop shortcuts disappear with no apparent reason whatsoever. Turns out that System Maintenance is responsible for that. It runs as a weekly maintenance service that affects desktop shortcuts in two ways.

It will first remove desktop shortcuts that have not been used for at least three months. System Maintenance will also remove broken shortcuts on the desktop if there are more than four of them. The problem with the latter is that Windows 7 will recognize shortcuts to disconnected network locations as broken and also shortcuts that link to shell folders.

Microsoft offers two solutions to the problem. The first advice is to keep the number of broken shortcuts below four. This is not always possible due to the problems with broken shortcuts mentioned above.
The second suggestion by Microsoft is to turn off Computer Maintenance:
  • Click Start, and then click Control Panel.
  • Under System and Security, click Find and fix problems.
  • On the left navigation pane, click Change settings.
  • Set Computer Maintenance to Off.
This does however turn off all other computer maintenance related tasks like checking the system time, disk volume errors or the disk space error reports.
All the maintenance tasks are powered by scripts that are located in c:\windows\Diagnostics\Scheduled\Maintenance\. Options are to edit the scripts directly in that directory. The permissions prohibit that the files are edited by the current user.

What we are going to do is to edit the default vaule in the scripts, so the condition in the scripts will never be satisfied and the sctript will not delete anything.

Fixing Windows 7 Desktop Shortcuts Disappearing

Go to C:\Windows\diagnostics\scheduled\Maintenance

The script files are :
TS_BrokenShortcuts.ps1.
and
TS_UnusedDesktopIcons.ps1

Take Ownership option and change the security setting so we can use Notepad to edit it.

Open file TS_BrokenShortcuts.ps1
search for "(Get-ListLength $brokenStartupShortcuts)) -gt 4)"
change 4 to something absurdly large
save file

Open file TS_UnusedDesktopIcons.ps1
search for "if((Get-ListLength $unusedDesktopIcons) -gt 10)"
change 10 to something absurdly large
save file


http://www.ghacks.net/2010/03/30/fix-windows-7-desktop-shortcuts-disappearing/