Reputation: 37059
I'm trying to write a PowerShell script which minifies a directory full of .js files on the webserver. The files belong to me; I created it; I'm logged in as me. I can open and save the files in a text editor or via a batch file. The PowerShell script thinks it's operating as me ($env:username
), and I'm running it from a PowerShell console that I ran with "Run As Administrator".
The loop looks like this. $files is confirmed to be a collection of file objects representing the files I want.
$tool = "c:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\ajaxmin.exe";
foreach($ofile in $files) {
$outfile = $ofile.DirectoryName + "\" + $ofile.BaseName + ".min.js";
& $tool $ofile.FullName > $outfile;
}
I get the following error on each file:
Access to the path '\\webserver\inetpub\whatever\js\validation.min.js' is denied.
At C:\minifyall.ps1:18 char:27
+ & $tool $ofile.FullName > <<<< $outfile;
+ CategoryInfo : OpenError: (:) [], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure
The file there is the file it ought to be writing to, and get-acl tells me I have Allow FullControl. It can read the non-.min files just fine.
If I change the output directory to the current working directory (on my own machine), the script works as expected:
# No prob
$outfile = $ofile.BaseName + ".min.js";
Is there something else I need to do to enable this script to write to remote files? Other than rewriting it in Perl (Update: In the end, I rewrote it in Perl and as of February 2020 have had no trouble with it. This is not an endorsement of Perl).
Tips on PowerShell coding style are welcome too. My professional experience with PowerShell began approximately an hour after breakfast today.
Upvotes: 0
Views: 15979
Reputation: 201692
Keep in mind that file ACLs on the remote machine that grant you permission isn't enough. You also need permission to write on the share. Go to the remote machine, right click the folder, go to the Sharing tab, Advanced Sharing and click the Permissions button. Check those permissions to make sure you have write privileges.
Upvotes: 0
Reputation: 24071
Try and print all the full paths in order to see there isn't anything funny going on.
Another a reason could be that, since the target files are Javascript, some security system prevents access. Are you running an antivirus?
If all else fails, use Procmon to see what exactly is going on on the filesystem.
Upvotes: 1