Because of the only build-in solution to get the datalog-files of a Siemens S7-1200 is through the webservice, I wrote a script that automatically
download all datalogs. ‘Additional the script can delete all log files of the machine with a parameter switch.

The first step of the script is to login in to the webservice to create an actually cookie with the tool wget. With the session IDs I can get all available datalogs and start the download with the help of curl.

The script is not verfy flexibel, so you have to change a few lines to environment.

First install GnuWin32 -> to get wget for Windows. Download

Copy all files to C:\Curlwget\

Open the script with any editor and change the IP-Adresses:
192.168.xxx.xxx

Also change the path where the datalog files will be saved:
Line 36 “\\server\share\”

To automatically download the files create a task in the Taskplaner:
Programm/script: powershell.exe
Add arguments (Optional): -command “& ‘C:\Curlwget\Download.ps1′
Start in (optional): C:\Curlwget

To delete all log files, run the script with the parameter -loeschen.

That’s it.

Skript:

Param([switch]$loeschen=$false)
write-host $loeschen
(C:\Curlwget\GnuWin32\bin\wget --user-agent=Mozilla --no-check-certificate --cookies=on --save-cookies C:\Curlwget\cookies.txt --keep-session-cookies --post-data "Login=rc&Password=rc" https://192.168.xxx.xxx/FormLogin)
Remove-Item "C:\Curlwget\Default.mwsl"

$ad_secure_session = Get-content C:\Curlwget\cookies.txt | select-string -Pattern siemens_ad_secure_session
$ad_session = Get-content C:\Curlwget\cookies.txt | select-string -Pattern siemens_ad_session

$ad_secure_session = $ad_secure_session.toString().SubString(54)
$ad_session = $ad_session.toString().SubString(48)
write-host C:\Curlwget\curl.exe "https://192.168.xxx.xxx/Portal/Portal.mwsl?PriNav=FileBrowser&Path=/DataLogs/" -H "Cookie: siemens_automation_language=de; siemens_ad_session=$ad_session; siemens_ad_secure_session=$ad_secure_session" --compressed --insecure
$output = (C:\Curlwget\curl.exe "https://192.168.xxx.xxx/Portal/Portal.mwsl?PriNav=FileBrowser&Path=/DataLogs/" -H "Cookie: siemens_automation_language=de; siemens_ad_session=$ad_session; siemens_ad_secure_session=$ad_secure_session" --compressed --insecure) | Out-String

$anfang = Select-String "<input type='hidden' name='OldName' value='" -input $output -AllMatches
$ende = Select-String "csv'>" -input $output -AllMatches

$dateien = @()
for($i=0;$i -ne $anfang.matches.count;$i++)
{
	$temp = $ende.matches.index[$i] - $anfang.matches.index[$i]
	$dateien += $output.Substring($anfang.matches.index[$i]+43,$temp-40)

}

if($loeschen){
$dateien | foreach-object{
$_
if($_ -ne "Vorlage44.csv") {
(C:\Curlwget\curl.exe "https://192.168.xxx.xxx/FileBrowser/DeleteFile?Path=/DataLogs/$_&x=9&y=5" -H "Referer: https://192.168.xxx.xxx/Portal/Portal.mwsl?PriNav=FileBrowser&Path=/DataLogs/" -H "Cookie: siemens_automation_language=de; siemens_ad_session=$ad_session; siemens_ad_secure_session=$ad_secure_session" -H "Connection: keep-alive" --compressed --insecure)
}
}
}else{
#Dateien löschen
$dateien | foreach-object{
$_
(C:\Curlwget\curl.exe "https://192.168.xxx.xxx/FileBrowser/Download?Path=/DataLogs/$_" -H "Referer: https://192.168.xxx.xxx/Portal/Portal.mwsl?PriNav=FileBrowser&Path=/DataLogs/" -H "Cookie: siemens_automation_language=de; siemens_ad_session=$ad_session; siemens_ad_secure_session=$ad_secure_session" -H "Connection: keep-alive" --compressed --insecure --output "\\server\share\$_")

}
}

Wenn dir dieser Artikel gefallen hat, kannst du ihn mit einem Klick auf die folgenden Buttons weiterempfehlen. Möchtest du in Zukunft keine neuen Beiträge mehr verpassen? Dann abonniere doch den RSS Feed dieses Blogs. Vielen Dank!