Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

PS Script only works in ISE - Creating Scheduled Task

$
0
0



$folder = 'd:\sharesroot\008-upload-stuff-here\'
$filter = '*.*'                            
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$timeStamp = $Event.TimeGenerated

Set-ExecutionPolicy -ExecutionPolicy UnRestricted



$EmailFrom = “EMAIL@gmail.com”
$EmailTo = “EMAIL@gmail.com”
$Subject = “File Added”
$Body = $name
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“USER”, “PASSWORD”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

}

Hi, i have this script and when i select the text in PowerShell ISE and click run --> it runs great.

If i right click on the Scrpt.PS1 File and click run... it does not.

I am trying to set up a scheduled task to run this and its not working however i am convinced the reason is my problem stated above ^^

I tried searching but was unable to resolve, how can i make my script just run by right clicking run, or ... or if i were to make a Scheduled Task, what would i need to run this.

Bonus Question:

I was wondering would any of you know what i'd add to this script to say... exclude a file type such as (  .txt   )??  This way i wouldn't be emailed every time a Text File appears but only other files.

Thank you....

been trying to get this thing working for 1.5 hrs now.


coding noob but trying.


Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>