Question

  • Creator
    Topic
  • #2247044

    Bat File

    Locked

    by wizard-09 ·

    Hello All, need a bat file to complete a task, looking some help.

    I have PS tools located on the PC I need a file that will do this.


    @echo
    off

    PSKILL RVD.exe

    I need it to prompt for a computer name so


    @echo
    off

    pskill rvd.exe %computername%

    Also it will be a remote computer that needs it killed not local

    could someone help, been a while from I have writen a bat file to prompt for computer name thanks.

All Answers

  • Author
    Replies
    • #2854463

      Clarifications

      by wizard-09 ·

      In reply to Bat File

      Clarifications

    • #2854444

      Usage

      by tobif ·

      In reply to Bat File

      From the instructions to PsKill:
      Usage: pskill [- ] [-t] [\\computer [-u username] [-p password]]

      The -t to also kill dependants. That’s up to you. But next is more important: unless you’re a domain admin, you’ll probably need to provide logon credentials to the remote computer. (and if you include these admin credentials in your batch file, then you may want to guard the file with a couple of angry dogs, who can’t read).

      Anyway, I see two main ways to do what you want.
      1. Parameter, you’d include the far computername when you call your batch file: RemoteFix FarComputerName
      Then the command in the line would read something like
      pskill \\%1 rvd.exe

      2. Prompt
      To have the batch file stop and ask, use the SET /P command:
      SET /P FarComputerName=Enter remote computer name
      pskill \\%FarComputerName% rvd.exe
      Set FarComputerName=

      The last line is intended to clear the variable again.

      • #2854265

        Thanks

        by wizard-09 ·

        In reply to Usage

        Yes I am domain admin, if I want to add commands to the bat file like, push 1 for this option will do kill rvd.exe push 2 for kill vnc.exe how would I complete that would that be the %if% command, I have the below but no matter what I select it will always go to the rvd.exe no matter what option I select?

        ECHO OFF
        CLS
        :MENU
        ECHO.
        ECHO ………………………………………..
        ECHO PRESS 1, 2 or 3 to select your task, or 4 to EXIT.
        ECHO ………………………………………..
        ECHO.
        ECHO 1 – PSKILL RVD REMOTE HOST
        ECHO 2 – **KILL VNC REMOTE HOST
        ECHO 3 – RESTART VM WARE
        ECHO.
        SET /P m=Type 1, 2, or 3, then press ENTER:
        IF %m%==1 GOTO KILL RVD
        IF %m%==2 GOTO KILL VNC
        IF %m%==3 GOTO RESTART VM
        IF %m%==4 GOTO EOF

        :KILL RVD

        SET /P FarComputerName=Enter remote computer name \\
        Set FarComputerName=
        pskill \\%FarComputerName% rvd.exe

        :KILL VNC

        SET /P FarComputerName=Enter remote computer name \\
        Set FarComputerName=
        pskill \\%FarComputerName% vnc.exe

        :RESTART VM

        SET /P FarComputerName=Enter remote computer name \\
        Set FarComputerName=
        pskill \\%FarComputerName% vnc.exe

        goto
        :eof

        • #2854262

          You’re lucky

          by tobif ·

          In reply to Thanks

          It’s weekend over here, and I felt like digging into this…

          Here we go

          Menubat.bat
          ============================
          REM 2010-10-29 by TobiF

          @echo
          off
          set farcomputer=%1

          :menu
          cls
          set choicecounter=0
          echo Currently selected computer name is: %farcomputer%
          echo.
          echo Choose number and press enter
          echo.
          echo (1) Select other computer. Current is: %farcomputer%
          echo (2) Command A
          echo (3) Command B
          echo (4) Command C
          echo (0) Quit
          echo.

          :choice
          set /P menuchoice=[1,2,3,4,0] ?
          set choicecounter=%ChoiceCounter%+1
          if %menuchoice%==1 goto setname
          if %menuchoice%==2 goto commanda
          if %menuchoice%==3 goto commandb
          if %menuchoice%==4 goto commandc
          if %menuchoice%==0 goto quit
          rem If we end up here, then no choice matched. So: bounce back!
          Set /A choicecounter+=1
          if %choicecounter% gtr 5 goto menu
          goto choice

          :setname
          echo.
          set /P farcomputer=[Enter name of remote computer: ]?
          goto menu

          :commanda
          echo.
          echo this is command A/%farcomputer%
          rem do what we need to do
          pause
          goto menu

          :commandb
          echo.
          echo this is command B /%farcomputer%
          rem do what we need to do
          pause
          goto menu

          :commandc
          echo.
          echo this is command C/%farcomputer%
          rem do what we need to do
          pause
          goto menu

          :quit
          echo.
          echo Thanks for your visit. Do come again!
          set farcomputer=
          set choicecounter=
          set menuchoice=

          ======================================

        • #2854258

          Not Working

          by wizard-09 ·

          In reply to You’re lucky

          I can’t get that one to work, can we just not edit this one to go whats needed please 🙂

          ECHO OFF
          CLS
          :MENU
          ECHO.
          ECHO ………………………………………..
          ECHO PRESS 1, 2 or 3 to select your task, or 4 to EXIT.
          ECHO ………………………………………..
          ECHO.
          ECHO 1 – PSKILL RVD REMOTE HOST
          ECHO 2 – **KILL VNC REMOTE HOST
          ECHO 3 – RESTART VM WARE
          ECHO.
          SET /P m=Type 1, 2, or 3, then press ENTER:
          IF %m%==1 GOTO KILL RVD
          IF %m%==2 GOTO KILL VNC
          IF %m%==3 GOTO RESTART VM
          IF %m%==4 GOTO EOF

          :KILL RVD

          SET /P FarComputerName=Enter remote computer name \\
          Set FarComputerName=
          pskill \\%FarComputerName% rvd.exe

          :KILL RVD

          SET /P FarComputerName=Enter remote computer name \\
          Set FarComputerName=
          pskill \\%FarComputerName% vnc.exe

          :RESTART VM

          start shutdown -i

          goto
          :eof

        • #2854255

          Problems

          by tobif ·

          In reply to Not Working

          1. You can’t use the same label several times.
          (:KILL RVD)
          2. You can’t use space in label names
          (:KILL RVD)
          3. Realize that labels are just labels. Unless you enter a goto command, execution will simply continue forward in your batch file.
          If user enters anything other than [1234] then you will, in order, kill rvd, kill vnc and then shutdown… (In between nagging you for computer names)
          4. What function does a single “goto” have?
          0 (cosmetic): Echo off on the first line will turn off echoing of the batch file’s commands to the screen. But this line itself will be replicated, unless you put a @ before it, like this: “@echo off”

          But, most important, if you have no clue (and not even really try to get any clue), then maybe you should work with something completely different?

          I don’t realize how your batch file in any way would make your life easier. If would be quicker to simply enter the suitable PSKILL command directly, than:
          1. enter the batch file name
          2. select a number
          3. answer your own prompt about computer name

          Don’t you agree?

        • #2854252

          Yea

          by wizard-09 ·

          In reply to Problems

          Might as well just use the command line for it 🙂

        • #2873514

          and for the sake of your BAT skills …

          by churdoo ·

          In reply to Not Working

          Taking Tobi’s suggestions (great advice Tobi!), plus some other fixes on your original bat file …

          As Tobi stated, your lables cannot include a space, so use an underscore instead for readability
          Also I noticed you were clearing your FarComputerName environment variable before using it in the pskill commands, and some other fixes I’ve tried to explain with the REM comments.


          @ECHO
          OFF
          CLS
          :MENU
          ECHO.
          ECHO ………………………………………..
          ECHO PRESS 1, 2 or 3 to select your task, or 4 to EXIT.
          ECHO ………………………………………..
          ECHO.
          ECHO 1 – PSKILL RVD REMOTE HOST
          ECHO 2 – **KILL VNC REMOTE HOST
          ECHO 3 – RESTART VM WARE
          ECHO.
          SET /P m=Type 1, 2, or 3, then press ENTER:
          Rem I’ve had better luck with quotes around the IF comparisons, I’m not sure why
          IF “%m%”==”1” GOTO KILL_RVD
          IF “%m%”==”2” GOTO KILL_VNC
          IF “%m%”==”3” GOTO RESTART_VM
          IF “%m%”==”4” GOTO EOF
          Rem the following GOTO is in case the user enters nothing or garbage
          GOTO menu

          :KILL_RVD
          SET /P FarComputerName=Enter remote computer name \\
          pskill \\%FarComputerName% rvd.exe
          Rem if you don’t put the following GOTO, your bat will proceed to the next section
          Rem you can GOTO MENU or GOTO EOF your choice
          Rem I like the idea of leaving this BAT running in its own CMD shell all day
          Rem then you may save some typing, and you can expand functionality as needed
          goto menu

          :KILL_VNC
          SET /P FarComputerName=Enter remote computer name \\
          pskill \\%FarComputerName% vnc.exe
          goto menu

          :RESTART_VM
          start shutdown -i
          Rem or perhaps the following …
          Rem SET /P FarComputerName=Enter remote computer name \\
          Rem shutdown -r -f -t 30 -m \\%FarComputerName%
          goto menu

          :eof
          Rem clean up your variables down here once versus at every section
          Set FarComputerName=
          Set m=

        • #2873511

          I wasn’t clearing before menu

          by tobif ·

          In reply to and for the sake of your BAT skills …

          But I allowed a first farcomputername to be included as a parameter when the batch file is called.

        • #2873502

          Same code, with comments…

          by tobif ·

          In reply to You’re lucky

          Through his post, Churdoo pointed out that I didn’t document what I was doing. (I kind of thought that the structure was easy to understand without additional comments. But here we go:

          Of course, you name the file as you like, as long as the extension is .bat.

          Menubat.bat
          ============================

          @echo
          off
          REM The previous line turns off echoing of commands.
          REM @ in the beginning makes sure this very line won’t be echoed, either.
          REM 2010-10-29 by TobiF

          REM It is possible to set an initial farcomputer
          REM as parameter when this batchfile is called.
          REM like this: MenuBat FirstComputerName

          set farcomputer=%1
          REM %1 is a local variable, which expands to the first argument.

          REM Here starts the menu.
          REM Edit it for the choices you want.
          REM You can easily add some lines. It’s just text.

          :menu
          REM the :menu is a label, to allow goto statements
          REM to branch execution from here.
          REM cls will clear the screen
          cls
          REM choicecounter is initalized
          set choicecounter=0
          echo Currently selected computer name is: %farcomputer%
          echo.
          echo Choose number and press enter
          echo.
          echo (1) Select other computer. Current is: %farcomputer%
          echo (2) Command A
          echo (3) Command B
          echo (4) Command C
          echo (0) Quit
          echo.
          REM echo. gives a blank line. (Just echo gives another result.)

          REM Here starts the choice handling.
          REM We won’t print the whole menu again if the user
          REM enters an invalid choice, since the menu will still
          REM remain visible on screen.
          REM But if 5 invalid choices are made, then it’s
          REM time to clear the screen and start over.
          :choice
          set /P menuchoice=[1,2,3,4,0] ?
          REM The text after “=” is simply printed as a prompt.
          REM But do edit it to match expected input.

          REM start testing input value against options.
          REM This section needs to be edited for the options
          REM you want to test. More options=additional lines.
          REM Feel free to edit the labels further down.
          REM In that case they should be changed here, too.
          REM Remember that a label can’t contain spaces.

          if %menuchoice%==1 goto setname
          if %menuchoice%==2 goto commanda
          if %menuchoice%==3 goto commandb
          if %menuchoice%==4 goto commandc
          if %menuchoice%==0 goto quit
          rem If we end up here, then no choice matched. So: bounce back!

          REM Increment choicecounter
          Set /A choicecounter+=1

          REM Test for more than 5 invalid responses
          REM in that case, reprint the whole menu

          if %choicecounter% gtr 5 goto menu

          REM If we’re still here, then iteration is max done 5 times
          REM So we can just go back to the input again.
          goto choice

          REM After the unconditional jump, we’re in to
          REM our “subroutines”

          REM setname is to change the far computer name
          :setname
          echo.
          set /P farcomputer=[Enter name of remote computer: ]?
          goto menu

          REM Here we execute the first optional command
          REM Label (and corresponding goto, higher up)
          REM may be changed to make easier to read.
          :commanda
          echo.
          REM next line should be replaced with a real command.
          echo this is command A/%farcomputer%
          REM do what we need to do
          REM In order to see the result of the command
          REM before clearing the screen with the menu
          REM we pause for the user to hit any key.
          pause
          goto menu

          :commandb
          echo.
          echo this is command B /%farcomputer%
          rem do what we need to do
          pause
          goto menu

          :commandc
          echo.
          echo this is command C/%farcomputer%
          rem do what we need to do
          pause
          goto menu

          REM When user wants to finish, we move execution to
          REM the end of the file.

          :quit
          REM the following echo lines are just
          REM placeholders. Not needed at all.
          echo.
          echo Thanks for your visit. Do come again!
          REM Let’s clear the variables we’ve used.
          set farcomputer=
          set choicecounter=
          set menuchoice=

          ======================================

Viewing 1 reply thread