Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Logon script : Invoke other user for management task

$
0
0

Hello,

When my user logon, a logon script is run. This logon script should delete a file on the computer. But, the user doesn't have right on that file. I have try this : 

$pwd = ConvertTo-SecureString "MyPassword" -asplaintext -force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\ACCOUNT_WITH_RIGHTS",$pwd

$PSSession = New-PSSession -Credential $cred

Invoke-Command -Session $PSSession -ScriptBlock{

   param ($pathtodelete)
   $tmp1 = "File to delete "  + $pathtodelete
   $tmp2 = [Environment]::Username
   Write-EventLog -LogName "Application" -Source "Application" -EventId 9876 -EntryType "Warning" -Message $tmp2 -Category 0 -ErrorAction SilentlyContinue
   Write-EventLog -LogName "Application" -Source "Application" -EventId 9876 -EntryType "Warning" -Message $tmp1 -Category 0 -ErrorAction SilentlyContinue

   Remove-Item -Path $pathtodelete

} -ArgumentList "C:\MyPath\filetodelete.ini"

I have write to the event log some debug line. I can see in the event the correct name ACCOUNT_WITH_RIGHTS. For me, the invoke command work. But, the Remove-Item doesn't work.

I have this for the error :

Accès refusé
    + CategoryInfo          : PermissionDenied: (C:\MyPath\filetodelete.ini:String) [Remove-Item], UnauthorizedAccessException+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand+ PSComputerName        : localhost

Can you help me ?




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>