Hi everbody!
Please, can someone help me with this issue: I want to delete my script as his "last action".
It works nice when I use the .ps1 file.
But after build the .exe and run it, it appears to not be able to delete itself.
Any ideas?
How to delete my .EXE script just before exit it?
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: How to delete my .EXE script just before exit it?
You can't delete any executable while it is running. The OS owns and controls teh EXE whie it is running. You can schedule a one-shot task to delete the exe after it ends.
Re: How to delete my .EXE script just before exit it?
Thanks!
Will try this approach. (as soon as I discover how to do it from the PowerShell script)
Will try this approach. (as soon as I discover how to do it from the PowerShell script)
Re: How to delete my .EXE script just before exit it?
You could try this..
#
# do stuff
#
# remove self
$Self = (Get-Process -Id $pid).Path
$Exe = 'powershell.exe'
$ExeArgs = '-command {remove-item '+$Self+' -force}'
start-process powershell -ArgumentList "sleep 1; $Exe $ExeArgs" -WindowStyle hidden
#
# do stuff
#
# remove self
$Self = (Get-Process -Id $pid).Path
$Exe = 'powershell.exe'
$ExeArgs = '-command {remove-item '+$Self+' -force}'
start-process powershell -ArgumentList "sleep 1; $Exe $ExeArgs" -WindowStyle hidden
Re: How to delete my .EXE script just before exit it?
This is the original issue. You cannot delete a process that is running. Read the first response. That is the only method that can be used. Your method will execute before the current process has exited and the file will still be locked by the system.
Re: How to delete my .EXE script just before exit it?
@jverra,
It does work, I have tried it.
The parent of the first powershell is the executable but the second powershell from the start-process launches after the primary executable has completed).
Firing order..
2021/11/24 16:15:24.483 B test "C:\cardinal\bin\test.exe"
2021/11/24 16:15:25.277 B powershell "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" sleep 1; powershell.exe -command {remove-item C:\cardinal\bin\test.exe -force}
2021/11/24 16:15:25.309 E test exit=0 "C:\cardinal\bin\test.exe"
2021/11/24 16:15:26.596 B powershell "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -encodedCommand cgBlAG0AbwB2AGUALQBpAHQAZQBtACAAQwA6AFwAYwBhAHIAZABpAG4AYQBsAFwAYgBpAG4AXAB0AGUAcwB0AC4AZQB4AGUAIAAtAGYAbwByAGMAZQA= -inputFormat xml -outputFormat text
2021/11/24 16:15:26.849 E powershell exit=0 "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -encodedCommand cgBlAG0AbwB2AGUALQBpAHQAZQBtACAAQwA6AFwAYwBhAHIAZABpAG4AYQBsAFwAYgBpAG4AXAB0AGUAcwB0AC4AZQB4AGUAIAAtAGYAbwByAGMAZQA= -inputFormat xml -outputFormat text
2021/11/24 16:15:26.874 E powershell exit=0 "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" sleep 1; powershell.exe -command {remove-item C:\cardinal\bin\test.exe -force}
It does work, I have tried it.
The parent of the first powershell is the executable but the second powershell from the start-process launches after the primary executable has completed).
Firing order..
2021/11/24 16:15:24.483 B test "C:\cardinal\bin\test.exe"
2021/11/24 16:15:25.277 B powershell "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" sleep 1; powershell.exe -command {remove-item C:\cardinal\bin\test.exe -force}
2021/11/24 16:15:25.309 E test exit=0 "C:\cardinal\bin\test.exe"
2021/11/24 16:15:26.596 B powershell "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -encodedCommand cgBlAG0AbwB2AGUALQBpAHQAZQBtACAAQwA6AFwAYwBhAHIAZABpAG4AYQBsAFwAYgBpAG4AXAB0AGUAcwB0AC4AZQB4AGUAIAAtAGYAbwByAGMAZQA= -inputFormat xml -outputFormat text
2021/11/24 16:15:26.849 E powershell exit=0 "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -encodedCommand cgBlAG0AbwB2AGUALQBpAHQAZQBtACAAQwA6AFwAYwBhAHIAZABpAG4AYQBsAFwAYgBpAG4AXAB0AGUAcwB0AC4AZQB4AGUAIAAtAGYAbwByAGMAZQA= -inputFormat xml -outputFormat text
2021/11/24 16:15:26.874 E powershell exit=0 "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" sleep 1; powershell.exe -command {remove-item C:\cardinal\bin\test.exe -force}
Re: How to delete my .EXE script just before exit it?
Sounds like too many operations when you can just schedule a one shot and exit the program. THat does everything you are doing in one line.
Why write ten times as much code as necessary?
Why write ten times as much code as necessary?
Re: How to delete my .EXE script just before exit it?
I have used TS in the past to divorce processes and have not always found it that reliable and you have to deal with credentials. But thats fine, the good thing with powershell and MS...you have loads of choices.
Re: How to delete my .EXE script just before exit it?
Not true under the question was asked and, in both cases, you would require credentials. The task-based method is the easiest, most reliable method except for one other.
In the end this would only be an issue for hackers attempting to bypass security as an installer can do all of this withot the need to delete anything.
I, and all software engineers oppose these hacking methods as they are mostly used to distribute malware and violate all basic principles of secure and reliable software development.
Often untrained techs learning scripting cook up these schemes as if they are new. They are not new. They are old schemes that are trivial and come in many forms. Learning correct methods that are compliant with all vendor APIs and directives are where all non-engineers should focus to avoid creating security holes in systems.
Happy Thanksgiving (in the US), good luck, take care and avoid poppycock methods by learning basic software engineering and current security methods.