Script Example: Open A Remote Command Prompt

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Script Example: Open A Remote Command Prompt

Post by CyberCitizen » Thu Jun 13, 2013 4:03 am

Hi Guys,

Was having a play today & modified my remote execute script.

Working in IT, there are various times you need to support a user remotely, however may have issues contacting them for things like connecting a printer or a simple map network drive.

Using Macro Scheduler I decided to write a front interface for IntelliAdmin's Remote Execute (Similar to PSExec). The difference here is I can use Remote Execute to open a command prompt under a users login. Useful if I need to map a network drive, get a list of what network drives they currently have or even connecting a network printer.

Image

Code: Select all

Let>APP_TITLE=SAH Command Line Under User Acct
Let>MSG_HEIGHT=320
Let>MSG_WIDTH=500
Let>RP_ADMIN=1

Dialog>Dialog1
object Dialog1: TForm
  Left = 461
  Top = 119
  HelpContext = 5000
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'SAH Command Line Under User Acct'
  ClientHeight = 125
  ClientWidth = 546
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poDefault
  ShowHint = True
  OnTaskBar = True
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 8
    Width = 177
    Height = 13
    Caption = 'SAH Command Line Under User Acct'
  end
  object Label2: TLabel
    Left = 8
    Top = 32
    Width = 48
    Height = 13
    Caption = 'Hostname'
  end
  object Label3: TLabel
    Left = 160
    Top = 32
    Width = 48
    Height = 13
    Caption = 'Username'
  end
  object Label4: TLabel
    Left = 312
    Top = 32
    Width = 46
    Height = 13
    Caption = 'Password'
  end
  object Label5: TLabel
    Left = 8
    Top = 80
    Width = 88
    Height = 13
    Caption = 'Drive Letter / Path'
    Visible = False
  end
  object Hostname: TEdit
    Left = 8
    Top = 48
    Width = 145
    Height = 21
    TabOrder = 0
  end
  object Username: TEdit
    Left = 160
    Top = 48
    Width = 145
    Height = 21
    TabOrder = 1
  end
  object Password: TEdit
    Left = 312
    Top = 48
    Width = 145
    Height = 21
    PasswordChar = '*'
    TabOrder = 2
  end
  object MSButton1: tMSButton
    Left = 464
    Top = 8
    Width = 75
    Height = 33
    Caption = 'Go'
    Default = True
    TabOrder = 6
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton2: tMSButton
    Left = 464
    Top = 56
    Width = 75
    Height = 33
    Caption = 'Exit'
    ModalResult = 2
    TabOrder = 7
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSComboBox1: tMSComboBox
    Left = 8
    Top = 96
    Width = 41
    Height = 21
    TabOrder = 4
    Visible = False
    Items.Strings = (
      'A'
      'B'
      'C'
      'D'
      'E'
      'F'
      'G'
      'H'
      'I'
      'J'
      'K'
      'L'
      'M'
      'N'
      'O'
      'P'
      'Q'
      'R'
      'S'
      'T'
      'U'
      'V'
      'W'
      'X'
      'Y'
      'Z')
    ListText =
      'A'#13#10'B'#13#10'C'#13#10'D'#13#10'E'#13#10'F'#13#10'G'#13#10'H'#13#10'I'#13#10'J'#13#10'K'#13#10'L'#13#10'M'#13#10'N'#13#10'O'#13#10'P'#13#10'Q'#13#10'R'#13#10'S'#13#10'T'#13#10'U'#13#10'V' +
      #13#10'W'#13#10'X'#13#10'Y'#13#10'Z'#13#10
  end
  object Path: TEdit
    Left = 56
    Top = 96
    Width = 481
    Height = 21
    TabOrder = 5
    Visible = False
  end
  object CheckBox1: TCheckBox
    Left = 344
    Top = 72
    Width = 113
    Height = 17
    Caption = 'Map Network Drive'
    TabOrder = 3
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,CheckBox1,OnClick,CheckEnableStatus
AddDialogHandler>Dialog1,MSButton1,OnClick,EstablishConnection
AddDialogHandler>Dialog1,MSButton2,OnClick,Exit
SetDialogProperty>Dialog1,,Top,10
SetDialogProperty>Dialog1,,Left,10
SetDialogProperty>Dialog1,,ClientHeight,92
SetDialogProperty>Dialog1,Username,Text,%USER_NAME%admin
SetDialogObjectFocus>Dialog1,Hostname

Show>Dialog1,D1Result

