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

How can I successfully run my remote script with administrator permissions?

$
0
0

I've been working on a small, redeployable .ps1 script that will be used to automate the clearing of the end user's print spooler and clearing of the printing queue, with more features in future implementation. Here's a snippet of some of the commands that require elevation.

    net stop spooler

   Remove-Item C:\Windows\System32\spool\PRINTERS\* -Force

   net start spooler

In order to successfully run this script on end user's computers, I've created a Powershell credential object like so. (It doesn't matter if the administrator credentials are in clear text, since it is executed in the background over RMM).

$password = "adminpassword" | ConvertTo-SecureString -asPlainText -Force
$username = "adminusername"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)

However, when I try to run any command with the newly made variable like this,

net stop spooler -credential $credential

I get some sort of complication, such as the PS window only returning the syntax of the command instead of actually running it. I'm relatively inexperienced in the Powershell automation; If someone could offer up a solution as to how to best integrate the newly made credential object into my script so that it will run with elevated permissions in its entirety, It'd be much appreciated. Thanks!!!



Viewing all articles
Browse latest Browse all 21975

Trending Articles



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