fb
Homepage 5 How-to Articles 5 How to Run PowerShell Script on Remote Computer as Administrator

How to Run PowerShell Script on Remote Computer as Administrator

Manage remote endpoints, deploy software and patches with Action1 cloud RMM solution.

Sign up and use free on up to 50 endpoints with no functionality limits or expiration.



March 15, 2019

To optimize IT workload, you must have those sysadmins on your team who know how to automate repetitive tasks by writing scripts — this is where PowerShell skills come in handy. However, running a workstation with standard privileges, you’ll soon find out that it’s impossible to launch a PowerShell script with administrator privileges by right-clicking the script and selecting Run as administrator from the context menu (which is available for most the executable file formats over types of executable). In this article, I’ll show you:

  • How to run a PowerShell script on a remote computer
  • How to run a PowerShell script with administrative privileges.

How to automate administration with Windows PowerShell 2.0 Remoting

PowerShell Remoting (PSRemoting) infrastructure is based on Windows Remote Management version 2.0 (WinRM 2.0), and therefore inherits all the advantages of this technology, among which are transfer encryption encrypting the data transferred, and the ability to work usage of HTTP / HTTPS ports. PSRemoting offers a wide range of ever-useful features to administer remote computers and run commands on them.

Before discovering the PowerShell Remoting potential, you first need to activate it on workstations with administrator or standard privileges. To do so, run the cmdlet (Windows PowerShell command) Enable-PSRemoting. You may also add the -Force key to continue with no confirmation. This command prompt runs WinRM quickconfig, if necessary, and creates exceptions in Windows Firewall so that no further action is needed.

Having done things right, you’ve got access to other computers, and you can remotely execute commands by using the Invoke-Command cmdlet (or its alias — icm):

Invoke-Command -ComputerName Main -ScriptBlock {netsh interface dump > c:\ipconfig.txt}

You adjust the command specifying the -ComputerName parameter with one or multiple workstations. 

Meanwhile, you’ll find the following sequence quite useful as it displays the version of the Explorer.exe file from three computers at once:

$Command = {(get-item c:\Windows\explorer.exe).VersionInfo.FileVersion} Invoke-Command -ComputerName Main, Server7, Replica -ScriptBlock $Command

invoke command

Running commands grouped in one block at once, you place their execution results on several computers into a variable and process them on endpoints using Windows PowerShell.

To start an interactive session with a single remote computer, use the Enter-PSSession. To exit the session, type the Exit-PSSession cmdlet or simply click Exit.

The New-PSSession cmdlet creates sessions with remote computers, whose pointers can be placed in a variable and pass it as an argument for Invoke-Command to execute commands on multiple computers at once in a persistent environment. Below, you’ll find an example of running a sequence of commands simultaneously on remote computers from the list c: \ computers.txt.

InvokeCommand2

How to Run Powershell Script as Administrator

Method 1: Modify a Script to Force Elevation

If you use the code snippet (see below) at the start of your PowerShell script, you run the UAC credential prompt requiring an administrator’s approval. After you’ve resolved the request, the code is being processed by a computer.

The code snippet:

param([switch]$Elevated)

function Check-Admin {

$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())

$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)

}

if ((Check-Admin) -eq $false) {

if ($elevated)

{

# could not elevate, quit

}

else {

Start-Process powershell.exe -Verb RunAs -ArgumentList (‘-noprofile -noexit -file “{0}” -elevated’ -f ($myinvocation.MyCommand.Definition))

}

exit

}

The first step to Run Remote Powershell Script as Administrator is to Modify a Script to Force Elevation

Method 2: Run a Code from an Elevated Instance of the Windows PowerShell Integrated Scripting Environment (ISE)

Another way of running PowerShell scripts as an administrator is to run scripts using the Windows PowerShell ISE. To start the ISE with administrator privileges:

  • Click on the Start menu (or the Windows key).
  • Type powershell ise in the search box, and select Windows PowerShell ISE
  • Press CTRL+SHIFT+ENTER to start the ISE (enter the administrator credentials if prompted).
start powershell ise
  • In the PowerShell ISE window, you go to the File drop-down menu and choose the Open option to load your script.
Run Remote Powershell Script as Administrator. Select Open from the File menu to load your script
  • After the script is loaded into the ISE, press F5 to run the script.

Action1’s Remote Administration Tool to Run PowerShell Script Remotely

Action1’s intuitive dashboard helps optimize routine tasks, significantly scaling up IT productivity.

Step 1: After logging into the Action1 dashboard, in the Navigation pane (the left column), select Managed Endpoints and mark the endpoint, for which you are going to run a remote PowerShell script.

managed endpoints kb

Step 2: Then click on the More Actions menu and select the Run PowerShell option.

Run PowerShell option

Step 3: In the box, type the respective command to run a remote Powershell script.

enter Invoke command

Step 4: In the Select Managed Endpoints window, you mark those endpoints on which you are going to run a remote PowerShell script. You can add all of the available endpoints or mark them one by one. 

select managed endpoints kb

Step 5: Schedule the action (Run now/ No schedule yet/ At specific time/ Repeat) and Finish.

schedule and finish kb

Action1’s RMM Solution

Staying competitive in the market is always a challenge, and loud words don’t do wonders for scaling up your business. But actions do! With cloud-based RMM solution Action1, your IT department will timely deploy patches and updates, manage IT assets, access and troubleshoot remote desktops, and complete many other complex tasks.

Read the TechRadar review of our product or be the judge — make the most of a free trial.

Related Articles

How To Delete User Profiles Remotely with PowerShell

When a user logs onto the computer for the first time (not via the network to access shared folders or printers), Windows creates a user profile. Among its contents are the NTUSER.DAT file (user profile settings), user-specific folders (My Documents, Desktop, etc.),...

About Action1 RMM

Action1 RMM is a cloud-based IT solution for remote monitoring and management, patching, and remote support.

Start your free two-week trial of Action1, or use RMM tools for free forever on 50 endpoints with no functionality limitations!



0 Comments

Submit a Comment

Your email address will not be published.

cloud patch management solutions action1

MSP Solution

Centralize endpoint management and boost efficiency of IT service delivery.

automated server patch management action compliance

Patch Management

Identify and deploy missing OS and third-party software updates.

cloud software deployment tools windows

Software Deployment

Distribute software and updates across managed endpoints.

software distribution tools software inventory action1

IT Asset Inventory

Keep a detailed inventory and manage hardware and software assets.

web client remote desktop

Remote Desktop

Support users via seamless remote desktop connection.

web based rdp client

Unattended Access

Provide administrative support and manage remote devices.

automated patch management action1

Endpoint Management

Run PowerShell, custom scripts, reboot computers and restart services.

API integrations action1

RESTful API

Integrate Action1 RMM to your IT ecosystem.

computer inventory tool for compliance

Reports and Alerts

Conduct endpoint security audits with comprehensive reporting.