Sign in to follow this  
Followers 0
PCrane

Help with script

10 posts in this topic

I am new to this scripting. I am wanting to create a script to basically go in a delete a certain file on the machine and than stop a service from running. Force a gpupdate policy refresh, than start the service back up and delete the script off the machine. Right now the command prompt shows and i do not want that to show. Want it more like a service that runs in the background. Maybe with a progress bar, like please wait or something of that nature. Also need to tweak the first msg box some. Seems when you press cancel or the x the script deletes itself. Also and needing this script to run as the local admin on the machine. Any help would be much appreciated.

Here is the code i am working with:

$g_szVersion = "Software reporting fix SD"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)
; Rest of your script goes here 
;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Question
If Not IsDeclared("iMsgBoxAnswer") Then Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(305,"Warning!","Close all open applications before proceeding")
Select
   Case $iMsgBoxAnswer = 1;OK

   Case $iMsgBoxAnswer = 2;Cancel

EndSelect
If $iMsgBoxAnswer = 2 Then Exit


RunAsSet("admin",@ComputerName, "password")
Run("C:\WINDOWS\system32\cmd.exe")
Send("       cd\")
Send("{ENTER}")
Send("cd program files")
Send("{ENTER}")
Send("cd ca")
Send("{ENTER}")
Send("cd unicenter dsm")
Send("{ENTER}")
Send("cd agent")
Send("{ENTER}")
Send("cd units")
Send("{ENTER}")
Send("cd 00000001")
Send("{ENTER}")
Send("cd uam")
Send("{ENTER}")
Send("cd bak")
Send("{ENTER}")
Send("del amsoft.xml")
Send("{ENTER}")
Send("caf stop")
Send("{ENTER}")
Sleep("9000")
Send("gpupdate /force")
Send("{ENTER}")
Sleep("22000")
Send("caf start")
Send("{ENTER}")
Sleep("20000")
Send("exit")
Send("{ENTER}")



Func OnAutoItExit()
    _SelfDelete()
EndFunc

Func _SelfDelete()
    Local $sCmdFile
    FileDelete(@TempDir & "Scratch.bat")
    $sCmdFile = ':loop' & @CRLF _
            & 'del "' & @ScriptFullPath & '"' & @CRLF _
            & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
            & 'del ' & @TempDir & 'Scratch.bat'
    FileWrite(@TempDir & "Scratch.bat", $sCmdFile)
    Run(@TempDir & "Scratch.bat", @TempDir, @SW_HIDE)
EndFunc; SelfDelete

Share this post


Link to post
Share on other sites



$path = "C:\Program Files\ca\unicenter dsm\agent\units\00000001\uam\bak\"
$file = "amsoft.xml"
If FileExists($path & $file) Then FileDelete($path & $file)
RunWait(@Comspec & " /c " $path & "caf stop")
RunWait(@Comspec & " /c " & "gpudate /force")
RunWait(@Comspec & " /c " $path & "caf start")

Share this post


Link to post
Share on other sites

Thanks. I removed most of the command line codes. But after trying to run the script, it gave a Line -1: Error:Error in expression ?

This needs to run with local admin rights as well. How would i code that based off the original code i had?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks. I removed most of the command line codes. But after trying to run the script, it gave a Line -1: Error:Error in expression ?

This needs to run with local admin rights as well. How would i code that based off the original code i had?

The code you had above for the admin set should work. The code I made should replace

Run("C:\WINDOWS\system32\cmd.exe")

Send(" cd\")

Send("{ENTER}")

Send("cd program files")

Send("{ENTER}")

Send("cd ca")

Send("{ENTER}")

Send("cd unicenter dsm")

Send("{ENTER}")

Send("cd agent")

Send("{ENTER}")

Send("cd units")

Send("{ENTER}")

Send("cd 00000001")

Send("{ENTER}")

Send("cd uam")

Send("{ENTER}")

Send("cd bak")

Send("{ENTER}")

Send("del amsoft.xml")

Send("{ENTER}")

Send("caf stop")

Send("{ENTER}")

Sleep("9000")

Send("gpupdate /force")

Send("{ENTER}")

Sleep("22000")

Send("caf start")

Send("{ENTER}")

Sleep("20000")

Send("exit")

Send("{ENTER}")

Repost the whole code and I'll check it out.

Edited by spudw2k

Share this post


Link to post
Share on other sites

$g_szVersion = "Software reporting fix SD"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)
; Rest of your script goes here 
;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Question
If Not IsDeclared("iMsgBoxAnswer") Then Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(305,"Warning!","Close all open applications before proceeding")
Select
   Case $iMsgBoxAnswer = 1;OK

   Case $iMsgBoxAnswer = 2;Cancel

EndSelect
If $iMsgBoxAnswer = 2 Then Exit
RunAsSet("username",@ComputerName, "password")
Run("C:\WINDOWS\system32\cmd.exe")  
$path = "C:\Program Files\ca\unicenter dsm\agent\units\00000001\uam\bak\"
$file = "amsoft.xml"
If FileExists($path & $file) Then FileDelete($path & $file)
RunWait(@Comspec & " /c " $path & "caf stop")
RunWait(@Comspec & " /c " & "gpudate /force")
RunWait(@Comspec & " /c " $path & "caf start")


Func OnAutoItExit()
    _SelfDelete()
EndFunc

Func _SelfDelete()
    Local $sCmdFile
    FileDelete(@TempDir & "Scratch.bat")
    $sCmdFile = ':loop' & @CRLF _
            & 'del "' & @ScriptFullPath & '"' & @CRLF _
            & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
            & 'del ' & @TempDir & 'Scratch.bat'
    FileWrite(@TempDir & "Scratch.bat", $sCmdFile)
    Run(@TempDir & "Scratch.bat", @TempDir, @SW_HIDE)
EndFunc; SelfDelete

This gives me error:

Line -1:

Error:Error in expression

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

My bad. Typo on my part. :)

