I’m trying to encrypt a file at rest with EFS remotely with WinRM.
I wrote the following code in Powershell:
$username = "DomainUsername"
$password = ConvertTo-SecureString "Password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Invoke-Command -ComputerName RemoteMachineName -Credential $credential -ScriptBlock {
$filePath = "C:PathToFile.txt"
$file = Get-Item $filePath
$file.Encrypt()
}
But I get the following error:
Exception calling "Encrypt" with "0" argument(s): "Access to the path 'C:PathToFile.txt' is denied."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException
+ PSComputerName : RemoteMachineName
I checked the user context with:
Invoke-Command -ComputerName RemoteMachineName -Credential $credential -ScriptBlock {
whoami
}
and it returns the DomainUsername
I run directly on the targeted machine
$filePath = "C:PathToFile.txt"
$file = Get-Item $filePath
$file.Encrypt()
and it encrypts the file correctly.
What can cause the exception?
New contributor
HVL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.