Task schedule don't run ps script

Hi,I can use this ps script to del snapshot.But I added the script to the task schedule and only saw PowerShell running in the process, but the script is not running

`foreach($vm in (Import-Csv C:\automatic\server-name-linuxpro.csv).vmname){
    if(get-vm $vm | Get-Snapshot -Name "linux patch*" ){
        # Write-Host $vm
        if(((get-vm $vm | Get-Snapshot -Name "linux patch*" -ErrorAction SilentlyContinue).Created).adddays(3) -lt (get-date)){
          # Write-Host $vm
           Get-VM -Name $vm |Get-Snapshot -Name "linux patch*" |Remove-Snapshot -Confirm:$false
           "$(get-date): $vm : remove for $(whoami)">> c:\LOG\Snapshot_del.txt
        }
    }
}`

one way to check if the problem is with the script or the task configuration is to replace this script with a simple script, may be a folder creation in a script and see it executes with the same task.

Another tip here is to avoid multiple Get-Vm usages, do it once and save it in a variable and use variable after that.

1 Like

Try using Start-transcript that way you can check the transcript file and see if the script is doing anything or there might be an error

I have had some issues with our AV solution blocking any scheduled task that invokes PowerShell, make sure it isn’t being blocked by your own AV.

It also might help to share the task details on how you are running the script.

task schedule information
图片
图片
图片

Try changing the Arguments from -command to -file.

image

… that looks weird … try removing the single quotes and the ./ in front of the path. … and use -File instead of -Command as Matt already pointed out. :wink:


I change the arguments,Its’s not work.

Did you check the task schedulers log for the task? Did you try to replace the code of the script with something simple like a file or folder creation as already recommended before? Did you try to add -EP Bypass and -NoProfile to the arguments of the task?

Or, just open a shell and test what you have in your task from there:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "c:\ibm\automatic\automatic_winpro.ps1"

If it runs from there fine, something else is wrong with your task. I am guessing it may be related to your ExecutionPolicy. Just noticed Olaf suggested the same :slight_smile:

$vm = get-vm | Get-Snapshot -Name "linux patch*" -ErrorAction SilentlyContinue

if((($vm).Created).adddays(3) -lt (get-date)){
     #Write-Host (($vm).Created).adddays(3) -lt (get-date)
    Get-VM |Get-Snapshot -Name "linux patch*" |Remove-Snapshot -Confirm:$false
    # “$(get-date): $vm : remove for $(whoami)">> c:\IBM\LOG\Snapshot_del.txt
}

I changed the script to set up bypass, but the scheduled task still didn’t run

Do we really have to request each and every single step we recommended already separately? :thinking:

It’s like discussing bed time with a 10 years old. :smirk:

$Username = 'username'
$Pass = ConvertTo-SecureString "password" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass

I tested login VC and run script to created a new file, but it did not run. The problem is that the scheduled task cannot log in VC

Why don’t you run the scheduled task with the appropriate account?