#88832 - 2002-10-24 09:44 AM
Remote execute WMI
|
KiXtart_HelpDesk
Fresh Scripter
Registered: 2001-11-29
Posts: 25
Loc: Denmark
|
HELP!!
I need to execute commands remotely on a server. The script should be run by ordinary users from their workstations but the commands require Administrator priviledge.
I also need to kill processes on a remote server.
Can anyone help me?? (please )
/Brian
|
|
Top
|
|
|
|
#88833 - 2002-10-24 02:18 PM
Re: Remote execute WMI
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Hi Brian, hows it going ?
You can use WMI to execute commands on a remote server, plus kill remote processes ... but think we might need more info here.
1) The "commands" you want to run on the server - will they involve "reaching-out" to yet another resource, like a remote share. Apparently Windows NT does not like passthru type authentication - your credentials on the remote server can not be used to authenticate to another resource. Might want to read this thread:
Topic: Remote Execution - Driving me insane!
This thread also has all the code you should nedd. Plus - while checking out this thread - might study-up on whether TaskScheduler can provide what you need here. See Jen's post in that thread. Jens wrote a Kixtart package called KSMS - an SMS type thingy and you may be able to borrow code and concepts out of that. Hopefully Jens will read this thread and step in here.
2) Plus, could you elaborate a bit on how this script is going to be used ... I get the impression this is a stand-alone Admin script, that will be used by a small no. of people that will not have domain admin rights ? (As opposed to a login script, used by hundreds)
-Shawn
|
|
Top
|
|
|
|
#88834 - 2002-10-24 02:50 PM
Re: Remote execute WMI
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
this is a script to remotely push and execute a happy program called burp.
code:
$pc="computername" call \\server\share\udf.kix
copy "S:\MIS Tools\burp.exe" "\\$pc\c$$" if @error ? "error copying file" quit endif RemoteRun("c:\burp.exe",, $pc) sleep 3 if findproc($pc,"burp.exe") ? "press enter to remove burp from target" gets $k findproc($pc,"burp.exe",1) endif del "\\$pc\c$$\burp.exe"
|
|
Top
|
|
|
|
#88836 - 2002-10-24 11:20 PM
Re: Remote execute WMI
|
KiXtart_HelpDesk
Fresh Scripter
Registered: 2001-11-29
Posts: 25
Loc: Denmark
|
Thank you guys.
OK, here is the problem: We have a database where users get locked from time to time. I need to provide the users with a way to unlock themselves.
The proper way to unlock the users is to: 1. Run a CLI utility to get ProcessID of the user's server instance. 2. Kill the user's Process on the server. 3. Run a second CLI utility to unlock the user in the database 4. Delete any files created by the user.
I went with BrianTX's solution: Topic: remote execution. And it works fine.
Now I have another problem: The CLI utilities has to be provided with Administrator's username and password. How do I hide the username and password? (Setting Permissions on the script to Execute only doesn't work!!)
And here is what I have come up with:
code:
;-----Define Variables--------------------------------------------------
$UserName = @UserID $RemoteBox = "SERVER1" $ServerShr = "\\SERVER1\Unlock$" $DbWorkShr = "\\SERVER1\Work" $ServerFile = "\\SERVER1\Unlock$\PList.txt"
$DBAName = "administrator" $DBApw = "password"
;-----Find ProcessID----------------------------------------------------
Shell "CMD /C " + $ServerShr + "\DbAdmin.exe " + $RemoteBox + " -u " + $DBAName + " -p " + $DBApw + " -r >" + $ServerFile
$Nul = Open(1,$ServerFile) $UserPID = ReadLine(1) While @ERROR = 0 $Read1 = ReadLine(1) If InStr($Read1, $UserName) $PID = LTrim(SubStr($Read1, 1, 3)) Else EndIf Loop $Nul = Close(1) DEL $ServerFile
;-----Kill ProcessID----------------------------------------------------
$NameSpace = "root\cimv2" $RCommand = "C:\NTReskit\Kill.exe " + $PID
$SysLoc = CreateObject("WbemScripting.SWbemLocator") $SysSet = $SysLoc.ConnectServer($RemoteBox, $NameSpace, $DBAName, $DBApw) $SysSet.Security_.ImpersonationLevel = 3
$objProcStart = $SysSet.Get("Win32_ProcessStartup") $ProcStartInst = $objProcStart.SpawnInstance_ $ProcStartInst.Put_
$objProc = $SysSet.Get("Win32_Process") $objProcInst = $ObjProc.Methods_("Create").InParameters.SpawnInstance_ $objProcInst.CommandLine = $RCommand $objProcInst.ProcessStartupInformation = $ProcStartInst $objProcOut = $SysSet.ExecMethod("Win32_Process","Create",$objProcInst)
;-----Unlock DB User----------------------------------------------------
$RCommand = "D:\DBsearch\Bin\DBlock.exe -unlock -user " + $UserName
$SysLoc = CreateObject("WbemScripting.SWbemLocator") $SysSet = $SysLoc.ConnectServer($RemoteBox, $NameSpace, $DBAName, $DBApw) $SysSet.Security_.ImpersonationLevel = 3
$objProcStart = $SysSet.Get("Win32_ProcessStartup") $ProcStartInst = $objProcStart.SpawnInstance_ $ProcStartInst.Put_
$objProc = $SysSet.Get("Win32_Process") $objProcInst = $ObjProc.Methods_("Create").InParameters.SpawnInstance_ $objProcInst.CommandLine = $RCommand $objProcInst.ProcessStartupInformation = $ProcStartInst $objProcOut = $SysSet.ExecMethod("Win32_Process","Create",$objProcInst)
;-----Delete User Files-------------------------------------------------
$UserFile = $DbWorkShr + $UserName + ".upd"
If Exist($UserFile) $Result1 = GetFileTime($UserFile) $FileName = Dir($DbWorkShr) While $FileName <> "" AND @ERROR = 0 $Result2 = GetFileTime($DbWorkShr + $FileName) If $Result2 = $Result1 DEL $DbWorkShr + $FileName Else EndIf $FileName = Dir() Loop Else Endif
[ 26. October 2002, 18:05: Message edited by: KiXtart_HelpDesk ]
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
1 registered
(Allen)
and 781 anonymous users online.
|
|
|