Evo neceg sto sam nedavno pisao.
Rec je o powershell skripti koja nadgleda odredjeni folder i cim se pojavi *.xml fajl uradi upload na ftp server.
while($true){
$configfilePath = 'E:\ftpxml\config.xml'
$settings = [xml](get-content $configfilePath)
$podesavanja = $settings.RootElement.ftp
###Log file
$LogFileName = (get-date).ToString("yyyyMMdd")
$logFile = "E:\ftpxml\Log\ftp_upload_$LogFileName.log"
$ftpaddress = $podesavanja.ftpaddress
$ftpuser = $podesavanja.username
$ftppass = $podesavanja.password
$copyfrom = $podesavanja.FolderA
$copytoo = $podesavanja.FolderB
$sleep = $podesavanja.waittime
$ftptime = $podesavanja.ftptime
$ftp = "ftp://$ftpaddress/"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpuser,$ftppass)
foreach($item in (dir $copyfrom -Include *.xml)){
Start-Sleep -Seconds $ftptime
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
if($?){
$uploadtime = Get-Date
Add-Content $logFile "$uploadtime File $item Uploaded"
Copy-Item $item.FullName $copytoo
if((Test-Path "$copytoo\$item") -eq 'True'){
$copytime = Get-Date
Add-Content $logFile "$copytime File $item Copyed"
Remove-Item $item.FullName
$removetime = Get-Date
Add-Content $logFile "$removetime File $item Deleted from Source Folder"
}
}
}
Start-Sleep -Seconds $sleep
}
Sva podesavanja su u *.xml fajlu koji izgleda:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<RootElement>
<ftp>
<ftpaddress>ftpServerAdresa:21</ftpaddress>
<username>ftpuser</username>
<password>ftppassword</password>
<FolderA>C:\test1</FolderA>
<FolderB>C:\test2</FolderB>
<waittime>60</waittime>
<ftptime>60</ftptime>
</ftp>
</RootElement>
Tako da skripta cita sva podesavanja sa xml fajla, nadgleda FolderA i cim se pojavi *.xml fajl uploaduje na ftp server i zatim kopira fajl u FolderB
u 22. redu skripte se nalazi deo
foreach($item in (dir $copyfrom -Include *.xml)){
gde mozete da izmenite da gleda bilo koju drugu ekstenziju ili ukoliko se ukloni -include *.xml gleda sve fajlove u FolderA.
Sve sto se desi, loguje se u folderu koji je definisan u 8. redu
$logFile = "E:\ftpxml\Log\ftp_upload_$LogFileName.log"
tako da bi vam skripta radila vi treba da promenite putanju LOG fajla kao i putanju config fajla u 2. redu.
Ja sam u svakom slucaju skriptu bildovao kao Windows Servis uz pomoc PowerGUI-a
Rec je o powershell skripti koja nadgleda odredjeni folder i cim se pojavi *.xml fajl uradi upload na ftp server.
while($true){
$configfilePath = 'E:\ftpxml\config.xml'
$settings = [xml](get-content $configfilePath)
$podesavanja = $settings.RootElement.ftp
###Log file
$LogFileName = (get-date).ToString("yyyyMMdd")
$logFile = "E:\ftpxml\Log\ftp_upload_$LogFileName.log"
$ftpaddress = $podesavanja.ftpaddress
$ftpuser = $podesavanja.username
$ftppass = $podesavanja.password
$copyfrom = $podesavanja.FolderA
$copytoo = $podesavanja.FolderB
$sleep = $podesavanja.waittime
$ftptime = $podesavanja.ftptime
$ftp = "ftp://$ftpaddress/"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpuser,$ftppass)
foreach($item in (dir $copyfrom -Include *.xml)){
Start-Sleep -Seconds $ftptime
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
if($?){
$uploadtime = Get-Date
Add-Content $logFile "$uploadtime File $item Uploaded"
Copy-Item $item.FullName $copytoo
if((Test-Path "$copytoo\$item") -eq 'True'){
$copytime = Get-Date
Add-Content $logFile "$copytime File $item Copyed"
Remove-Item $item.FullName
$removetime = Get-Date
Add-Content $logFile "$removetime File $item Deleted from Source Folder"
}
}
}
Start-Sleep -Seconds $sleep
}
Sva podesavanja su u *.xml fajlu koji izgleda:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<RootElement>
<ftp>
<ftpaddress>ftpServerAdresa:21</ftpaddress>
<username>ftpuser</username>
<password>ftppassword</password>
<FolderA>C:\test1</FolderA>
<FolderB>C:\test2</FolderB>
<waittime>60</waittime>
<ftptime>60</ftptime>
</ftp>
</RootElement>
Tako da skripta cita sva podesavanja sa xml fajla, nadgleda FolderA i cim se pojavi *.xml fajl uploaduje na ftp server i zatim kopira fajl u FolderB
u 22. redu skripte se nalazi deo
foreach($item in (dir $copyfrom -Include *.xml)){
gde mozete da izmenite da gleda bilo koju drugu ekstenziju ili ukoliko se ukloni -include *.xml gleda sve fajlove u FolderA.
Sve sto se desi, loguje se u folderu koji je definisan u 8. redu
$logFile = "E:\ftpxml\Log\ftp_upload_$LogFileName.log"
tako da bi vam skripta radila vi treba da promenite putanju LOG fajla kao i putanju config fajla u 2. redu.
Ja sam u svakom slucaju skriptu bildovao kao Windows Servis uz pomoc PowerGUI-a
Нема коментара:
Постави коментар