Home > Articles > Programming > Windows Programming

Running Windows PowerShell from Your Smartphone

You can't be everywhere at once. Or can you? If you need to implement emergency server fixes from a remote location, Timothy Warner, author of Sams Teach Yourself Windows PowerShell 5 in 24 Hours, shows you how PowerShell makes it possible.
Like this article? We recommend

Imagine that you're on vacation with your family, enjoying the sunshine, when you receive an emergency call from your IT department colleague back at the office. It turns out one of your infrastructure servers is messed up, and the fix can be accomplished by running a single PowerShell pipeline.

The trouble is that you're on the beach with only your smartphone. Sure, you can walk through the PowerShell with your colleague, but the chances are good that he or she is PowerShell-phobic. Wouldn't it be awesome if you could just send that PowerShell instruction directly from your smartphone?

I'm leading you a bit, of course, because you absolutely can access a PowerShell console session from just about any desktop or mobile browser in the world that supports cookies, SSL/TLS, and JavaScript—which is the vast majority of them, of course.

In this article, you'll learn how to deploy a PowerShell Web Access (PSWA) gateway to support "PowerShell through a pinhole."

Understanding PowerShell Web Access (PSWA)

PowerShell Web Access (PSWA) is a built-in role in Windows Server 2012 and Windows Server 2012 R2. PSWA combines an Internet Information Services (IIS) web application with the magic of Windows PowerShell remoting to allow administrators to access the PowerShell console host (powershell.exe) from a web browser.

One common use case I've seen is employing the PSWA gateway as a "jump box." Imagine that you want to send PowerShell commands to one or more hosts located on a screened, protected subnet, and firewall/security rules prevent you from interacting with the secure hosts directly. As Figure 1 shows, as long as you have access to a PSWA jump box, and the jump box has permissions to the screened subnet, you can accomplish your goal.

Figure 1

Figure 1 PowerShell Web Access serves as an excellent jump box to manage hosts that are normally out of reach.

The best way to learn how PSWA works is to set it up and work with it. For this example, my environment uses the computers described in the following table.

Fully Qualified Host Name

Computer Role

cli1.company.pri

Windows 10 administrative workstation.

mem1.company.pri

Windows Server 2012 R2 member server that will be our PSWA gateway.

dc1.company.pri

Windows Server 2012 R2 domain controller that is our ultimate management target. In other words, we'll tunnel through the PSWA gateway to get to dc1.

Setting Up the PSWA Gateway

First the possibly bad news: Installing the PowerShell Web Access role involves a server restart if you don't have the IIS bits already installed. Once you get over that hump, mosey on over to your PSWA gateway box (mem1 in my example), start an elevated Windows PowerShell console session, and issue the following one-liner:

Install-WindowsFeature -Name WindowsPowerShellWebAccess -IncludeManagementTools -Restart

We'll use Install-PSWAWebApplication to provision our new IIS website. PSWA requires SSL/TLS, so you need to decide which way you'll address this requirement:

  • Use a test certificate. Install-PSWAWebApplication can actually create a self-signed certificate and even automatically create the IIS binding for you.
  • Use an internal certificate. This option is convenient if your business already has an internal public key infrastructure (PKI).
  • Use a public certificate. This option is obviously the most expensive, but you get built-in public trust.

For a lab or demo environment, the test certificate is fine. Here we go:

Install-PSWAWebApplication -UseTestCertificate

We can open IIS Manager to verify what happened (shown in Figure 2).

Figure 2

Figure 2 Verifying the SSL certificate binding on the PSWA virtual directory.

Note that unless we override with the -WebSiteName parameter of the Install-PSWAWebApplication command, PowerShell Web Access uses the Default Web Site web application. Along the same lines, the gateway installs into a virtual directory named PSWA unless we override with the -WebApplicationName parameter.

Finally, if you're going to use your own SSL certificate, simply leave off the -UseTestCertificate switch parameter and create the IIS binding manually.

Securing the PSWA Gateway

Other than SSL, the PowerShell Web Access gateway denies all connection attempts. To demonstrate, fire up a web browser and attempt to load the gateway. In my environment, mem1 is my gateway, and I used the installation defaults:

https://mem1/pswa

