Invoking CMD from PS-script with Scheduled task
Seems to have been permission problems on the folder wich i wanted to delete files from.
Strangely it worked running the script by itself. However Task Scheduler seems to require some elevated folder security?!
March 10th, 2012 8:51am
To start with - The script functions well when I run it manually. The problem occur when I schedule it via Schedule Task manager.
Here are the basic parts:
if (Test-Path $exists)
{
$smtpServer = "smtp.smtp.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer, 25)
$msg.From = $user
$msg.To.Add($user)
$msg.Subject = "Mail"
$msg.Body = "Automaticly generated E-mail"
foreach ($file in gci $folder)
{
$att = New-Object Net.Mail.Attachment($file.fullname)
$msg.Attachments.Add($att)
}
$smtp.Send($msg)
start-sleep -second 30
$att.Dispose();
$msg.Dispose();
start-sleep -second 30
cmd /c copyandremove.cmd
}
else
{
Write-Host "No files to send was found... Exiting"
exit
}
it is the CMD /C COPYANDREMOVE.CMD that doesn't run when scheduled.
I guess I cannot specify the entire path as there is an @ in the foldername?
The CMD-file is located in the same path as the script itself. I am running the task this way:
"Start a program"
program/script: "powershell"
argument: "-command "&'C:\path\script.ps1'
Does anybody know how to solve this?
A PS copy-file solution would also be fine. This is what I want to do:
@ECHO OFF
REM Remember to change the folder variable below to the correct E-mail
SET FOLDER=c:\person@company.com
XCOPY /s /A /H /Z /Y %folder%\*.* c:\OldFiles\
DEL %folder%\*.* /q /f /s
Thanks!
Free Windows Admin Tool Kit Click here and download it now
March 11th, 2012 3:29am