[
  {
    "Id": "515836",
    "ThreadId": "233140",
    "Html": "\r\n<p>I'm writing a PowerShell script which zips up log files. Everything seems to work OK until the call to Save, at which point I get this error:</p>\r\n<p style=\"color:red\">Exception calling &quot;Save&quot; with &quot;1&quot; argument(s): &quot;Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.&quot;<br>\r\nAt U:\\scriptdev\\logarchiver\\logarchiver.ps1:27 char:15<br>\r\n&#43;&nbsp;&nbsp;&nbsp;&nbsp; $zipFile.Save &lt;&lt;&lt;&lt; ($zipFileName)<br>\r\n&nbsp;&nbsp;&nbsp; &#43; CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotSpecified: (:) [], MethodInvocationException<br>\r\n&nbsp;&nbsp;&nbsp; &#43; FullyQualifiedErrorId : DotNetMethodException</p>\r\n<p>The script works fine if I copy it to a folder on C: and run it from there. From what I can gather on Google, this is some sort of trust issue, but it seems like the utilities for setting trust (&quot;gacutil&quot; and &quot;caspol&quot;) are unavailable/deprecated since .NET\r\n Framework 4? Any ideas how to solve this?</p>\r\n<p>Here's the script in question:</p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:green\"># Log Archiver</span>\r\n<span style=\"color:green\"># Checks to see if we have a month's worth of log files, then zips them up</span>\r\n\r\n<span style=\"color:green\"># Load DotNetZip Library</span>\r\n<span style=\"color:orangered\">$dotNetZip</span> <span style=\"color:gray\">=</span> Resolve<span style=\"color:gray\">-</span>Path <span style=\"color:#a31515\">&quot;Ionic.Zip.dll&quot;</span>\r\n<span style=\"color:gray\">[</span><span style=\"color:teal\">System.Reflection.Assembly</span><span style=\"color:gray\">]</span><span style=\"color:gray\">::</span>LoadFrom(<span style=\"color:orangered\">$dotNetZip</span>) | Out<span style=\"color:gray\">-</span>Null\r\n\r\n<span style=\"color:green\"># Get all log files in current folder, sorted by last write time</span>\r\n<span style=\"color:orangered\">$logFiles</span> <span style=\"color:gray\">=</span> Get<span style=\"color:gray\">-</span>ChildItem | Where {<span style=\"color:orangered\">$_</span>.extension <span style=\"color:gray\">-eq</span> <span style=\"color:#a31515\">&quot;.log&quot;</span>} | Sort<span style=\"color:gray\">-</span>Object LastWriteTime\r\n<span style=\"color:green\"># Find date of earliest log file</span>\r\n<span style=\"color:orangered\">$earliestLog</span> <span style=\"color:gray\">=</span> <span style=\"color:orangered\">$logFiles</span><span style=\"color:gray\">[</span><span style=\"color:teal\">0</span><span style=\"color:gray\">]</span>.LastWriteTime\r\n<span style=\"color:green\"># Find current path</span>\r\n<span style=\"color:orangered\">$currentPath</span> <span style=\"color:gray\">=</span> Resolve<span style=\"color:gray\">-</span>Path <span style=\"color:orangered\">$logFiles</span><span style=\"color:gray\">[</span><span style=\"color:teal\">0</span><span style=\"color:gray\">]</span>.Name | Split<span style=\"color:gray\">-</span>Path\r\n<span style=\"color:orangered\">$monthAfterEarliest</span> <span style=\"color:gray\">=</span> <span style=\"color:orangered\">$earliestLog</span>.AddMonths(1)\r\n<span style=\"color:orangered\">$currentDate</span> <span style=\"color:gray\">=</span> Get<span style=\"color:gray\">-</span>Date <span style=\"color:#a31515\">&quot;8/11/2010&quot;</span> <span style=\"color:green\"># date is there for testing purposes, remove for live usage</span>\r\n<span style=\"color:green\"># Compare</span>\r\n<span style=\"color:blue\">if</span> (<span style=\"color:orangered\">$currentDate</span> <span style=\"color:gray\">-gt</span> <span style=\"color:orangered\">$monthAfterEarliest</span>) {\r\n\t<span style=\"color:green\"># logs need archiving</span>\r\n\t<span style=\"color:orangered\">$zipFileName</span> <span style=\"color:gray\">=</span> <span style=\"color:orangered\">$currentPath</span> <span style=\"color:gray\">&#43;</span> <span style=\"color:#a31515\">&quot;\\logarchive-&quot;</span> <span style=\"color:gray\">&#43;</span> <span style=\"color:orangered\">$currentDate</span>.ToString(<span style=\"color:#a31515\">&quot;yyyy-MM-dd&quot;</span>) <span style=\"color:gray\">&#43;</span> <span style=\"color:#a31515\">&quot;.zip&quot;</span>\r\n\t<span style=\"color:orangered\">$zipFile</span> <span style=\"color:gray\">=</span> New<span style=\"color:gray\">-</span>Object Ionic.Zip.ZipFile\r\n\t<span style=\"color:blue\">foreach</span> (<span style=\"color:orangered\">$logFile</span> <span style=\"color:blue\">in</span> <span style=\"color:orangered\">$logFiles</span>) {\r\n\t\t<span style=\"color:orangered\">$fileName</span> <span style=\"color:gray\">=</span> Resolve<span style=\"color:gray\">-</span>Path <span style=\"color:orangered\">$logFile</span>.Name\r\n\t\t<span style=\"color:orangered\">$zipFile</span>.AddFile(<span style=\"color:orangered\">$fileName</span>,<span style=\"color:#a31515\">&quot;&quot;</span>) | Out<span style=\"color:gray\">-</span>Null\r\n\t}\r\n\t<span style=\"color:orangered\">$zipFile</span>.Save(<span style=\"color:orangered\">$zipFileName</span>)\r\n\t<span style=\"color:orangered\">$zipFile</span>.Dispose()\r\n\t<span style=\"color:green\"># zipping complete, delete log files</span>\r\n\t<span style=\"color:blue\">foreach</span> (<span style=\"color:orangered\">$logFile</span> <span style=\"color:blue\">in</span> <span style=\"color:orangered\">$logFiles</span>) {\r\n\t\t<span style=\"color:orangered\">$fileName</span> <span style=\"color:gray\">=</span> Resolve<span style=\"color:gray\">-</span>Path <span style=\"color:orangered\">$logFile</span>.Name\r\n\t\tRemove<span style=\"color:gray\">-</span>Item <span style=\"color:orangered\">$fileName</span>\r\n\t}\r\n}\r\n\r\n</pre>\r\n</div>\r\n",
    "PostedDate": "2010-11-02T03:14:26.39-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "518375",
    "ThreadId": "233140",
    "Html": "\r\n<p>As you pointed out, this is not an issue related to DotNetZip.</p>\r\n<p>This is a security issue, related to running powershell scripts from networked drives, if i understand correctly.&nbsp;</p>\r\n<p>I think you would get a similar security exception if you ran a simple PS script from u:\\ that simply created a file on c:\\ , with no use of DotNetZip at all.&nbsp; The error occurs when calling&nbsp;System.IO.File.Create() (or Open()),&nbsp;and it would\r\n occur when running the ps1 script from u:\\, regardless of whether you used a third party library.&nbsp;</p>\r\n<p>The solution is to adjust the security for powershell scripts, or run the script locally.&nbsp; I'm not an expert on powershell security, but I think\r\n<a href=\"http://blogs.msdn.com/b/powershell/archive/2007/05/06/running-scripts-downloaded-from-the-internet.aspx\">\r\nthis blog post</a> may be relevant.</p>\r\n",
    "PostedDate": "2010-11-07T06:42:58.393-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "524243",
    "ThreadId": "233140",
    "Html": "\r\n<p>Thanks for the reply. I tried changing the Powershell security temporarily (I ran it from a batch file with -ExecutionPolicy Bypass) which resulted in a different error:</p>\r\n<p><span style=\"color:#ff0000\">out-lineoutput : Exception getting &quot;formatValueList&quot;: &quot;Microsoft.PowerShell.Commands.Internal.Format.FreeFormatEntry.formatValueList&quot;</span><br>\r\n<span style=\"color:#ff0000\">&nbsp;&nbsp;&nbsp; &#43; CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotSpecified: (:) [out-lineoutput], GetValueInvocationException</span><br>\r\n<span style=\"color:#ff0000\">&nbsp;&nbsp;&nbsp; &#43; FullyQualifiedErrorId : CatchFromBaseAdapterGetValue,Microsoft.PowerShell.Commands.OutLineOutputCommand</span></p>\r\n<p>Googling this gives only one result: <a href=\"http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/dd5dcae2-1ccc-4be2-b986-61c069102ffb\">\r\nhttp://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/dd5dcae2-1ccc-4be2-b986-61c069102ffb</a>&nbsp;which curiously enough also relates to DotNetZip.</p>\r\n<p>However, for the moment I've been able to arrange things so the script can be run from a local location on a server.</p>\r\n",
    "PostedDate": "2010-11-19T01:58:27.147-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "524285",
    "ThreadId": "233140",
    "Html": "\r\n<p>Wow, that error message is astoundingly opaque.</p>\r\n<p>I don't know what to make of it.</p>\r\n",
    "PostedDate": "2010-11-19T03:59:48.92-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]