Run a command as a different user in Powershell

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.

Option 1 –¬†System.Diagnostics.ProcessStartInfo

(more…)

Read More

Resource Win7Only referenced in attribute displayName could not be found

This error appears when you have updated the ADML and ADMX file to Windwos 10, version 1803. Let’s see how to fix it!

When trying to editing a policy, you receive this error:¬†Resource ‘$(string id=Win7Only)’ referenced in attribute displayName could not be found.

This is a known issue for Microsoft, in fact there was an update in the Windows 10 version 1803’s SearchOCR.ADML file and this line was missed in the new ADML file:

<string id=”Win7Only”>Microsoft Windows 7 or later</string>

The way I used to fix this was to manually editing SearchOCR.ADML.

Editing SearchOCR.ADML

  • Make a copy of \Policies\PolicyDefinitions\en-us\searchocr.adml. Normally this path is under \\yourdomain\sysvol\yourdomain. This is needed in case the file you’ll edit gets corrupted.
  • With a text editor, open¬†\Policies\PolicyDefinitions\en-us\searchocr.adml and search for¬†<string id=”OCREveryPage”>Force TIFF IFilter to perform OCR for every page in a TIFF document</string>.
    • The line above this, should be:¬†<string id=”OCR”>OCR</string>
  • Add the following string, right after¬†<string id=”OCR”>OCR</string>:
    • <string id=”Win7Only”>Microsoft Windows 7 or later</string>
  • Save the file and try again (allow some time for replication if you have multiple Domain Controllers).

EDIT: There’s a KB from MS finally released for this HERE.

Read More

Add output to PVOutput with Powershell

In this article, we will see how to setup your PVOutput account in order to add output to PVOutput with Powershell.

I found a ton of info on how to add Ouput to PVOutput with curl, but nearly nothing (as I needed it) for Powershell. No wonder why I received a couple of questions on this.

If you’re the owner of a GoodWe inverter, take a look at¬†Get GoodWe data with Powershell.

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).

Super easy! You can add more functionalities, like I said above. One of them could also be checking for the Content (or Status/Description) that will be held in $PVOutputInvokeResult:

Have fun ūüôā

Read More

Get a list of users logged on a list of servers – Powershell

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.

The above command, will look like this.

quser_simple-example

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.

We’re not out of the woods yet, but here’s how the output looks like now.quser_output-converted-from-csv-after-replace

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…)

Read More

Building a runbook with Powershell and Telegram

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):

Telegram-simple-runbook-example

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.

(more…)

Read More