{"id":2026,"date":"2017-02-06T18:00:20","date_gmt":"2017-02-06T17:00:20","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=2026"},"modified":"2017-02-06T18:00:20","modified_gmt":"2017-02-06T17:00:20","slug":"setting-a-windows-cookie-with-powershell-using-internetsetcookie-in-wininet","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2017\/02\/setting-a-windows-cookie-with-powershell-using-internetsetcookie-in-wininet\/","title":{"rendered":"Setting a Windows Cookie with Powershell (using InternetSetcookie in WinInet)"},"content":{"rendered":"<p>As I&#8217;m trying to improve OnedriveMapper, I&#8217;ve been looking into methods to avoid using Browser Emulation to authenticate with Office 365.<\/p>\n<p>This wasn&#8217;t difficult, but storing the cookie posed a challenge. There are no available methods in Powershell to do so, thus I went searching until I ran into a post on <a href=\"http:\/\/stackoverflow.com\/questions\/8232419\/why-this-code-using-internetsetcookie-to-set-cookies-at-a-webbroser-control-is-n\/8239165\" target=\"_blank\">Stackoverflow that shows how to store a cookie using C#<\/a><\/p>\n<p>Since Powershell can eat C#, this ended up being my working code to set a persistent OS cookie from Powershell:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n\n$source=@&quot;\nusing System.Runtime.InteropServices;\nusing System;\nnamespace Cookies\n{\n    public static class setter\n    {\n        &#x5B;DllImport(&quot;wininet.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]\n        private static extern bool InternetSetCookie(string url, string name, string data);\n\n        public static bool SetWinINETCookieString(string url, string name, string data)\n        {\n            bool res = setter.InternetSetCookie(url, name, data);\n            if (!res)\n            {\n                throw new Exception(&quot;Exception setting cookie: Win32 Error code=&quot;+Marshal.GetLastWin32Error());\n            }else{\n                return res;\n            }\n        }\n    }\n}\n&quot;@\n\n$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters\n$compilerParameters.CompilerOptions=&quot;\/unsafe&quot;\n\nAdd-Type -TypeDefinition $source -Language CSharp -CompilerParameters $compilerParameters\n\n&#x5B;DateTime]$dateTime = Get-Date\n$dateTime.AddDays(1)\n$str = $dateTime.ToString(&quot;R&quot;)\n\n&#x5B;Cookies.setter]::SetWinINETCookieString(&quot;https:\/\/cookieURL&quot;,&quot;cookieNAME&quot;,&quot;value;Expires=$str&quot;)\n<\/pre>\n<p>edit: don&#8217;t use the Get-Hotfix PS command before you run above code, for some reason it breaks things.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I&#8217;m trying to improve OnedriveMapper, I&#8217;ve been looking into methods to avoid using Browser Emulation to authenticate with Office 365. This wasn&#8217;t difficult, but storing the cookie posed a challenge. There are no available methods in Powershell to do so, thus I went searching until I ran into a post on Stackoverflow that shows &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2017\/02\/setting-a-windows-cookie-with-powershell-using-internetsetcookie-in-wininet\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Setting a Windows Cookie with Powershell (using InternetSetcookie in WinInet)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[39],"tags":[],"class_list":["post-2026","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=2026"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2026\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=2026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=2026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=2026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}