From: Jason Rosolowski on
I've created a script and put it on my Machine OU under Computer
Configuration->Windows Settings->Scripts->Startup. I can get it to work,
but I'd like to streamline this script a little bit. Right now, I have to
shell out to DOS to run

objShell.Run "schtasks /create /tn " & chr(34) & "Defrag C" &chr(34) & " /ru
system /sc weekly /d SAT /st 02:00:00 /tr " & chr(34) & "\" & chr(34) &
"%systemroot%\system32\defrag.exe\" & chr(34) & "c:" & chr(34)

This creates an entry in the Scheduled Tasks called "Defrag C". If I run
this chunk of code again, within the DOS box is tells me the job already
exists and just continues on. I'd like to be able to do this without
shelling out though. I've tried this

strComputer = "."
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create("Defrag.exe c:",
"********020000.000000-300", True , 32, , , JobID)

But that creates a job called "At1". If you run the script again, there
will be 2 jobs. 1 called "At1" and another called "At2". If you would keep
executing this, it would fill up I guess.

So, is there property in the Win32_ScheduleJob object to define the job
name? I haven't been able to find anything about it.


From: Jeffery Hicks [MVP] on
That WMI class is for the older and now deprecated AT style jobs. Your
approach using schtasks, despite using a shell command, is really the best
way to accomplish this. There's nothing wrong with this approach. It's the
right tool for the job. Schtasks.exe in Vista has a new parameter to force
creation ignoring any errors. You could use Schtasks /Query and parse the
output using STDOUT to see if the job already exists.

Or skip the VBScript format altogether and do it all in a batch file. You
could query for the jobs and see if your job already exists. If not, then
create it.

schtasks /query | find /i "Defrag C"
if errorlevel 1 goto :CREATE
GOTO :EOF

:CREATE
rem insert schtasks code here

:EOF


--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://blog.sapien.com

Now Available: WSH and VBScript Core: TFM

"Jason Rosolowski" <jrosolowski(a)nolandhealth.com> wrote in message
news:%23MXskjKHIHA.3768(a)TK2MSFTNGP06.phx.gbl...
> I've created a script and put it on my Machine OU under Computer
> Configuration->Windows Settings->Scripts->Startup. I can get it to work,
> but I'd like to streamline this script a little bit. Right now, I have to
> shell out to DOS to run
>
> objShell.Run "schtasks /create /tn " & chr(34) & "Defrag C" &chr(34) & "
> /ru system /sc weekly /d SAT /st 02:00:00 /tr " & chr(34) & "\" & chr(34)
> & "%systemroot%\system32\defrag.exe\" & chr(34) & "c:" & chr(34)
>
> This creates an entry in the Scheduled Tasks called "Defrag C". If I run
> this chunk of code again, within the DOS box is tells me the job already
> exists and just continues on. I'd like to be able to do this without
> shelling out though. I've tried this
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" &
> "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
> errJobCreated = objNewJob.Create("Defrag.exe c:",
> "********020000.000000-300", True , 32, , , JobID)
>
> But that creates a job called "At1". If you run the script again, there
> will be 2 jobs. 1 called "At1" and another called "At2". If you would
> keep executing this, it would fill up I guess.
>
> So, is there property in the Win32_ScheduleJob object to define the job
> name? I haven't been able to find anything about it.
>

From: Jason Rosolowski on
Awesome, thank you for the reply and sample code.


