Script to Shut Down client machine - RPC Server error?

Script to Shut Down client machine - RPC Server error?

This question is answered
  • Wondering if you have info detailing how to implement the above script using Grid Junction scripting function. I tried to lay in the script and then test it (after changing IP address to the client and inputting my own log in and password) but could not "test" the script - kept getting an RPC server not available error code. Any help appreciated
    Tim

Verified Answer
  • Hi Tony,

    No worries we will take little steps, let us just try and get a basic "Test" working first :)

    At a minimum the script requires 2 sets of information, the client IP (which you want to shutdown) and the credentials (with permissions to do so).

    1. Disable any firewall software including the build-in windows firewall  (temporarily) on "Box_1"
    2. Check "Box_1" IP Address that it is indeed "192.168.1.246" (ignore MAC address not needed)
    3. Per administrator name of "fred" and password of "friday13th" the entire script should look like, make the appropriate changes to the 3 bold sections (below) and copy and paste the below script into the Grid Junction Custom Action script window - press save and click on the "Test" button. Wait about 5-10 seconds and observe, verify any error messages or check that "Box_1" has been shutdown. Please remember to re-enable any firewall software too.

    '
    ' LOGOFF/REBOOT/SHUTDOWN REMOTE PC
    '********************************
    'Author: Alexander Kent
    'Date: 12/30/20008
    'Email: akent AT kentdome DOT com
    'Web: http://www.kentdome.com
    '
    'Use this script as an example how to logoff,
    'restart or shutdown a remote PC computer.
    'DO NOT RUN this shutdown code against another
    'Windows Home Sever!
    '
    'User credentials must have permissions to the WMI database,
    'and of course to be able to reboot the station.
    '
    'References: http://msdn.microsoft.com/en-us/library/9bbdkx3k(VS.85).aspx
    '
    '
    Dim strComputer, OperationMode, strUsername, strPassword

    'Remote computername or IP
    strComputer="192.168.1.246"

    'Specify the OperationMode (0 = Restart, 1 = Logoff, 2 = Shutdown)
    OperationMode = 2

    'Credentials
    strUsername = "fred"
    strPassword = "friday13th"

    'put computername into uppercase
    strComputer = UCase(strComputer)

    'Perform the operation on remote PC
    Call OperationAction

    Sub OperationAction
        On Error GoTo 0
        Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
     Set objSWbemServices = objSWbemLocator.ConnectServer _
          (strComputer, "root\cimv2", strUsername, strPassword)
     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")

    Dim OpSysSet, OpSys

    'Set PC to reboot
    If OperationMode = 0 Then
            For each OpSys in OpSysSet
                opSys.Reboot()
            Next

    'Set PC to logoff
    ElseIf OperationMode = 1 Then

            Const EWX_LOGOFF = 0
            For each OpSys in OpSysSet
                opSys.win32shutdown EWX_LOGOFF
            Next

    'Set PC to shutdown
    ElseIf OperationMode = 2 Then
            For each OpSys in OpSysSet
                opSys.Shutdown()
            Next

        End If
    End Sub
    '
    ' THIS IS THE END OF THE SCRIPT
    '

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
All Replies
  • Hi Tim,

    Yes - we will use the sample script as pointed out by Michael, but first lets walk backwards starting from the client machine you wish to shutdown programmatically.

    Step 1.
    Ensure client IP has a consistent  IP Address, either by assigning a static IP address or reserving an IP address via DHCP, say at your router level. The goal is to ensure your client machine will always have the same network address, for example: 192.168.1.101

    Step 2.
    Verify that the client IP address matches your presumed client IP address, to do so please go to Command Prompt (Start -> Run or Start -> Search -> "cmd.exe" (without the quotes) - Once you are in the command shell enter:  ipconfig /all
    You may have multiple network interface adapters so ensure you are looking at the correct one, and note you can use this step to verify/lookup your NIC (network interface card) MAC address (medium access control) as-well and use that to reserve an IP address via DHCP for step 1.
    A typical MAC address format looks like: 01:23:45:67:89:ab or sometimes represented as : 01-23-45-67-89-ab
    If you have assigned a static IP via router, a client machine system restart should request the new lease information. You may as an alternative use "ipconfig /release" to release your current IP lease to 0.0.0.0, followed by "ipconfig /flushdns" and finally "ipconfig /renew" to request a new IP address from your router (applicable if you are using DHCP to assign IP addresses).

    Step 3.
    The Grid Junction script will make use of the Windows Management Instrumentation (WMI) technology to remotely interoperate with the client machine, in our case telling it to safely shutdown. In order for this to work we need to ensure that the build-in firewall allows WMI traffic to pass-through. Please see this wiki link: http://www.homeserverland.com/wikis/gridjunction/windows-firewall-wmi.aspx

    Step 4.
    An account must exist on the client machine with the necessary rights to safely shutdown the machine. Generally speaking the Administrator or any administrative account will do the job.

    Step 5.
    Set credentials and client machine information in the Grid Junction sample script:
    'Remote computername or IP
    strComputer="10.0.0.133"

    'Specify the OperationMode (0 = Restart, 1 = Logoff, 2 = Shutdown)
    OperationMode = 2

    'Credentials
    strUsername = "Administrator"
    strPassword = "password"

    Hope this helps,

    purcellt:

    Wondering if you have info detailing how to implement the above script using Grid Junction scripting function. I tried to lay in the script and then test it (after changing IP address to the client and inputting my own log in and password) but could not "test" the script - kept getting an RPC server not available error code. Any help appreciated
    Tim

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
    Suggested by
  • I've looke at the examples on the website, but my understanding of script-writing is "0".  Exactly how should the shutdown script look like is "0".

    It would be good to have a worked out scenario to follow, for example:

    Computer named "Box_1"; IP address : "192.168.1.246", or  MAC address: 00-14-gh-87-ty-56;

    Administrator name: "fred";

    Password: "friday13th"

    What should I have in the script box? Exactly how should this worked example look for a remote PC shut down?

    I'd be very grateful for your help.  Thanks

    Tony

    • Top 100 Contributor
    • Male
  • Hi Tony,

    No worries we will take little steps, let us just try and get a basic "Test" working first :)

    At a minimum the script requires 2 sets of information, the client IP (which you want to shutdown) and the credentials (with permissions to do so).

    1. Disable any firewall software including the build-in windows firewall  (temporarily) on "Box_1"
    2. Check "Box_1" IP Address that it is indeed "192.168.1.246" (ignore MAC address not needed)
    3. Per administrator name of "fred" and password of "friday13th" the entire script should look like, make the appropriate changes to the 3 bold sections (below) and copy and paste the below script into the Grid Junction Custom Action script window - press save and click on the "Test" button. Wait about 5-10 seconds and observe, verify any error messages or check that "Box_1" has been shutdown. Please remember to re-enable any firewall software too.

    '
    ' LOGOFF/REBOOT/SHUTDOWN REMOTE PC
    '********************************
    'Author: Alexander Kent
    'Date: 12/30/20008
    'Email: akent AT kentdome DOT com
    'Web: http://www.kentdome.com
    '
    'Use this script as an example how to logoff,
    'restart or shutdown a remote PC computer.
    'DO NOT RUN this shutdown code against another
    'Windows Home Sever!
    '
    'User credentials must have permissions to the WMI database,
    'and of course to be able to reboot the station.
    '
    'References: http://msdn.microsoft.com/en-us/library/9bbdkx3k(VS.85).aspx
    '
    '
    Dim strComputer, OperationMode, strUsername, strPassword

    'Remote computername or IP
    strComputer="192.168.1.246"

    'Specify the OperationMode (0 = Restart, 1 = Logoff, 2 = Shutdown)
    OperationMode = 2

    'Credentials
    strUsername = "fred"
    strPassword = "friday13th"

    'put computername into uppercase
    strComputer = UCase(strComputer)

    'Perform the operation on remote PC
    Call OperationAction

    Sub OperationAction
        On Error GoTo 0
        Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
     Set objSWbemServices = objSWbemLocator.ConnectServer _
          (strComputer, "root\cimv2", strUsername, strPassword)
     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")

    Dim OpSysSet, OpSys

    'Set PC to reboot
    If OperationMode = 0 Then
            For each OpSys in OpSysSet
                opSys.Reboot()
            Next

    'Set PC to logoff
    ElseIf OperationMode = 1 Then

            Const EWX_LOGOFF = 0
            For each OpSys in OpSysSet
                opSys.win32shutdown EWX_LOGOFF
            Next

    'Set PC to shutdown
    ElseIf OperationMode = 2 Then
            For each OpSys in OpSysSet
                opSys.Shutdown()
            Next

        End If
    End Sub
    '
    ' THIS IS THE END OF THE SCRIPT
    '

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
  • Thanks - only other step I used was to follow your instructions in this thread to Tony in order to allow WMI through firewall. It works perfectly, smoothly and exactly how I wanted it - great job!!

    Thanks

    • Top 500 Contributor
  • Thanks Alex.  That's a great help.

    This is what happened on testing - I'd disabled Norton firewall for the test.

    • Top 100 Contributor
    • Male
  • OK this is not really that bad because it tells us that it located the client machine but that it is not able to communicate with it, are you sure the credentials are correct?

    I am not sure if the Norton firewall by default install disables the build-in Windows Firewall? might be worth to double check that under control panel too, furthermore are you using Windows Vista Home Premium?

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
  • Yup. I double and treble checked credentials.  You're quite right,, my OS is WVHP.

    I have the Windows firewall disable on the Vista PC and only Norton used.

    • Top 100 Contributor
    • Male
  • By default- Windows Vista Home Premium has the machine Administrator account disabled, let us enable that account and use that for our test :)

    1. On your Windows Vista Home Premium machine RIGHT CLICK and run the Command Prompt (cmd.exe) as ADMINISTRATOR.

    2. Run the following command:     net user administrator /active
    (This will enable the default Administrator account).

    3. Next give it a strong password via control panel user accounts -> manage accounts

    4. Try these new "Administrator" credentials in the script

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
  • I think that I'd previously enabled the administrator account when I'd installed MS Visual Studio, but did follow your suggestions step by step.

    Created a new admin account with pwd, only to get the same result on testing script.

    Thanks for your perseverance.

    Tony

    • Top 100 Contributor
    • Male
  • Hi Tony,

    Looking at your screenshot please try: " net user administrator /active "    
    without the quotes and ensure there is a whitespace between administrator and the slash /

     

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
  • The test worked after this alteration and I didn't have to make any firewall changes (to Norton Internet Secuirty) to get it to work.

    You're the best!  Thank you so much.

    Tony

    • Top 100 Contributor
    • Male
  • You are most welcome and I am glad you weren't discouraged :)

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
  • This thread was very helpful in getting 90% to the solution for a similar situation. Thanks for the info. I have one problem, if anyone can help ....

    I am running Windows7 and am limited in AV/Security packages. I am currently running Norton 360 (3.0 Beta). If I disable the firewall, the script runs smoothly, but if I enable it, it doesn't work. Obviously it's finding the right settings for WMI passthrough. I can't seem to find it ... any thoughts?

    Thanks!

    • Top 150 Contributor
    • Male
  • I am going to install Windows 7 and the Norton 360 3.0 Beta version in a virtual machine and will take a look at the WMI pass-through settings for you... (busy week) please allow 24-48 hours to hear back from me :-)

    Meanwhile if you are comfortable around GJ scripting perhaps you could quickly test if the custom actions fire during a simulated power outage too, not just via the "Test" button at your end? -received a possible bug report and are working on the new GJ 1.3.5/1.4.0 version  to improve this area.

    Thanks 

    JohnnyCanuck:

    This thread was very helpful in getting 90% to the solution for a similar situation. Thanks for the info. I have one problem, if anyone can help ....

    I am running Windows7 and am limited in AV/Security packages. I am currently running Norton 360 (3.0 Beta). If I disable the firewall, the script runs smoothly, but if I enable it, it doesn't work. Obviously it's finding the right settings for WMI passthrough. I can't seem to find it ... any thoughts?

    Thanks!

    Microsoft MVP - Windows Home Server

    • Top 10 Contributor
    • Male
Page 1 of 3 (40 items) 123