Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set-Content : Access to the path 'C:\Test.html' is denied. #81

Closed
PrzemyslawKlys opened this issue Nov 17, 2019 · 3 comments
Closed

Set-Content : Access to the path 'C:\Test.html' is denied. #81

PrzemyslawKlys opened this issue Nov 17, 2019 · 3 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@PrzemyslawKlys
Copy link
Member

Set-Content : Access to the path 'C:\Test.html' is denied.
At C:\Users\przemyslaw.klys\Documents\WindowsPowerShell\Modules\PSWriteHTML\PSWriteHTML.psm1:12230 char:17      
+ ...     $HTML | Set-Content -LiteralPath $FilePath -Force -Encoding $Enco ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Test.html:String) [Set-Content], UnauthorizedAccessException
    + FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.SetContentCommand

Provide workaround in case access to path is denied when saving for EMail/Save-HTML

@PrzemyslawKlys PrzemyslawKlys added bug Something isn't working enhancement New feature or request labels Nov 17, 2019
@xlrod
Copy link
Contributor

xlrod commented Nov 20, 2019

Would the desktop of the current user be a good place to save it to?

# Access Denied
if((Test-Path -LiteralPath $FilePath) -eq $true){
    Write-Verbose "Save-HTML - Saved HTML to file $FilePath"
}
else{
    $NewFilePath = Split-Path -Path "$FilePath" -Leaf
    $ErrorMessage = $_.Exception.Message -replace "`n", " " -replace "`r", " " # Not working for me
    Write-Warning "Save-HTML - Failed with error: $ErrorMessage" # Not working for me
    Write-Verbose "Save-HTML - Saving HTML to file $env:USERPROFILE\Desktop\$NewFilePath"    
    $HTML | Set-Content -LiteralPath "$env:USERPROFILE\Desktop\$NewFilePath" -Force -Encoding $Encoding
    if((Test-Path -LiteralPath $NewFilePath) -eq $true){
        Write-Verbose "Save-HTML - Saved HTML to file $env:USERPROFILE\Desktop\$NewFilePath"
    }
    else{
        Write-Verbose "Save-HTML - Failed"
    }
}

@PrzemyslawKlys
Copy link
Member Author

You may overwrite someone's file. I guess Temp Directory is better place with random file name but with information to screen where it was saved. You also need try/catch. Would you submit this and other stuff as PR's? :-)

@PrzemyslawKlys
Copy link
Member Author

Thank you @xlrod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants