Archive for the ‘Windows Script Host’ Category

Aggiungere a un file con WSH

August 10, 2007

Dim objFileSystem, objOutputFile
Dim strOutputFile

Const OPEN_FILE_FOR_APPENDING = 8

‘ generate a filename base on the script name
strOutputFile = “./writefile.out”

Set objFileSystem = CreateObject(“Scripting.fileSystemObject”)
Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, OPEN_FILE_FOR_APPENDING)

objOutputFile.WriteLine(“Hello world (” & Now & “)”)
objOutputFile.Close

Set objFileSystem = Nothing

WScript.Quit(0)

Advertisements

Scrivere un file con WSH

August 10, 2007

Dim objFileSystem, objOutputFile
Dim strOutputFile

‘ generate a filename base on the script name
strOutputFile = “./” & Split(WScript.ScriptName, “.”)(0) & “.out”

Set objFileSystem = CreateObject(“Scripting.fileSystemObject”)
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)

objOutputFile.WriteLine(“Hello world (” & Now & “)”)
objOutputFile.Close

Set objFileSystem = Nothing

WScript.Quit(0)

Interrogare i log di sistema

August 9, 2007

Questo script consente di ottenere tutti gli eventi del log di sistema.

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colLoggedEvents = objWMIService.ExecQuery _
(“Select * from Win32_NTLogEvent Where Logfile = ‘System'”)
For Each objEvent in colLoggedEvents
    Wscript.Echo “Date/Time: ” & objEvent.TimeWritten
    Wscript.Echo “Category: ” & objEvent.Category
    Wscript.Echo “Computer: ” & objEvent.ComputerName
    Wscript.Echo “Event ID: ” & objEvent.EventCode
    Wscript.Echo “Description: ” & objEvent.Message
    Wscript.Echo “Record Number: ” & objEvent.RecordNumber
    Wscript.Echo “Source: ” & objEvent.SourceName
    Wscript.Echo “Type: ” & objEvent.Type
    Wscript.Echo “User: ” & objEvent.User
Next

Restartare i servizi (in autostart) stoppati

August 9, 2007

Questo script consente di restartare tutti i servizi, configurati per partire in automatico, che si sono stoppati

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colListOfServices = objWMIService.ExecQuery (“Select * from Win32_Service Where State = ‘Stopped’ and StartMode = ” & “‘Auto'”)
For Each objService in colListOfServices
objService.StartService()
Next

Eliminare i log di IIS più vecchi

August 9, 2007

Questo script può tornare molto utile quando non si ha a disposizione molto spazio su Hard Disk, e si vuole eliminare automaticamente i log, più vecchi di una certa data, di IIS

Option Explicit

Dim DaysOld, strObjTopFolderPath, LogFileExt, ObjFS, ObjTopFolder
Dim ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile

DaysOld = 7
strObjTopFolderPath = “E:\logs\WWW\”
LogFileExt = “.log”

Set ObjFS = CreateObject(“Scripting.FileSystemObject”)
Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath)

    For Each ObjW3SvcFolder in ObjTopFolder.SubFolders
        Set ObjSubFolder = ObjFS.GetFolder(ObjW3SvcFolder)
            For each ObjLogFile in ObjSubFolder.files
                Set ObjFile = ObjFS.GetFile(ObjLogFile)
                If datediff(“d”,ObjFile.DateLastModified,Date()) > DaysOld and lcase(right(ObjLogFile,4))=strLogFIleSuffix then
                    WScript.Echo(“Stò cancellando il file ” & ObjSubFolder.name & “\” & ObjFile.name)
                    ObjFile.Delete
                End If
                Set ObjFile = nothing
            Next
        Set ObjSubFolder = nothing
    Next

Set ObjTopFolder = nothing
Set ObjFS = nothing

Avviare un processo su un computer remoto

July 30, 2007

Ecco uno script che consente di avviare, su un computer remoto, un qualsiasi processo. In questo caso, verrà avviato il processo cmd.exe (shell dos).

strComputer = “nome_computer”
Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2:Win32_Process”)

Error = objWMIService.Create(“cmd.exe”, null, null, intProcessID)

If Error = 0 Then
   Wscript.Echo “Il processo è stato avviato con ID ” & intProcessID & “.”
Else
   Wscript.Echo “Il processo non può essere avviato per errore ” & Error & “.”
End If

Scheduled Tasks attivi su un computer

July 30, 2007

Con questo script, potrete avere la lista dei task attivi su un computer:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colScheduledJobs = objWMIService.ExecQuery _
    (“Select * from Win32_ScheduledJob”)

For Each objJob in colScheduledJobs
    Wscript.Echo “Caption: ” & objJob.Caption
    Wscript.Echo “Command: ” & objJob.Command
    Wscript.Echo “Days of Month: ” & objJob.DaysOfMonth
    Wscript.Echo “Days of Week: ” & objJob.DaysOfWeek
    Wscript.Echo “Description: ” & objJob.Description
    Wscript.Echo “Elapsed Time: ” & objJob.ElapsedTime
    Wscript.Echo “Install Date: ” & objJob.InstallDate
    Wscript.Echo “Interact with Desktop: ” & objJob.InteractWithDesktop
    Wscript.Echo “Job ID: ” & objJob.JobID
    Wscript.Echo “Job Status: ” & objJob.JobStatus
    Wscript.Echo “Name: ” & objJob.Name
    Wscript.Echo “Notify: ” & objJob.Notify
    Wscript.Echo “Owner: ” & objJob.Owner
    Wscript.Echo “Priority: ” & objJob.Priority
    Wscript.Echo “Run Repeatedly: ” & objJob.RunRepeatedly
    Wscript.Echo “Start Time: ” & objJob.StartTime
    Wscript.Echo “Status: ” & objJob.Status
    Wscript.Echo “Time Submitted: ” & objJob.TimeSubmitted
    Wscript.Echo “Until Time: ” & objJob.UntilTime
Next

Lista delle patch installate

July 30, 2007

Questo script vi restituirà la lista di tutte le hot fix installate sul vostro computer:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colQuickFixes = objWMIService.ExecQuery _
    (“Select * from Win32_QuickFixEngineering”)

For Each objQuickFix in colQuickFixes
    Wscript.Echo “Computer: ” & objQuickFix.CSName
    Wscript.Echo “Description: ” & objQuickFix.Description
    Wscript.Echo “Hot Fix ID: ” & objQuickFix.HotFixID
    Wscript.Echo “Installation Date: ” & objQuickFix.InstallDate
    Wscript.Echo “Installed By: ” & objQuickFix.InstalledBy
Next

Lista delle service pack installate

July 30, 2007

Questo script vi restituirà la lista di tutte le service pack installate sul vostro computer:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colOperatingSystems = objWMIService.ExecQuery _
(“Select * from Win32_OperatingSystem”)

For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.ServicePackMajorVersion _
& “.” & objOperatingSystem.ServicePackMinorVersion
Next

Creare shortcuts sul desktop

July 30, 2007

Ecco come creare uno shortcuts sul desktop, tramite vbscript:

Set Shell = CreateObject(“WScript.Shell”)
DesktopPath = Shell.SpecialFolders(“Desktop”)
Set link = Shell.CreateShortcut(DesktopPath & “\LinkName.lnk”)
link.Description = “LinkDescription”
link.IconLocation = “c:\cartella\programma.exe,0”
link.TargetPath = “c:\cartella\programma.exe”
link.WindowStyle = 1
link.WorkingDirectory = “c:\cartella”
link.Save