Remember that mem1 is my own server, so you'll need to substitute the name of your machine.

Because I used an untrusted self-signed SSL certificate, I'll have to manually continue the page load as shown in Figure 3.

Figure 3

Figure 3 Self-signed SSL/TLS certificates are fine in development, but not in production.

Unless your user account is granted PSWA gateway access via an authorization rule (which we'll create next), you'll receive an authentication failure upon login. Figure 4 shows the user interface (UI).

Figure 4

Figure 4 You see this error if your user account isn't granted PSWA gateway access through an authorization rule.

If you run Get-PSWAAuthorizationRule, by default you should see no output. Change that result right now by using Add-PSWAAuthorizationRule to enforce your authorization rules. Here are mine:

  • Only members of the Domain Admins global group can access the gateway.
  • The gateway allows remote connections only to the host dc1.company.pri.
  • We'll use the default (open) session configuration for now.

Here's the code:

Add-PSWAAuthorizationRule -UserGroupname 'company\domain admins' -ComputerName dc1.company.pri -ConfigurationName *

The asterisk (*) at the end of the command is a wildcard; I've used it here to denote any PowerShell remoting session configuration. Although the asterisk is fine in dev/test environments, in production you'll want to create granular authorization rules that subscribe to the IT security principle of least privilege.

Now let's run Get-PSWAAuthorizationRule to verify that the gateway accepted the changes:

PS C:\> Get-PswaAuthorizationRule

Id    RuleName         User                   Destination           ConfigurationName
--    --------         ----                   -----------           -----------------
0     Rule 0           company\domain admins  company\dc1           *

Testing Access

From my administrative workstation (cli1), I'll reopen my browser and connect to the PSWA gateway URL. Make it a habit to open the Optional Connection Settings (see Figure 5) and change the default session configuration if necessary. You can also specify alternate credentials.

Figure 5

Figure 5 Open the Optional Connection Settings to use alternate credentials or change the session configuration.

Finally we see the actual PSWA console, as shown in Figure 6. Notice in Figure 6 that $host returns the Server Remote Host. Although the PSWA console looks and behaves a lot like the traditional powershell.exe console host, it's actually a different environment, which operates under different security rules.

Figure 6

Figure 6 We're running Windows PowerShell from a web browser!

Remember that this PowerShell access is available from any web browser that supports HTTPS, browser cookies, and JavaScript. You could conceivably run remote PowerShell commands from your iPhone or Android smartphone from anywhere in the world!

Remember that our authorization rules control the default connection (dc1 in my example). Attempting to connect to any other machine should cause an error.

To ensure that result, we can edit the PSWA authorization rules. In fact, let's quickly look up all the PSWA PowerShell commands related to authorization rules:

PS C:\> Get-Command -Name *authorizationrule*

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Add-PswaAuthorizationRule                          PowerShellWebAccess
Cmdlet          Get-PswaAuthorizationRule                          PowerShellWebAccess
Cmdlet          Remove-PswaAuthorizationRule                       PowerShellWebAccess
Cmdlet          Test-PswaAuthorizationRule                         PowerShellWebAccess

It strikes me as inconsistent and wonky that Microsoft forces us to redefine our authorization rules instead of modifying them in place (with a Set- verb, for instance), but we don't have a choice. Take a look:

Add-PswaAuthorizationRule -ComputerGroupName 'company\secureservers' -ConfigurationName * -RuleName mem1-dc1 -
UserGroupName 'company\domain admins' -Verbose

Thanks to the -RuleName parameter, the auth rule now has a friendly name. For this example, I put dc1 and mem1 in a global computer group named SecureServers and used the -ComputerGroupName parameter to bring them into my previously established authorization rule.

To delete a rule, simply use the pipeline:

Get-PSWAAuthorizationRule -Rule 'mem1-dc1' | Remove-PSWAAuthorizationRule -Force

Next Steps

In my opinion, your next step is to deep-dive into session configuration files so you can get a handle on how to limit a remote user's capability in a session. For example, you might give domain administrators full access to all infrastructure servers, but only allow junior administrators to run Get- commands against specific servers.

To learn how to control session configuration, check any of the following references:

Thanks for reading, and happy PowerShelling!

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020