Gem nedenstående i en .ps1 fil, opsæt scheduleren til at køre powershell med argumentet "-command <sti>\<script>.ps1". Det kan også være du skal sætte powershells execution policy til "Remote Signed" før det virker korrekt, det afhænger lidt af hvordan maskinen er konfigureret.
Den uploader alle .csv filer i samme mappe som scriptet, men du kan jo bare tilpasse den efter behov.
Det er også muligt at få tilsendt mails ved endt upload eller hvis der opstår fejl.
# Variables
$Ftp = '
ftp.domain.com/'" target="_blank">ftp://username:password@
ftp.domain.com/'$FileType = "*.csv"
$SmtpHost = "mail.domain.com"
Try
{
# Initialization
$WebClient = New-Object System.Net.WebClient
$SmtpClient = New-Object System.Net.Mail.SmtpClient
$SmtpClient.Host = $SmtpHost
$Files = @(Get-ChildItem .\ -Filter $FileType)
If ($Files.Count)
{
ForEach($File in $Files)
{
Write-Host "Uploading: $File"
$Uri = New-Object System.Uri($Ftp + $File.Name)
$WebClient.UploadFile($Uri, $File.FullName)
}
$Count = $Files.Count
$Body = "$Count files were uploaded to server."
}
Else
{
Write-Host "No files to upload!"
$Body = "No files to upload to server."
}
# Uncomment to get status e-mails
# $SmtpClient.Send("noreply@domain.com", "recipient@domain.com", "Upload report", $Body)
}
Catch [Exception]
{
$Body = $_.Exception.Message
# Uncomment to get error mails
# $SmtpClient.Send("noreply@domain.com", "recipient@domain.com", "Error in upload", $Body)
throw $_
}