IntelligensRendszerFelugyeletFeladat5
Ez az oldal a korábbi SCH wikiről lett áthozva.
Ha úgy érzed, hogy bármilyen formázási vagy tartalmi probléma van vele, akkor, kérlek, javíts rajta egy rövid szerkesztéssel!
Ha nem tudod, hogyan indulj el, olvasd el a migrálási útmutatót.
-- kispe - 2008.05.09.
Itt egy olyan script, ami lekérdezi a telepített szoftvereket egy távoli gépről
A listát XML-el formázva adja vissza
Csinálsz 2 db vmware-s XP-t, és elindítod mindkettőt úgy, hogy tudják egymást pingelni (nekem NAT-tal sikerült, de valakinek meg Bridge módban)
valamint a TÁVOLI gépen megcsinálod ezeket a beállításokat
valamint ezeket is, különös tekintettel a DCOM és a WMI részre, akkor ha futtatod a programot, megkapod a listát.
Parancssorból lehet futtatni.
következzenek a kódok:
lekérdező script (lista.vbs legyen a neve)
Note: A lekérdezés visszaadja a programokhoz tartozó UninstallStringet is (ez jól jöhet, ha pl le kell törölni)
Note2: A lekérdezés visszaadja a programok mappáját is
Note3: a scriptben át kell irni a keresési kulcsszót a saját karbantartási feladatodnak megfelelően (ami itt solitaire az nálad pl counter strike vagy pinball :D
Note4: A script tényleg letörli a mappát ha megtalálja, erősen ajánlott vmware-ben futtatni (és főleg ne teszteld host OS-en pl c:\ bemenettel, lehet h furcsán fog kinézni utána a gép
Dim strKey, strSubKey, oReg, arrSubKeys(), strDisplayKey, strDisplayVersion, strInstallLocation, strDisplayName, strUninstallString, strFolder, i
Dim arrSoftware(399) ' Allow for 400 program entries in add/remove programs
Dim pos, minone 'for string manipulation
Dim arrToDel(399), j 'list of folders to delete
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer=WScript.Arguments.Item(0) ' Change to name of computer if not local
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
Set oReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
oReg.EnumKey HKEY_LOCAL_MACHINE, strKey, arrSubKeys
i=0
j=0
minone=-1
' Read the data from the registry and load it into an array
For Each strSubKey In arrSubKeys
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKey & "\" & strSubKey, "DisplayName", strDisplayName
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKey & "\" & strSubKey, "UninstallString", strUninstallString
If not IsNull(strDisplayName) And Left(strDisplayName,1)<>"" Then
arrSoftware(i)="<name>" & replace(strDisplayName,","," ") & "</name>" ' Remove commas and replace with spaces
If not IsNull(strUninstallString) And Left(strUninstallString,1)<>"" Then
strFolder = Left(strUninstallString,InStrRev(strUninstallString, "\"))
pos=Len(strFolder)
pos=pos-1
If pos>0 Then
strFolder = Left(strFolder, pos)
End If
arrSoftware(i)=arrSoftware(i) & "<Folder>" & strFolder & "</Folder>"
If (InStr(Lcase(strFolder),"solitaire"))<>0 Then 'the name contains the required string -> solitaire
arrToDel(j)=strFolder 'store the folder
j=j+1
End If
arrSoftware(i)=arrSoftware(i) & "<Uninstall>" & replace(strUninstallString,","," ") & "</Uninstall>"
End If
i=i+1
End If
strUninstallString = vbEmpty
strDisplayName = vbEmpty
strDisplayVersion = vbEmpty
strInstallLocation = vbEmpty
Next
' bubble sort the data
For i = UBound(arrSoftware) - 1 To 0 Step -1
for j= 0 to i
if arrSoftware(j)>arrSoftware(j+1) Then
temp=arrSoftware(j+1)
arrSoftware(j+1)=arrSoftware(j)
arrSoftware(j)=temp
end If
Next
Next
' write the data and ignore empty array elements
wscript.Echo "<?xml version=" & Unescape("%22") & "1.0" & Unescape("%22") & " encoding=" & Unescape("%22") & "UTF-8" & Unescape("%22") & "?>"
wscript.Echo("<root>")
For i = 0 To UBound(arrSoftware) Step 1
If arrSoftware(i)<>"" Then
wscript.Echo("<element>")
wscript.Echo(arrSoftware(i))
wscript.Echo("</element>")
End If
Next
'list the elements to delete
For j = 0 To UBound(arrToDel) Step 1
If arrToDel(j)<>"" Then
wscript.Echo("<toDelElement>")
wscript.Echo(arrToDel(j))
wscript.Echo("</toDelElement>")
End If
Next
wscript.Echo("</root>")
'delete the necessary files
For j = 0 To UBound(arrToDel) Step 1
If arrToDel(j)<>"" Then
runotherscript "uninstall.vbs " & " " & Unescape("%22") & strComputer & Unescape("%22") & " " & Unescape("%22") & arrToDel(j) & Unescape("%22")
End If
Next
Sub runotherscript(name)
Dim oShell
Set oShell = WScript.CreateObject("WSCript.shell")
oShell.run name, 1, true
Set oShell = Nothing
end sub
Lekérdezést futtató script (lista.bat legyen a neve, 2 paramétert kap: első a távoli IP cím, második a kimenet neve)
SET IP=%1% SET OUT=%2% cscript lista.vbs %IP% > %OUT% cscript cleanup.vbs %OUT% cscript cleanup.vbs %OUT% PAUSE
Maga a lekérdezés (indíts egy parancssort és írd bele)
lista 192.168.49.132 c:\eredmeny.xml
Kimeneti fájlt formázó script (cleanup.vbs)
strInputFile = WScript.Arguments.Item(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
If Not objInputFile.AtEndOfStream Then
objInputFile.SkipLine
Else
WScript.Quit
End If
strContents = ""
While Not objInputFile.AtEndOfStream
If strContents = "" Then
strContents = objInputFile.ReadLine
Else
strContents = strContents & VbCrLf & objInputFile.ReadLine
End If
Wend
objInputFile.Close
Set objInputFile = Nothing
Set objOutputFile = objFSO.CreateTextFile(strInputFile, True)
objOutputFile.Write strContents
objOutputFile.Close
Set objOutputFile = Nothing
Set objFSO = Nothing
Nemkívánatos mappát törlő script (uninstall.vbs)
Dim arrFolders()
intSize = 0
strComputer = WScript.Arguments.Item(0) 'computer location
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strFolderName = WScript.Arguments.Item(1) 'folder to delete
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next
Sub GetSubFolders(strFolderName)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub
For i = Ubound(arrFolders) to 0 Step -1
strFolder = arrFolders(i)
strFolder = Replace(strFolder, "\", "\\")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolder & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
Next
A lekérdezéshez tartozó xsd (mostanában ez a fejlett és a menő xml struktúra definíció)
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="element"> <xs:complexType> <xs:sequence> <xs:all> <xs:element name="name" type="xs:string"/> <xs:element name="Folder" type="xs:string"/> <xs:element name="Uninstall" type="xs:string"/> </xs:all> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="toDelElement" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>