There are three main ways to run a command as a different user in Powershell, besides the classing Right click shift. This article will show you how to do that, within the same Powershell session.
By the same Powershell session, I mean something like this:

You’re logged on as ITDroplets\UserA.

You have a powershell script/console running as UserA.

Within that powershell script/console, you want to run a command as ITDroplets\UserB.

In the Options below, I will consider the above example and I will run “Get-Process Explorer” as UserB. This is very handy when running elevated commands, for instance when UserA is a standard user account and UserB has local admin rights. Of course, Get-Process Explorer doesn’t really need elevation 🙂
Remember that the examples are super concentrated, which means I didn’t add any check to see if the command ran successfully etc. They’re there as pure examples, you can then shape them to fit your needs.

I don’t want to go too deep, so in this article I will just explain how to send data to your own system on PVOutput and how to set up the account to be able to do so.

Set your PVOutput account

In order to make API calls to PVOutput, against your system, you’ll need to enable API Access and generate a new API Key. You will also need the System Id you want to manage.

This is super easy as it’s all located in the same area of the account settings.

Login to PVOutput

Click Settings

Scroll at the bottom and:

Enable API Access

Click New Key to generate a new Key

Write down your system ID

Save

That’s it! We’re ready to send data to our system.

Note: Before proceeding, make sure your system is correctly set up. You need to ensure you’ve added the right amount of solar panels as well as the correct Watt hour or else you may end up receiving errors.

A very simple script

So, this script is very simple and will just show you how to send your system’s outputs to PVOutput. I strongly recommend adding a Try/Catch to make sure you Catch any error (so that you can also set up an alert, perhaps with Telegram: Automating Telegram Messages with Powershell) and, most importantly, you want to automatically get the date, time etc automatically (look at Get GoodWe data with Powershell to see how I grabbed these info from my inverter).

This post will show you how to get a list of users logged on a list of servers (or a specific server) and how to format the output in order to work with it, in Powershell.

Specifically, we will leverage quser, let’s see a quick example on how to query the current user sessions on a remote server.

1

quser/server:myserver.itdroplets.com

The above command, will look like this.

This is just text however. If we were to assign that result into a variable (with Invoke-Command for instance), it’ll still be unmanageable. We can work with -Replace and with ConvertFrom-Csv in order to get this output to look decent.

Just for the sake of showing every step, the code below will first grab the list of sessions on a remote server and then use the -Replace and the ConvertFrom-Csv.

PowerShell

1

2

3

4

5

6

7

$Server="myserver.itdroplets.com"

#Get all sessions, without working on the output

$Sessions=quser/server:$Server

#Sort the Output (and keep the same variable name)

$Sessions=$Sessions-replace'\s{2,}',','|ConvertFrom-Csv

We’re not out of the woods yet, but here’s how the output looks like now.

I’ve added some color to show you what the problem is at the minute. The output in green is good and it’ll always be good as long as the session is Active. The ones in yellow though have their values shifted up. That’s because SESSIONNAME doesn’t exist and so converting it to CSV shifted all values up.

To fix this, we just need the script to identify all sessions where SESSIONNAME isn’t like “console” and not like “rdp-tcp*“. When this is the case (like in the yellow sessions in the screenshot above), then we need to shift the values and we’re done! So, SESSIONNAME will have to be empty, the ID will match SESSIONNAME, the STATE will match the ID and so on. (more…)

In this article, we will go through building a runbook with Powershell and Telegram to allow us to interact with the script with just a message.

If you haven’t read it yet, have a look at Automating Telegram Messages with Powershell – In there, I build a simple script that helps me interact with users members of a Telegram group. That article has the core of what I’m about to show you now and will also explain how to get a Telegram Bot setup.

So in this specific example, we will build a very simple runbook with Powershell that will integrate with Telegram: this will allow us to “talk” to our powershell script with just a Telegram message. What’s cool about the below, is that you can keep adding “switches” to increase the amount of tasks the script can perform. What’s even cooler is that the script below will allow you to run powershell with a Telegram message and get the output on your phone! In other words, I can tell the script to run whatever command I want. 🙂

Here’s an example of me messaging the Telegram bot with “run get-childitem -path c:\users” and the result (screenshot taken from the web interface of Telegream, but it’s the same on your phone):

The script leverages a “Switch” to handle multiple IFs and specifically, it’ll will follow this flow:

Check for any new Message in the Telegram conversation, in a loop.

Note: To be safe, when the script starts up, it’ll ignore the last message sent, and will be listening for any new message right after that.

To perform this, I used the Message Timestamp that is in Telegram’s json ($LastMessage.message.date).

If a new Message has been received, check with a Switch if the message contains either “run *” and “quit_script”.

If it contains “run *” (example: run Get-ChildItem -Path C:\), it’ll split the string received and run the command in powershell.

If the message is “quit_script”, it’ll exit the script running on the computer.

If it doesn’t contain anything like that (example: Hello), it’ll tell the user that the script doesn’t understand that command.