{"id":505,"date":"2015-12-30T11:34:20","date_gmt":"2015-12-30T10:34:20","guid":{"rendered":"http:\/\/hamsterhirn.de\/?p=505"},"modified":"2016-03-12T17:53:39","modified_gmt":"2016-03-12T16:53:39","slug":"powershell-outlook-scripting","status":"publish","type":"post","link":"https:\/\/hamsterhirn.de\/index.php\/2015\/12\/powershell-outlook-scripting\/","title":{"rendered":"PowerShell Outlook Scripting"},"content":{"rendered":"<p>This small PowerShell example opens my Outlook and searches for a inbox folder called &#8222;Planungen&#8220;.<br \/>\nIt&#8217;s going through all mail and processes them if they have special keywords in them like <em>Planung<\/em> and <em>KAR<\/em>. If there are keywords like <em>NB<\/em> or <em>Nachberechnung<\/em> it&#8217;s not processed. Then it saves the attachment to a temporary Directory and uses pscp to copy it over to a linux machine. It uses my Windows kerberos key to authenticate via GSSAPI so I don&#8217;t need a RSA Key or password. It&#8217;s executed each day as a scheduled job. Neat, isn&#8217;t it?<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n$pscp_path=&quot;C:\\Users\\someuser\\Downloads\\pscp.exe&quot;\n$linux_path=&quot;somehost.de:\/tmp&quot;\n$linux_user=&quot;someuser&quot;\n$filepath = &quot;C:\\temp\\&quot;\n$date = get-date -format d\n$time = get-date -format h-mm\n$log = &quot;C:\\Temp\\Logs\\parameterpflege_&quot; + $date + &quot;_&quot; + $time + &quot;.log&quot;\n\n$olFolderInbox = 6 \n$outlook = new-object -com outlook.application; \n$ns = $outlook.GetNameSpace(&quot;MAPI&quot;);\n$inbox = $ns.GetDefaultFolder($olFolderInbox)\n$planungsordner=$inbox.Folders | where-object { $_.name -eq &quot;Planungen&quot; }\n$messages = $planungsordner.items \n\nforeach($message in $messages){\n&nbsp;&nbsp;If ($message.subject -match &quot;.*Planung.*&quot; -And $message.subject -match &quot;.*KAR.*&quot; -And $message.UnRead -eq &quot;$true&quot; -And (!($message.subject -match &quot;.*NB.*&quot; -Or $message.subject -match &quot;.*Nachberechnung.*&quot;))) {\n&nbsp;&nbsp;&nbsp;&nbsp;Start-Transcript -Force -Path $log\n&nbsp;&nbsp;&nbsp;&nbsp;$subject=$message.subject\n&nbsp;&nbsp;&nbsp;&nbsp;write-host &quot;bearbeite Mail &quot;&quot;$subject&quot;&quot;&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;# save attachment\n&nbsp;&nbsp;&nbsp;&nbsp;$message.attachments|foreach {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$attachmentname = $_.filename\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If ($attachmentname.Contains(&quot;doc&quot;)) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Write-Host &quot;save $attachmentname to $filepath&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$_.saveasfile((Join-Path $filepath $attachmentname)) \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} \n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;# mark as read so it&#039;s not gonna processed again\n&nbsp;&nbsp;&nbsp;&nbsp;$message.UnRead = $false\n&nbsp;&nbsp;&nbsp;&nbsp;$docfile = &quot;$filepath&quot; + $attachmentname\n&nbsp;&nbsp;&nbsp;&nbsp;Write-Host &quot;kopiere $docfile nach $linux_user@$linux_path&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;&amp;$pscp_path $docfile $linux_user@$linux_path\n&nbsp;&nbsp;&nbsp;&nbsp;Stop-Transcript\n&nbsp;&nbsp;}\n}<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This small PowerShell example opens my Outlook and searches for a inbox folder called &#8222;Planungen&#8220;. It&#8217;s going through all mail and processes them if they have special keywords in them like Planung and KAR. If there are keywords like NB or Nachberechnung it&#8217;s not processed. Then it saves the attachment to a temporary Directory and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[255,87,180,253,254,256],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-it","tag-gssapi","tag-linux","tag-outlook","tag-powershell","tag-pscp","tag-scripting"],"_links":{"self":[{"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/comments?post=505"}],"version-history":[{"count":3,"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/posts\/505\/revisions\/525"}],"wp:attachment":[{"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/media?parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/categories?post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hamsterhirn.de\/index.php\/wp-json\/wp\/v2\/tags?post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}