question

PersonnePersonne-8433 avatar image
0 Votes"
PersonnePersonne-8433 asked MotoX80 commented

Cannot save XML file when file attrbute is hidden

Hello guys,

Kind of a weird behavior, when I open a hidden file, then I cannot save it.
Any idea why ?

Here is the code, nothing special.
If I save the result into another file, it works fine.
But if the source file is hidden, and I try to save the modification to the save file, it will fail

I know the work around, removing the hidden attribute from the file, save it, and then set the file as hidden again.

I'm trying to understand with the .Save() method cannot save directly to a hidden file.

Thank you


 $gpopath = 'D:\Mes documents\GPO\user3'
 $file = 'manifest.xml'
 $manifest = join-path $gpopath $file
 [xml]$xmlDoc = Get-Content $manifest
 $xmlDoc.Backups.BackupInst.GPODisplayName.'#cdata-section' = 'add user3'
 $xmlDoc.Save($manifest)


Here is the error:

 Exception calling "Save" with "1" argument(s): "Access to the path 'D:\Mes documents\GPO\user3\manifest.xml' is denied."
 At line:7 char:1
 + $xmlDoc.Save($manifest)
 + ~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
     + FullyQualifiedErrorId : DotNetMethodException



windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

When you overwrite a file, an UnauthorizedAccessException exception is thrown if the file is hidden.
https://docs.microsoft.com/en-us/dotnet/api/system.io.filemode

You can clear the hidden attribute before overwriting it or you may use Set-Content

 $xmlDoc.OuterXml | Set-Content $manifest

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 commented

Remove the attribute before you call save.

 $manifest = 'c:\temp\foo.xml'
 [xml]$xmlDoc = Get-Content $manifest
 (Get-Item $manifest -force).Attributes                       # show attributes 
 (Get-Item $manifest -force).Attributes -= 'Hidden'           # remove hidden 
 $xmlDoc.save($manifest) 
 (Get-Item $manifest -force).Attributes += 'Hidden'           # hide it


https://social.technet.microsoft.com/Forums/en-US/33665b03-d383-41ed-a836-fd83c217b3f1/making-files-hidden-with-powershell

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi MotoX, thank you for your answer but as I stated I already know the workaround

'I know the work around, removing the hidden attribute from the file, save it, and then set the file as hidden again.'

I'm more looking for an explanation of why $xmlDoc.Save($manifest) cannot work on hidden file

0 Votes 0 ·
MotoX80 avatar image MotoX80 PersonnePersonne-8433 ·

I didn't write the code, but taking an educated guess I would say that they had to do something to account for the fact that a user must have had a reason to hide the file in the first place. A better question would be; why is there no override on the .save method to do what the -force switch option does on the Get-Item cmdlet.

Something like:


 $xmlDoc.save($manifest,$true)


0 Votes 0 ·