Page 2 of 2 <12
Topic Options
#95067 - 2006-04-20 07:58 PM Re: Trigger Your Logon Script - WMI
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
I've been revisiting this as a project for streaming updates to RAS users using robocopy...

Here is my development code including display text checkpoints... client use of course would either remove or redirect those statements.

Code:

break on

$asSubnetList="192.168.0.0/255.255.0.0",""

$sServer = 'server'
$Source = '\\server\share\hotfixes\WinXP'
$dest = 'c:\temp'
$Switches = ' /zb /IPG:700 /R:3 /w:3 >nul'

$logonscript = "cmd /c KiX32 \\server\netlogon\kixtart.kix"
$MsgboxTitle = "Connected to Domain"



While 1
$Query = "SELECT TargetInstance.Name FROM __InstanceOperationEvent WITHIN 4 WHERE TargetInstance ISA 'Win32_NetworkAdapterConfiguration'"
$objEvents = GetObject("winmgmts:\\.\root\cimv2").ExecNotificationQuery($query)
? 'waiting for network state change'
$objConnectEvent = $objEvents.nextevent
? 'State has changed'
$aIPAddress = $objConnectEvent.TargetInstance.IPAddress
For Each $sAddress in $aIPAddress
If VarType($sAddress)=8
for each $sSubnet in $asSubnetList
if instr($sSubnet,'/')
$network=split($sSubnet,'/')
$bFoundMatch = moan($sAddress,$network[0],$network[1])
? iif($bFoundMatch,'ip address on network '+$sAddress,'no address match')
endif
next
EndIf
Next
If $bFoundMatch=1
sleep 3
? 'pinging '+$sServer
$Res = wmiPing($sServer)
if not @error
? $sServer +' is available'
$script = Messagebox("Run Logon Script now",$MsgboxTitle , 36, 10)
if $script = 6
Run $logonscript
endif
if ingroup("\\@wksta\Administrators") ;Do not run section if not local admin
? 'Checking for installed patches'
$arrkey = arrEnumKey('HKLM\SOFTWARE\Microsoft\Updates\Windows XP\SP3')
? 'Checking available updates'
$arrdir = FileList($source,'.exe')
if not @error
$reboot = 0
for each $dir in $arrdir
$installed = 0
$parsed = split($dir,'-')[1]
for each $key in $arrkey
if $key = $parsed $installed = 1 endif
next
if not $installed
$RoboCommand = '%comspec% /c "'+@scriptdir+'\robocopy.exe" '+$source+'\ '+$dest+' '+$dir+$Switches
md $dest
? 'copying '+$dir
shell $RoboCommand
if not @error > 3
$_=sendmessage(@wksta,"A CRTICAL upgrade will start in 30 seconds. Please close any programs.")
sleep 10
$instFile = '"'+$dest +'\'+ $dir+'"'
? 'Installing '+$instfile
; shell '%comspec% /c ' + $instFile + ' /passive /norestart'
if not @error
$_=sendmessage(@wksta,'The update completed sucessfully')
$reboot = 1
sleep 5
del $instFile
else
$_=sendmessage(@wksta,'An error occured installing the update')
endif
endif
endif
next
else
? "files or dir of "+$source+" not available"
endif
if $reboot
; ShutDown ('', 'Updates have been applied that require your computer to restart', 5, 1, 1)
; quit
endif
endif
endif
EndIf
?
Loop


