#97605 - 2003-02-05 01:56 AM
Re: is there a "RemoteComputerFromUser" UDF out there
|
JimRobinson
Fresh Scripter
Registered: 2003-01-10
Posts: 42
Loc: Tempe, AZ
|
I'm not sure if this still belongs here, but here is a KiXtart solution to Rocco's RemoteComputerFromUser question. I don't know if this would be a good one to turn into a UDF, but I do think it could make a nice little KiXform.
And I probably redid someone else's work with the InStrArray() UDF, based as it is on jpols variation of sealeopard's IsInArray(). But I really needed to find partial string matches, AScan and IsInArray matched only full strings, and I couldn't find anything else.
Please post improvements, as I am sure there are several.
code:
;locate.kix ;Queries WINS for a supplied username and if found ;attempts to resolve it to a hostname. ; ;Requirements ;A functioning WINS environment. ;Windows 2000 or XP (not tested on any other OS). ;winscl.exe from the Windows 2000 Resource Kit. ;winsrpc.dll from a Windows 2000 server. Both in your path. ;the UDFs WshPipe() ;and InStrArray() ; ;Usage ;kix32 locate.kix $user="username" ;or a batch file like this: ; ;@echo off ;if "%1"=="" ( ; call :usage) else ( ; kix32.exe %0\..\locate.kix $user=%1 ;) ;GoTo :eof ; ;:usage ;echo/Usage: Locate username ;GoTo :eof
;Set some variables. break on $WINSserver = "YOUR_WINS_SERVER_ADDRESS_HERE" $ShortOffset = Len($WINSserver) $LongOffset = ($ShortOffset+25) $file = "%0\..\winscl.txt" $IP="Not Found" del $file
;Write the command file used by winscl.exe and output the username $X = open (1, "$file", 5) $X = writeline(1, "$WINSserver" + @CRLF) $X = writeline(1, "QN" + @CRLF) $X = writeline(1, "$user" + @CRLF) $X = writeline(1, "1" + @CRLF) $X = writeline(1, "03" + @CRLF) $X = writeline(1, "0" + @CRLF) $X = writeline(1, "EX" + @CRLF) cls "Username: " $user ?
;Populate $WINSArray with the results of winscl.exe ;Assign the variables holding the offset to the record in WINSArray ;containing the IP address, if it exists. Output the IP or Not Found. $WINSarray=WshPipe("%comspec% /c winscl.exe T NOME< winscl.txt",1) $X = InStrArray("Address is (", $WINSarray) $Y = InStrArray("Member is (", $WINSarray) Select Case $X <> -1 $SrchLen = (LEN($WINSarray[$x])-($ShortOffset+1)) $IP = SubStr($WINSarray[$x],$ShortOffset, $SrchLen ) "Has IP: " $IP ? Case $Y <> -1 $SrchLen = (LEN($WINSarray[$y])-($LongOffset+1)) $IP = SubStr($WINSarray[$y],$LongOffset, $SrchLen ) "Has IP: " $IP ? Case 1 "Was Not Found in WINS" ? EndSelect
;Resolve the hostname using ping -a. Output the hostname or Not Found. $HostArray = WshPipe("%comspec% /c ping -a -n 1 $IP",1) $X = InStrArray("Pinging", $HostArray) if $X <> -1 $YArray = Split($HostArray[1],"") if $YArray[1] = $IP "On Host: Not Resolved" ? else "On Host: " $YArray[1] ? endif else "Hostname Not Found in WINS" ? endif exit
;function InStrArray($string, $array) ;Looks for $string in $array using InStr() ; ;Returns the offset or -1 is not found.
function InStrArray($string, $array) if vartype($array) >= 8192 for $i = 0 to ubound($array) if instr($array[$i],$string) $InStrArray = $i return endif next endif $InStrArray = -1 return endfunction
; ;Function WshPipe() ; ;Author Christopher Shilt (christopher.shilt@relizon.com) ; ;Version 1.1 ; ;Version History ; ; 14 June 2002 Version 1.1 ; Removed ReadAll Function and incorporated it into WshPipe. Added ; $WshErrorMsg to return the actual error message generated by the ; shelled command. ; ; 14 June 2002 Version 1.0 ; ;Action Performs a shell-like command and pipes the output to an array and returns the ; exitcode of the command to the @error macro. ; ;Syntax WshPipe(COMMAND, optional NOECHO) ; ;Parameters ; COMMAND : REQUIRED. The "shell" command you want to perform. ; ; NOECHO : OPTIONAL. Suppress the command's output to the screen, ouput ; is still stored in an array. ; ;Remarks ; ; ;Returns Output of shell command in an array, exitcode of the command in the @error ; macro, and the error message of the command to $WshErrorMsg. By default, the output ; is echoed to the screen but can be suppressed. ; ;Dependencies KiX 4.02 ; WSH 5.6 (Included with Microsoft Internet Explorer 6.0. Also available for download ; from Microsoft's MSDN website.) ; ;Example: ; ; Display all KiX files in C:\ directory: ; $rc=WshPipe("%comspec% /c dir c:\*.kix") ; ? "Exit Code: " @error " " $WshErrorMsg ; ; ; Display all KiX files in C:\ directory, but suppress output to screen: ; $rc=WshPipe("%comspec% /c dir c:\*.kix",1) ; ? "Exit Code: " @error " " $WshErrorMsg ; ; ; Display all KiX files in C:\ directory, suppress output to screen. Then use FOR/NEXT ; ; to exclude data. ; $rc=WshPipe("%comspec% /c dir c:\*.kix",1) ; for each $line in $rc ; if not instr($line, "File Not Found") ; ? $line ; endif ; next ; ? "Exit Code: " @error " " $WshErrorMsg ; ;Source Function WshPipe($ShellCMD, OPTIONAL $NoEcho) Dim $WshShell, $oExec, $AllOutput, $Exit, $WshExitCode $WshErrorMsg="" $WshShell=CreateObject("WScript.Shell") $oExec=$WshShell.Exec($ShellCMD) While $Exit<>1 Dim $Output Select Case Not $oExec.StdOut.AtEndOfStream $Output=$oExec.StdOut.ReadAll Case Not $oExec.StdErr.AtEndOfStream $Output=$oExec.StdErr.ReadAll $WshErrorMsg = $Output Case 1 $Output=-1 EndSelect If $Output=-1 If $oExec.Status=1 $Exit=1 Endif Else If $NoEcho<>1 ? $Output Endif $AllOutput = $AllOutput + $Output Endif Loop $WshExitCode=$oExec.ExitCode $WshPipe=split($AllOutput,chr(10)) Exit($WshExitCode) EndFunction
Jim
_________________________
Jim --------------------
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 657 anonymous users online.
|
|
|