Needed and additional & between " /c " and $path

I also removed the Run("C:\Windows\system32\cmd.exe") line. Not neccessary anymore.

Don't forget to change user and pass

$g_szVersion = "Software reporting fix SD"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)
; Rest of your script goes here 
;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Question
If Not IsDeclared("iMsgBoxAnswer") Then Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(305,"Warning!","Close all open applications before proceeding")
Select
   Case $iMsgBoxAnswer = 1;OK

   Case $iMsgBoxAnswer = 2;Cancel

EndSelect
If $iMsgBoxAnswer = 2 Then Exit
RunAsSet("username",@ComputerName, "password")
$path = "C:\Program Files\ca\unicenter dsm\agent\units\00000001\uam\bak\"
$file = "amsoft.xml"
If FileExists($path & $file) Then FileDelete($path & $file)
RunWait(@Comspec & " /c " & $path & "caf stop")
RunWait(@Comspec & " /c " & "gpudate /force")
RunWait(@Comspec & " /c " & $path & "caf start")


Func OnAutoItExit()
    _SelfDelete()
EndFunc

Func _SelfDelete()
    Local $sCmdFile
    FileDelete(@TempDir & "Scratch.bat")
    $sCmdFile = ':loop' & @CRLF _
            & 'del "' & @ScriptFullPath & '"' & @CRLF _
            & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
            & 'del ' & @TempDir & 'Scratch.bat'
    FileWrite(@TempDir & "Scratch.bat", $sCmdFile)
    Run(@TempDir & "Scratch.bat", @TempDir, @SW_HIDE)
EndFunc; SelfDelete
Edited by spudw2k

Share this post


Link to post
Share on other sites

Ok it was able to delete the needed file, but the service did not seem to stop at all?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Ok, is caf an executbale in the same dir as the xml file? That's what I assumed from the code cause I've never heard of a prog called caf. Try changing the first and second RunWait line to this.

RunWait(@Comspec & " /c " & "caf stop")

or better

RunWait(@Comspec & " /c " & chr(34) & "fullpath to caf file\caf" & chr(34) & " stop")

Edited by spudw2k

Share this post


Link to post
Share on other sites

Awesome that seemed to work. The only thing i noticed, is when you either click cancel or the X the file will delete itself?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I think that is the nature of the _SelfDelete function. If you want it to delete itself only if the script completes, then pull the _SelfDelete call out of the OnAutoItExit() func.

$g_szVersion = "Software reporting fix SD"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)
; Rest of your script goes here 
;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Question
If Not IsDeclared("iMsgBoxAnswer") Then Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(305,"Warning!","Close all open applications before proceeding")
Select
   Case $iMsgBoxAnswer = 1;OK

   Case $iMsgBoxAnswer = 2;Cancel

EndSelect
If $iMsgBoxAnswer <> 1 Then Exit
RunAsSet("username",@ComputerName, "password")
$path = "C:\Program Files\ca\unicenter dsm\agent\units\00000001\uam\bak\"
$file = "amsoft.xml"
If FileExists($path & $file) Then FileDelete($path & $file)
RunWait(@Comspec & " /c " & "caf stop")
RunWait(@Comspec & " /c " & "gpudate /force")
RunWait(@Comspec & " /c " & "caf start")

_SelfDelete()


Func _SelfDelete()
    Local $sCmdFile
    FileDelete(@TempDir & "Scratch.bat")
    $sCmdFile = ':loop' & @CRLF _
            & 'del "' & @ScriptFullPath & '"' & @CRLF _
            & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
            & 'del ' & @TempDir & 'Scratch.bat'
    FileWrite(@TempDir & "Scratch.bat", $sCmdFile)
    Run(@TempDir & "Scratch.bat", @TempDir, @SW_HIDE)
EndFunc; SelfDelete
Edited by spudw2k

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0