Jump to content

Recommended Posts

Posted

Hi. I'm facing a problem with my code.

#NoTrayIcon
$var=Call("_IsWindowsVersion")

If $var=True Then ; If OS Version is above Windows XP
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
RunWait(@ComSpec & " /c " & "schtasks /create /xml" & "Wise Memory Optimizer Task.xml" "/tn" "Wise Memory Optimizer Task")    
DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1)

Else ; If OS Version is Windows XP or lower
FileInstall('.\WinXP\Wise Memory Optimizer Task.job', @WindowsDir & '\Tasks\Wise Memory Optimizer Task.job')
EndIf

Func _IsWindowsVersion() ;If OS is newer than Windows XP
   $ver=RegRead('HKLM64\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      If $ver="" Then
         $ver=RegRead('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      EndIf
Return $ver > 5.1
EndFunc

If I execute batch command

schtasks /create /xml "Wise Memory Optimizer Task.xml" /tn "Wise Memory Optimizer Task"

Everything works fine and a new task is created. But in my script, it doesn't work. I assume it's this line:

RunWait(@ComSpec & " /c " & "schtasks /create /xml" & "Wise Memory Optimizer Task.xml" "/tn" "Wise Memory Optimizer Task")

I've never been good at executing ComSpec. Usually I find ways to do what I need, but this time I'm sitting here for half a day and no luck.

 

I'd appreciate if someone could spot an error in my code. Thank you (and I'm on Windows 7).

Posted (edited)

Doesn't cut it so far. And I don't understand what

$sQuote

  is about.

 

And nope, apparently not supported. The worst thing is that I DID it before, and it worked. But I lost the file.

Edited by supraspecies
Posted

if you need quoted strings you need a way to add the quotes in addition to quoting the string

it needed more spaces

ConsoleWrite(@ComSpec & " /c " & "schtasks /create /xml " & $sQuote & "Wise Memory Optimizer Task.xml" & $sQuote & " /tn " & $sQuote & "Wise Memory Optimizer Task" & $sQuote)

RunWait(@ComSpec & " /c " & "schtasks /create /xml " & $sQuote & "Wise Memory Optimizer Task.xml" & $sQuote & " /tn " & $sQuote & "Wise Memory Optimizer Task" & $sQuote)

 

 

Posted

This is how I did it before (also supplied WMOT.xml file with it)

RunWait(@ComSpec & " /c " & 'schtasks /create /xml "WMOT.xml" /tn "Wise Memory Optimizer Task"', "", @SW_HIDE)

But somewhere down the line I messed up. Maybe similar names aren't allowed, or i forgot to put XML with my script. I'll figure this out. But I appreciate all the help.

Posted

And yes this way is supported as well

ConsoleWrite(@ComSpec & ' /c ' & 'schtasks /create /xml ' & '"Wise Memory Optimizer Task.xml"' & ' /tn ' & '"Wise Memory Optimizer Task"')
RunWait(@ComSpec & ' /c ' & 'schtasks /create /xml ' & '"Wise Memory Optimizer Task.xml"' & ' /tn ' & '"Wise Memory Optimizer Task"')

 

Posted

Your best bet is to use consolewrite() like I did above to see what the resulting command looks like you might also need your path to the xml file as well

 

Posted
7 minutes ago, Subz said:

RunWait(@ComSpec & ' /c schtasks /create /xml "Wise Memory Optimizer Task.xml" /tn "Wise Memory Optimizer Task"')

Well, it doesn't work for whatever reason. Also my guess with the names was wrong. If worst c0omes to worst, I'll just use my old, working script. I just wanted to clean it up a bit.

Posted

what if you swap the switches and or use a path for the xml?

RunWait(@ComSpec & ' /c schtasks /create /tn "Wise Memory Optimizer Task" /xml "C:\Fully\Qualified\Path\Wise Memory Optimizer Task.xml"')

 

Posted (edited)

Ok, this nis the final, cleaned up script. Everything else I tried didn't work. No idea why.

#NoTrayIcon
$var=Call("_IsWindowsVersion")

If $var=True Then ; If OS Version is above Windows XP
RunWait(@ComSpec & " /c " & 'schtasks /create /xml "WMOT.xml" /tn "Wise Memory Optimizer Task"', "", @SW_HIDE)
Else
FileCopy('WMOT.job', @WindowsDir & '\Tasks\Wise Memory Optimizer Task.job')
EndIf


Func _IsWindowsVersion() ;If OS is newer than Windows XP
   $ver=RegRead('HKLM64\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      If $ver="" Then
         $ver=RegRead('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      EndIf
Return $ver > 5.1
EndFunc

 

Edited by supraspecies
Posted

Check/Edit your file path:

 

#NoTrayIcon
#RequireAdmin

Global Const $WinVersion = _GetWinVer()
Global $IsWin64 = False, $IsNewOS = False
If StringInStr(@OSArch, "64") > 0 Then $IsWin64 = True
If $WinVersion > 5.2 Then $IsNewOS = True

If $IsWin64 Then _SysNative(0)

Global $TasksPath = @WindowsDir & '\Tasks\Wise Memory Optimizer Task.job'
Global $TasksCMD = 'schtasks /create /xml "' & 'WMOT.xml"' & ' /tn "' & 'Wise Memory Optimizer Task"'


If $IsNewOS Then ; If OS Version is above Windows XP
    _RunCmd($TasksCMD)
Else ; If OS Version is Windows XP or lower
    FileInstall('WinXP\Wise Memory Optimizer Task.job', $TasksPath)
    If @error Or (Not FileExists($TasksPath)) Then
        MsgBox(0, "", "Install file Error!")
    Else
        MsgBox(0, "", "Install  OK")
    EndIf
EndIf


#Region Function#
; * -----:|
Func _GetWinVer($sType = 0)
    Local $_WinVer = FileGetVersion(@SystemDir & "\ntoskrnl.exe")
    If @error Then $_WinVer = FileGetVersion(@SystemDir & "\winver.exe")
    $_WinVer = StringLeft($_WinVer, 3)
    If $sType Or $_WinVer < 1 Then
        Local Const $tagOSVERSIONINFO = 'struct;dword OSVersionInfoSize;dword MajorVersion;dword MinorVersion;dword BuildNumber;dword PlatformId;wchar CSDVersion[128];endstruct'
        Local $tOSVI = DllStructCreate($tagOSVERSIONINFO)
        DllStructSetData($tOSVI, 1, DllStructGetSize($tOSVI))
        Local $aRet = DllCall('kernel32.dll', 'bool', 'GetVersionExW', 'struct*', $tOSVI)
        If @error Or Not $aRet[0] Then Return SetError(@error, @extended, 0)
        $_WinVer = BitOR(BitShift(DllStructGetData($tOSVI, 2), -8), DllStructGetData($tOSVI, 3))
        If $sType = 2 Then Return $_WinVer
        Return BitAND(BitShift($_WinVer, 8), 0xFF) & '.' & BitAND($_WinVer, 0xFF)
    Else
        Return $_WinVer
    EndIf
EndFunc   ;==>_GetWinVer
; * -----:|
Func _SysNative($xEnableFsRedirection = 0)
    If $xEnableFsRedirection Then
        Local $iResult = DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1)
        Return SetError(@error, @extended, $iResult)
    Else
        Local $aResult = DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
        Return SetError(@error, @extended, $aResult)
    EndIf
EndFunc   ;==>_SysNative
; * -----:|
Func _RunCmd($sCommand)
    ConsoleWrite("+Execute: " & $sCommand & @CRLF)
    Local $sOutput = '', $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, '', @SW_HIDE, 0x6)
    Do
        $sOutput &= StdoutRead($iPID)
    Until @error
    Do
        $sOutput &= StderrRead($iPID)
    Until @error
    $sOutput = StringStripWS($sOutput, 7)
    ConsoleWrite("!" & $sOutput & @CRLF)
    ConsoleWrite("!--------------------------------------------------------------" & @CRLF)
    Return $sOutput
EndFunc   ;==>_RunCmd

#EndRegion Function#

 

GoodLucky

Regards,
 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...