;****************************************************************************************************
;Function will return an error value if registry key does not exist
function arrenumkey($regkey)
dim $Keylist, $c
if not keyexist($regkey) exit 87 endif
do
$Key = $Key+'|'+enumkey($regkey,$c)
$c = $c + 1
until @error
$arrenumkey = split(substr($Key,2,len($Key)-2),'|')
Endfunction
;****************************************************************************************************
;Function returns array of all files in a folder, similar to a DOS DIR command
Function FileList($folderName,optional $mask, optional $path)
Dim $objDir, $fullpath, $t, $objfile
$objDir = CreateObject("Scripting.FileSystemObject")
if not @error ; usually folder not found
$files=$objDir.GetFolder($folderName).Files
$Fullpath=iif($path,$foldername+'\','')
For Each $objFile In $files
$name=$objFile.name
if ($mask and instr($name,$mask)) or not $mask
$t=$t+'|'+$Fullpath+$name
endif
Next
$FileList=split(substr($t,2),'|')
endif
exit iif(len($t)>1,0,1)
EndFunction

Function moan(optional $a,optional $b,optional $c)
Dim $,$d,$e,$g,$h,$m[3]

;Build Subnet Mask
for $e=8 to 31
$=$e
$h=1
for $g=0 to 3
$d=0,128,192,224,240,248,252,254
$m[$g]=iif($/8,255,$d[$ mod 8])
$d="."
$=($>8)*($-8)
if $a & $b
$h=$h & split($b,$d)[$g]=(0+split($a,$d)[$g] & $m[$g])
endif
next
if $h
$c=$c+" /"+$e
endif
if $e=$c
$c=join($m,$d)
endif
next
;End build mask array
if instr($c,$d)
;build array of input values by splitting a string. Then convert to integers
; Mask = $[0..3] NetID = $[4..7] IPaddr = $[8..11]
$=split($c+$d+$b,$d)
$c=""
if $a
;Calculate NetworkID using $ array

for $e=0 to 3
$c=$c+$d+(0+$[$e] & 0+split($a,$d)[$e])
next
if $d+join(split($a),"")=$c
$c=""
endif

;Output: 1 if the IP is a member of a nework defined by network ID and subnet mask, otherwise 0
;Input NetworkID = Calculated NetworkID
if $b
$c=$d+join(split($b),"")=$c
endif
else
;Output: Network ID or empty string if no match
;Output: Array of all potential IP addresses or empty string if no match
for $b=$[4] to 255 + $[4] - $[0]
for $h=$[5] to 255 + $[5] - $[1]
for $e=$[6] to 255 + $[6] - $[2]
for $a=1+$[7] to 254 + $[7] - $[3]
$c = $c+" "+$b+$d+$h+$d+$e+$d+$a
next
next
next
next
endif
endif $=$c,split(substr($c,2))
$moan=$[len($c)>1]
EndFunction

Function wmiPing($Address,Optional $Timeout,Optional $BlockSize)
Dim $Query,$oWMI,$oItem,$cItems

$Query = "Select ResponseTime,StatusCode From Win32_PingStatus Where Address='" + $Address + "'"
If $Timeout
$Query = $Query + " And TimeOut=" + $Timeout
EndIf
If $BlockSize
$Query = $Query + " And BufferSize=" + $BlockSize
EndIf
$oWMI = GetObject("winmgmts:root\cimv2")
$cItems = $oWMI.ExecQuery($Query)
For Each $oItem In $cItems
If (VarTypeName($oItem.StatusCode) = 'Null') Or $oItem.StatusCode
Exit 1
Else
$wmiPing = $oItem.ResponseTime
EndIf
Next
EndFunction

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#201784 - 2011-03-25 02:55 AM Re: Trigger Your Logon Script - WMI [Re: Sealeopard]
Alucard_Kakashi Offline
Fresh Scripter

Registered: 2009-02-05
Posts: 37
Loc: USA
this is awesome, i'm working on something like at my job. thanks
Top
#201799 - 2011-03-26 12:28 PM Re: Trigger Your Logon Script - WMI [Re: Alucard_Kakashi]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
iprange is not an option with all vpn-connections...
need to monitor availability of the servers instead.

but this is a way good start. thanks guys!
_________________________
!

download KiXnet

Top
Page 2 of 2 <12


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (mole) and 598 anonymous users online.
Newest Members
Raoul, Timothy, Jojo67, MaikSimon, kvn317
17875 Registered Users

Generated in 0.036 seconds in which 0.016 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org