Solved

Shut Down a Remote servers -Script

Posted on 2012-03-20
2
233 Views
Last Modified: 2012-03-26
Hello,

My question I have virtual server that is connected to a UPS via USB and I want to turn off 5 servers in case that the fall supply voltage.

I have managed with VBScript turn off the servers but only an administrator's password is the same to all of the servers:

'This Scrip Will Work Only if the Root password is the same'
strComputer = "Enter Remote Computer Name"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
        strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Shutdown(1)
Next

Open in new window


Can anyone advise me where to continue???

Thanks
ItzhakBM
0
Comment
Question by:itzhakbm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 78

Accepted Solution

by:
arnold earned 500 total points
ID: 37741967
http://msdn.microsoft.com/en-us/library/windows/desktop/aa389290(v=vs.85).aspx#vbs_example1


Not too sure what you are asking. The link above has an example where the credentials are included in a script.
0
 

Author Comment

by:itzhakbm
ID: 37762256
Hi,

Very much helped to understand ,It is new for VBscript

It took me a few days to understand the script.

but i did not manage to run the script automatic .


Dim strComputer, RestartMode, debug, strUsername, strPasswd

Debug = 1

'declare arguments variable
Set objArgs = WScript.Arguments

'if there are command-line arguments, then...
If objargs.Count Then

For I = 0 to objArgs.Count - 1
'If debug = 1 Then wscript.echo "argument: " & objargs(i)

'check to find the computername that is specified
If InStr(1,LCase(objargs(I)),"computer:") Then
On Error Resume Next
'split into an array, using : as a delimiter
strMDArray = split(objargs(I),":") 
'set strcomputer to be what follows the ':'
strComputer = strMDArray(1)
'Next, find what action you wish to perform on strcomputer 
ElseIf InStr(1,LCase(objargs(I)),"action:") Then
'split into array again
strActionArray = split(objargs(I),":")
'set restartmode to what follows the ':'
restartmode = Trim(LCase(strActionArray(1)))
' Sam Add Some code start here
ElseIf InStr(1,LCase(objargs(I)),"User:") Then
'split into array again
strActionArray = split(objargs(I),":")
'set restartmode to what follows the ':'
strUsername = Trim(LCase(strActionArray(1)))

ElseIf InStr(1,LCase(objargs(I)),"Password:") Then
'split into array again
strActionArray = split(objargs(I),":")
'set restartmode to what follows the ':'
strPasswd = Trim(LCase(strActionArray(1)))
' Sam Add Some code end here
End If
Next
'call chkcomputername to verify that there is something
' entered.
Call ChkComputerName
'if the word reboot appears in the restartmode var, then...
If InStr(LCase(restartmode),"reboot") Then 
    RestartMode = 0
ElseIf InStr(LCase(restartmode),"logoff") Then 
RestartMode = 2
ElseIf InStr(LCase(restartmode),"shutdown") Then
    RestartMode = 1
Else
'if the restartmode variable is not correct, then quit
wscript.echo "You must use the following syntax when defining an action: reboot|logoff|shutdown" & vbcrlf _
& vbcrlf & "Invalid syntax: [" & restartmode & "] now quitting."    
wscript.quit
End If
Else
'if no command-line parameters were passed, then prompt with inputbox
' first for the computername 
strComputer=InputBox("Perform restart action on what computer?","Enter Computer Name",strComputer)
'find out if computername is valid
Call ChkComputerName
'prompt for restartmode using inputbox
RestartMode = InputBox("I would like to perform the following action on " & strComputer & ":" & vbcrlf & vbcrlf _ 
& "0 - Restart " & strComputer & vbcrlf _
& "1 - Logoff " & strComputer & vbcrlf _
& "2 - Shutdown " & strComputer & vbcrlf _ 
& vbcrlf,"Restart action",RestartMode)
'if no restartmode was specified, then quit
If RestartMode = "" Then 
wscript.echo "No restart action was specified, now quitting."
wscript.quit
'if restartmode does not fall between 0 and 2, then quit
ElseIf RestartMode < 0 or Restartmode > 2 Then
wscript.echo "You must select a valid option between 0 and 3. Script will now exit."
wscript.quit
End If

End If

'put computername into uppercase
strComputer = UCase(strComputer)

'Call actual code to restart the PC
Call RestartAction

Sub ChkComputerName
'if no computername is specified (blank), then quit
If strComputer = "" Then 
wscript.echo "You must define a computer name after the computer: argument. None " _
& "were specified, now quitting."
wscript.quit
'if computername contains two backslashes, then quit
ElseIf InStr(strComputer,"\\") Then
wscript.echo "Please specify the computer name without the leading backslashes. " _
& "Now quitting." 
wscript.quit
End If
End Sub

Sub RestartAction
    On Error GoTo 0
         
' Sam Comment out some lines of code, and add a prompt box to get the username and password input from user.
    ' ===== Sam added & modified codes start here =====  
strUsername=InputBox("Please input remote user's account?","Enter the account",strUsername)  
    strPasswd=InputBox("Please input remote user's password?","Enter the password",strPasswd)
	Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objSWbemServices = objSWbemLocator.ConnectServer _
    		(strComputer, "root\cimv2", strUsername, strPasswd)
	objSWbemServices.Security_.ImpersonationLevel = 3 
	
'Call WMI query to collect parameters for reboot action
    'Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//"_ 
     '& strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem"_
     '& " where Primary=true") 

Set OpSysSet = objSWbemServices.ExecQuery("select * from Win32_OperatingSystem"_
    & " where Primary=true") 
    
 ' ===== Sam added & modified code end here =====

Dim OpSysSet, OpSys
    If debug = 1 Then wscript.echo "Computer: " & strComputer & vbcrlf _
     & "Restart Action: " & RestartMode
     
'set PC to reboot
If RestartMode = 0 Then

        For each OpSys in OpSysSet 
            opSys.Reboot()
        Next 

'set PC to logoff
ElseIf RestartMode = 1 Then
        
        Const EWX_LOGOFF = 0 
        For each OpSys in OpSysSet 
            opSys.win32shutdown EWX_LOGOFF
        Next 

'set PC to shutdown
ElseIf RestartMode = 2 Then
                
        For each OpSys in OpSysSet 
            opSys.Shutdown()
        Next 
                
    End If
End Sub 

Open in new window




Thanks
ItzhakBM
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
This article covers how to install the Microsoft Windows Operating System (OS). What is covered in this article:  > Different Versions and Editions of the Windows OS  > Upgrading versus Fresh Installation of the OS           - Steps to take pr…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question