"Jeffery Hicks [MVP]" <jhicks(a)sapien.com> wrote in message
news:4C10EDD0-CFAC-44E7-A7BF-DA22B26E267B(a)microsoft.com...
> That WMI class is for the older and now deprecated AT style jobs. Your
> approach using schtasks, despite using a shell command, is really the best
> way to accomplish this. There's nothing wrong with this approach. It's
> the right tool for the job. Schtasks.exe in Vista has a new parameter to
> force creation ignoring any errors. You could use Schtasks /Query and
> parse the output using STDOUT to see if the job already exists.
>
> Or skip the VBScript format altogether and do it all in a batch file. You
> could query for the jobs and see if your job already exists. If not, then
> create it.
>
> schtasks /query | find /i "Defrag C"
> if errorlevel 1 goto :CREATE
> GOTO :EOF
>
> :CREATE
> rem insert schtasks code here
>
> :EOF
>
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://blog.sapien.com
>
> Now Available: WSH and VBScript Core: TFM
>
> "Jason Rosolowski" <jrosolowski(a)nolandhealth.com> wrote in message
> news:%23MXskjKHIHA.3768(a)TK2MSFTNGP06.phx.gbl...
>> I've created a script and put it on my Machine OU under Computer
>> Configuration->Windows Settings->Scripts->Startup. I can get it to work,
>> but I'd like to streamline this script a little bit. Right now, I have
>> to shell out to DOS to run
>>
>> objShell.Run "schtasks /create /tn " & chr(34) & "Defrag C" &chr(34) & "
>> /ru system /sc weekly /d SAT /st 02:00:00 /tr " & chr(34) & "\" & chr(34)
>> & "%systemroot%\system32\defrag.exe\" & chr(34) & "c:" & chr(34)
>>
>> This creates an entry in the Scheduled Tasks called "Defrag C". If I run
>> this chunk of code again, within the DOS box is tells me the job already
>> exists and just continues on. I'd like to be able to do this without
>> shelling out though. I've tried this
>>
>> strComputer = "."
>> Set objWMIService = GetObject("winmgmts:" &
>> "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
>> Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
>> errJobCreated = objNewJob.Create("Defrag.exe c:",
>> "********020000.000000-300", True , 32, , , JobID)
>>
>> But that creates a job called "At1". If you run the script again, there
>> will be 2 jobs. 1 called "At1" and another called "At2". If you would
>> keep executing this, it would fill up I guess.
>>
>> So, is there property in the Win32_ScheduleJob object to define the job
>> name? I haven't been able to find anything about it.
>>
>


From: Dave on
Here is some code I use.

'On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim strOSCaption, strOSSPVersion, strOSVersion, strTempDir, strDays
Dim intTimeZone
Dim blnCheckSched, blnCheckSched_W2K
Dim arrDays

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnvironment = WshShell.Environment("Process")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Drives = fso.Drives

intHighNumberDays = 6
intLowNumberDays = 0
intHighNumberHours = 7
intLowNumberHours = 0

GetOSInfo

strDays = ("SAT,SUN,MON,TUE,WED,THU,FRI")
arrDays = Split(strDays, ",")

strHours = ("00,01,02,05,20,21,22,23")
arrHours = Split(strHours, ",")

'i = 0

Select Case strOSVersion
Case "5.1.2600"
WScript.Echo "This is XP"
For Each Drive In Drives
If Drive.DriveType = 2 Then
CheckSched Drive
If blnCheckSched Then
WScript.Echo "Defrag already enabled for drive (" & Drive & ")"
Else
WScript.Echo "Scheduling defrag of drive (" & Drive & ")"
i = RandomNumb(intHighNumberDays, intLowNumberDays)
k = RandomNumb(intHighNumberHours, intLowNumberHours)
WScript.Echo "schtasks.exe /Create /RU SYSTEM /SC WEEKLY /D " &
arrDays(i) & " /ST " & arrHours(k) & ":00:00 /TN " & chr(34) & "Defrag Drive
(" & Left(Drive,1) & ")" & Chr(34) & " /TR " & Chr(34) &
"%WinDir%\System32\defrag.exe " & Drive & " -f" & Chr(34) & ""
WshShell.Exec ("schtasks.exe /Create /RU SYSTEM /SC WEEKLY /D " &
arrDays(i) & " /ST " & arrHours(k) & ":00:00 /TN " & chr(34) & "Defrag Drive
(" & Left(Drive,1) & ")" & Chr(34) & " /TR " & Chr(34) &
"%WinDir%\System32\defrag.exe " & Drive & " -f" & Chr(34) & "")
End If
Else
End If
Next
Case "5.2.3790"
WScript.Echo "This is Windows 2003"
For Each Drive In Drives
If Drive.DriveType = 2 Then
CheckSched Drive
If blnCheckSched Then
WScript.Echo "Defrag already enabled for drive (" & Drive & ")"
Else
WScript.Echo "Scheduling defrag of drive (" & Drive & ")"
i = RandomNumb(intHighNumberDays, intLowNumberDays)
k = RandomNumb(intHighNumberHours, intLowNumberHours)
WScript.Echo "schtasks.exe /Create /RU SYSTEM /SC WEEKLY /D " &
arrDays(i) & " /ST " & arrHours(k) & ":00:00 /TN " & chr(34) & "Defrag Drive
(" & Left(Drive,1) & ")" & Chr(34) & " /TR " & Chr(34) &
"%WinDir%\System32\defrag.exe " & Drive & " -f" & Chr(34) & ""
WshShell.Exec ("schtasks.exe /Create /RU SYSTEM /SC WEEKLY /D " &
arrDays(i) & " /ST " & arrHours(k) & ":00:00 /TN " & chr(34) & "Defrag Drive
(" & Left(Drive,1) & ")" & Chr(34) & " /TR " & Chr(34) &
"%WinDir%\System32\defrag.exe " & Drive & " -f" & Chr(34) & "")
End If
Else
End If
Next
Case Else
WScript.Echo "I don't know what OS this is"
End Select

WScript.Echo VbCrLf & "Done!"

'**********************************************************************************************************
Sub GetOSInfo

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM
Win32_OperatingSystem", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo "Caption: " & objItem.Caption
strOSCaption = objItem.Caption
WScript.Echo "CSDVersion: " & objItem.CSDVersion
strOSSPVersion = objItem.CSDVersion
WScript.Echo "CurrentTimeZone: " & objItem.CurrentTimeZone
intTimeZone = objItem.CurrentTimeZone
WScript.Echo "OSProductSuite: " & objItem.OSProductSuite
WScript.Echo "OSType: " & objItem.OSType
WScript.Echo "Version: " & objItem.Version
strOSVersion = objItem.Version
WScript.Echo
Next

End Sub

Function WMIDateStringToDate(dtmDate)

WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" &
Mid(dtmDate,13, 2))

End Function

Sub CheckSched(d)

Set objWshScriptExec = WshShell.Exec ("schtasks.exe /query")
Set objStdOut = objWshScriptExec.StdOut

WScript.Echo "Checking scheduled defrag task for drive (" & d & ")"

blnCheckSched = False

While Not objStdOut.AtEndOfStream
strLine = objStdOut.ReadLine
If InStr(strLine, "Defrag Drive (" & Left(d,1) & ")") Then
blnCheckSched = True
Exit Sub
Else
End If
Wend

End Sub

Function RandomNumb(intHi, intLo)

Randomize
RandomNumb = Int((intHi - intLo + 1) * Rnd + intLo)

End Function



"Jason Rosolowski" <jrosolowski(a)nolandhealth.com> wrote in message
news:etV%23bSMHIHA.1548(a)TK2MSFTNGP05.phx.gbl...
> Awesome, thank you for the reply and sample code.
>
>
> "Jeffery Hicks [MVP]" <jhicks(a)sapien.com> wrote in message
> news:4C10EDD0-CFAC-44E7-A7BF-DA22B26E267B(a)microsoft.com...
>> That WMI class is for the older and now deprecated AT style jobs. Your
>> approach using schtasks, despite using a shell command, is really the
>> best way to accomplish this. There's nothing wrong with this approach.
>> It's the right tool for the job. Schtasks.exe in Vista has a new
>> parameter to force creation ignoring any errors. You could use Schtasks
>> /Query and parse the output using STDOUT to see if the job already
>> exists.
>>
>> Or skip the VBScript format altogether and do it all in a batch file. You
>> could query for the jobs and see if your job already exists. If not,
>> then create it.
>>
>> schtasks /query | find /i "Defrag C"
>> if errorlevel 1 goto :CREATE
>> GOTO :EOF
>>
>> :CREATE
>> rem insert schtasks code here
>>
>> :EOF
>>
>>
>> --
>> Jeffery Hicks
>> Microsoft PowerShell MVP
>> http://www.scriptinganswers.com
>> http://blog.sapien.com
>>
>> Now Available: WSH and VBScript Core: TFM
>>
>> "Jason Rosolowski" <jrosolowski(a)nolandhealth.com> wrote in message
>> news:%23MXskjKHIHA.3768(a)TK2MSFTNGP06.phx.gbl...
>>> I've created a script and put it on my Machine OU under Computer
>>> Configuration->Windows Settings->Scripts->Startup. I can get it to
>>> work, but I'd like to streamline this script a little bit. Right now, I
>>> have to shell out to DOS to run
>>>
>>> objShell.Run "schtasks /create /tn " & chr(34) & "Defrag C" &chr(34) & "
>>> /ru system /sc weekly /d SAT /st 02:00:00 /tr " & chr(34) & "\" &
>>> chr(34) & "%systemroot%\system32\defrag.exe\" & chr(34) & "c:" & chr(34)
>>>
>>> This creates an entry in the Scheduled Tasks called "Defrag C". If I
>>> run this chunk of code again, within the DOS box is tells me the job
>>> already exists and just continues on. I'd like to be able to do this
>>> without shelling out though. I've tried this
>>>
>>> strComputer = "."
>>> Set objWMIService = GetObject("winmgmts:" &
>>> "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
>>> Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
>>> errJobCreated = objNewJob.Create("Defrag.exe c:",
>>> "********020000.000000-300", True , 32, , , JobID)
>>>
>>> But that creates a job called "At1". If you run the script again, there
>>> will be 2 jobs. 1 called "At1" and another called "At2". If you would
>>> keep executing this, it would fill up I guess.
>>>
>>> So, is there property in the Win32_ScheduleJob object to define the job
>>> name? I haven't been able to find anything about it.
>>>
>>
>
>

From: ekkehard.horner on
Dave schrieb:
> Here is some code I use.
>
[... very nice script ...]
>
> Function RandomNumb(intHi, intLo)
>
> Randomize
> RandomNumb = Int((intHi - intLo + 1) * Rnd + intLo)
>
> End Function

Don't restart the random number generator repeatedly.

Eric Lippert comment.
No, you only need to call Randomize _once_, not once per call to Rnd.
Once the system has been seeded, each random number generated acts as
the seed for the NEXT call to Rnd. The RNG has been cleverly designed
so that it does not go into short loops.
In fact, you MUST only call Randomize once. Doing so in a loop, as you
are doing, makes the random number sequence LESS random, not MORE random.
Why's that? Read on!
The Randomize function seeds the random number generator by passing it
the current system time, a nifty little trick considering the fact
that – in this dimension anyway – each moment in time is unique.

http://blogs.msdn.com/gstemp/archive/2004/02/23/78434.aspx

[...]

 |  Next  |  Last
Pages: 1 2
Prev: File in Use
Next: event supervise