0

I need your help resolving an exercise in Powershell. I have to make a script which runs only when another one is running. For example, I have to run a script which deletes files older than 1 day while a script which restarts a process runs.

I tried to use jobs to make the script run in parallel but I haven't had any succes.

--The script to delete files
Get-ChildItem -Path "a path" -include *.txt -Recurse | Where-Object {$_.LastWriteTime -lt $DateToDelete} | Remove-Item -Force

--The script to restart a process
Get-Process notepad | Stop-Process | Start-Process

1 Answers1

0

I think your problem is with the second script, you can't restart a process like that.

If you tried this line Get-Process notepad | Stop-Process | Start-Process in the console it will prompt you requesting the FilePath to the process you want to start, that's because Stop-Process do not return any result to the pipeline and then Start-Process is not receiving anything from the pipeline.

Look here to see how to restart process using PowerShell

And take a look at this MS module Restart-Process

Use this code to run scripts as job:

$days = -1
$currentDate = Get-Date
$DateToDelete = $currentDate.AddDays($days)

start-job -ScriptBlock {
    Get-ChildItem -Path "a path" -include *.txt -Recurse | Where-Object {$_.LastWriteTime -lt $DateToDelete} | Remove-Item -Force
}

start-job -ScriptBlock {
    Get-Process notepad | Stop-Process
}

Shadowfax
  • 556
  • 2
  • 13