SRT>CheckEnableStatus
  GetDialogProperty>Dialog1,CheckBox1,Checked,TickCheck
  If>TickCheck=False
    SetDialogObjectVisible>Dialog1,MSComboBox1,0
    SetDialogObjectVisible>Dialog1,Path,0
    SetDialogObjectVisible>Dialog1,Label5,0
    SetDialogProperty>Dialog1,,ClientHeight,92
    SetDialogObjectFocus>Dialog1,Hostname
  Else
    SetDialogObjectVisible>Dialog1,MSComboBox1,1
    SetDialogObjectVisible>Dialog1,Path,1
    SetDialogObjectVisible>Dialog1,Label5,1
    SetDialogProperty>Dialog1,,ClientHeight,125
    SetDialogObjectFocus>Dialog1,MSComboBox1
  Endif
END>CheckEnableStatus

SRT>EstablishConnection
  SetDialogProperty>Dialog1,MSButton1,Enabled,False
  SetDialogProperty>Dialog1,MSButton2,Enabled,False
  GetDialogProperty>Dialog1,Hostname,Text,HOSTNAME
  GetDialogProperty>Dialog1,Username,Text,USERNAME
  GetDialogProperty>Dialog1,Password,Text,PASSWORD
  GetDialogProperty>Dialog1,CheckBox1,Checked,TickCheck
  GetDialogProperty>Dialog1,Path,Text,NetPath
  GetDialogProperty>Dialog1,MSComboBox1,Text,DriveLetter
  GoSub>ExtractProgram
  If>TickCheck=False
    Label>FileCheckFalse
    IfFileExists>%TEMP_DIR%RemoteExecute.exe
      Let>RP_WAIT=1
      Let>RP_WINDOWMODE=1
      Let>ProgramPath=%systemroot%\system32\cmd.exe
      RunProgram>%TEMP_DIR%RemoteExecute.exe -cs -su -n -h %HOSTNAME% -u HAD\%USERNAME% -p %PASSWORD% %ProgramPath%
    Else
      GoSub>ExtractProgram
      Wait>1
      Goto>FileCheckFalse
    EndIf
  Else
    Label>FileCheckTrue
    IfFileExists>%TEMP_DIR%RemoteExecute.exe
      Let>RP_WAIT=1
      Let>RP_WINDOWMODE=1
      IfFileExists>%TEMP_DIR%DriveMAP.bat
        DeleteFile>%TEMP_DIR%DriveMAP.bat
      Else
      Endif
      Let>Pattern=\\$
      RegEx>Pattern,NetPath,0,matches,num,1,,NetPath
      WriteLn>%TEMP_DIR%DriveMAP.bat,,net use %DriveLetter%: "%NetPath%" /persistent:yes
      RunProgram>%TEMP_DIR%RemoteExecute.exe -c -cs -su -n -h %HOSTNAME% -u HAD\%USERNAME% -p %PASSWORD% %TEMP_DIR%DriveMAP.bat
    Else
      GoSub>ExtractProgram
      Wait>1
      Goto>FileCheckTrue
    EndIf
  EndIf
  SetDialogProperty>Dialog1,MSButton1,Enabled,True
  SetDialogProperty>Dialog1,MSButton2,Enabled,True
END>EstablishConnection

SRT>ExtractProgram
  //Extracts RemoteExecute Command Line Tool
  Let>RA=REMOVED BASE64 ENCODED STRING OTHERWISE FORUM'S WOULD HAVE A FIT.  YOU CAN MANUALLY LINK THE FILE YOURSELF OR BASE64 IT.  HERE THE FILE IS ENCODED USING BASE64 FOR SMALLER FILE SIZE, IT SAVES IT TO THE TEMP DIRECTORY ON THE COMPUTER. THERE IS NO REASON WHY YOU COULDN'T HAVE THE FILE LINKED VIA SCRIPT_DIR I JUST WANTED TO KEEP IT PORTABLE AND AS A SINGLE EXECUTABLE.  IF YOU NEED HELP WITH THIS PART PLEASE SEND ME A PM OR POST HERE.
  Base64>RA,Decode,RABinaryData
  Let>WLN_NOCRLF=1
  WriteLn>%TEMP_DIR%RemoteExecute.exe,RAwres,RABinaryData
END>ExtractProgram

SRT>Exit
  Exit>0
END>Exit

Label>Exit
DeleteFile>%TEMP_DIR%RemoteExecute.exe
DeleteFile>%TEMP_DIR%DriveMAP.bat
FIREFIGHTER

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts