[Powershell] - Loop (until) & Kill

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 7 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Etmila
Posts: 5
Last visit: Mon Jul 18, 2016 5:33 pm

[Powershell] - Loop (until) & Kill

Post by Etmila »

Hi

I have a Powershell script and everything is working good presently :
  1. powershell do{sleep 5;(New-Object Net.WebClient).DownloadFile('site/file.old','%APPDATA%\file.exe')}while(!$?);&'%APPDATA%\file.exe
I would like to add a loop in this script to close a specific process until the process appear. I saw that the command to kill a process should be "Stop-Process -processname process" but I don't know about the loop and how to integrate it to my script ?

Someone adviced me a script (thanks to him) :
  1. $ProcessActive = Get-Process processname -ErrorAction SilentlyContinue ; if($ProcessActive -eq $null){do{sleep 5;(New-Object Net.WebClient).DownloadFile('site/file.exe','%APPDATA%\file.exe')}while(!$?);&'%APPDATA%\file.exe'}else{while($ProcessActive -ne $null) {Stop-Process -processname test | powershell -windowstyle hidden do{sleep 5;(New-Object Net.WebClient).DownloadFile('site/file.exe','%APPDATA%\file.exe')}while(!$?);&'%APPDATA%\file.exe' }}

But I get this error message :
  1. Exception for « DownloadFile » with « 2 » argument(s) : Exception happened when WebClient was asked
  2. Letter : 153
  3. + $ProcessActive = Get-Process processname -ErrorAction SilentlyContinue ; if($ProcessActive -eq $null){do{sleep 5;(New
  4. -Object Net.WebClient).DownloadFile <<<< ('site/file.exe','%APPDATA%\file.exe')}while(!$?);&'%APPDA
  5. TA%\file.exe'}else{while($ProcessActive -ne $null) {Stop-Process -processname test | powershell -windowstyle hidden d
  6. o{sleep 5;(New-Object Net.WebClient).DownloadFile(site/file.exe','%APPDATA%\file.exe')}while(!$?);
  7. &'%APPDATA%\file.exe' }}
  8.    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  9.    + FullyQualifiedErrorId : DotNetMethodException
Have you some advice/idea ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: [Powershell] - Loop (until) & Kill

Post by jvierra »

Start by fixing the script. "%APPDATAS%" won't work in PowerShell. The variable is $env:APPDATA.

I would suggest not using that location as it is protected and should not be used. Use $PWD instead or $env:TEMP.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: [Powershell] - Loop (until) & Kill

Post by jvierra »

Here is the best way to do this:
  1. Get-Process processname -ErrorAction SilentlyContinue|Stop-Process -Force
  2. Try{
  3.     $wc=New-Object System.Net.WebClient
  4.     $wc.DownloadFile('site/file.exe', "$PWD\file.exe")
  5.     & "$PWD\file.exe"
  6. }
  7. Catch{
  8.     Throw $_
  9. }
User avatar
Etmila
Posts: 5
Last visit: Mon Jul 18, 2016 5:33 pm

Re: [Powershell] - Loop (until) & Kill

Post by Etmila »

Thanks for your advices.

Sorry if I misunderstood but I don't see where I define my process to kill ?
  1. Get-Process processname -ErrorAction SilentlyContinue|Stop-Process -Force Try{ $wc=New-Object System.Net.WebClient $wc.DownloadFile('http://d.7-zip.org/a/7z1601.exe', "$PWD\7z1601.exe") & "$PWD\7z1601.exe" } Catch{ Throw $_ }
(I meant, after my file has been executed another process will be opened and I want to kill it when it appears. So I'm thinking about a loop in my script to kill the process as soon as it appears)

E
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: [Powershell] - Loop (until) & Kill

Post by jvierra »

"Processname" is the process name"
  1. Get-Process notepad -ErrorAction SilentlyContinue|Stop-Process -Force
  2. Try{
  3.     $wc=New-Object System.Net.WebClient
  4.     $wc.DownloadFile('site/file.exe', "$PWD\file.exe")
  5.     & "$PWD\file.exe"
  6. }
  7. Catch{
  8.     Throw $_
  9. }
User avatar
Etmila
Posts: 5
Last visit: Mon Jul 18, 2016 5:33 pm

Re: [Powershell] - Loop (until) & Kill

Post by Etmila »

Does your code works on your side ?
  1. Get-Process notepad -ErrorAction SilentlyContinue|Stop-Process -Force
  2. Try{
  3. $wc=New-Object System.Net.WebClient
  4. $wc.DownloadFile('http://d.7-zip.org/a/7z1601.exe', "$PWD\7z1601.exe")
  5. & "$PWD\7z1601.exe"
  6. }
  7. Catch{
  8. Throw $_
  9. }
It just close the process when the process is already active but do nothing if the process is not opened yet. Also it download and execute nothing too.

I tried to write the script on a line too, I get an error.
  1. Get-Process test -ErrorAction SilentlyContinue|Stop-Process -Force Try{ $wc=New-Object System.Net.WebClient $wc.DownloadFile('http://d.7-zip.org/a/7z1601.exe', "%APPDATA%\7z1601.exe") & "%APPDATA%\7z1601.exe"}Catch{ Throw $_}
This topic is 7 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked