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


Read More

Automating Telegram Messages with Powershell

In this post I will go through automating Telegram Messages with Powershell, including a full script as an example.

Truth to be told, I’ve installed, and first used, Telegram about 3 hours before writing this post, but I saw so much potentials that I couldn’t wait to publish this. Consider also that I was actually after something similar for WhatsApp, but there’s no official API from them yet.

So because I’m such a noob here, I will actually go through the steps I’ve followed to get a Bot configured to work. Note that a Bot is an easier way to handle this sort automation, but if you’re an advanced user, you could look directly into Telegram’s API which will be way more flexible.

What can I actually use this for?

Well, of course you can just do it for fun and be able to send a message via powershell. But that’d be wasting this great potential. If I look out of the box, I see a possibility to build a (cheap) notification system and/or a (cheap) runbook system.

For example, imagine that you’ve got a script running that right now sends you an email once done just to tell you that the script has finished in 30 minutes. Why an email? Isn’t it handier having an actual push-notification on your phone telling you that?

Let’s think bigger, you’re deploying a new Virtual Machine with an automated script and, besides sending you a report via email, you want to know when it’s done so that you perhaps can go and work on the VM you just deployed, without continuously checking the status of the deployment. Or imagine adding a simple Message after an SCCM Task Sequence has been completed or even just use it to alert in case of a low disk space etc.

Now, what I like the most, what if Powershell can read what we’re writing into that conversation and based on that take actions? Like a runbook. Example: you write “Restart-Computer”. The PS script¬† could have a part of the code that checks every X amount of time if somebody wrote something and if they did, it checks the message. If the message equals to “Restart-Computer” then go and restart the computer. This is a very basic example, but it contain the core of what this can be used for.

Based on this idea, I actually build a very simple runbook automation script to leverage a Telegram message. Check it out here: Building a runbook with Powershell and Telegram

In the example at the end of this article I will be showing you how to send a message to a Telegram group and a possible action take after somebody replies with a specific keyword.

Set a Bot up

I suggest you to use a computer after step 1 as it’s going to be a bit faster in my opinion. This first bit is super simple.

  1. The first thing you want to do is register with Telegram if you haven’t done it yet. To do that, just go and download the app from the store (depending on what smartphone you’ve got).
  2. Launch the BotFather by opening this link:
    • Start a conversation with the BotFather by typing¬†/newbot – This will start the Bot creation wizard.
    • At this point, you will be asked to provide a Friendly name and a username. Once that is done, you will be provided with the token to be used in our scripts.
    • Telegram_botfather_create-bot

Time to intercept the Chat ID and run a quick test

So, in order for us to leverage the Bot to send a message, we will need to get the Chat ID of the conversation we want the Bot to talk/listen in. (more…)

Read More

Get GoodWe data with Powershell

I recently had a few solar panels installed over the roof of my house and right after that I thought, how can I get GoodWe data with Powershell?

GoodWe is the brand of the inverter installed, which connects to my home wifi and sends data automatically to the GoodWe Portal. The model I have is the GW4200D-NS.

I have to say that I wasn’t happy with the app I was provided with, neither was I happy with the portal, which refreshes the¬†Real Time Data every 15 minutes! What kind of real time is that? However, to be fairly honest, why would you want to see actual Read Time readings? Well, because I want to. ūüôā

Before looking for a solution on my own, I always search online, I don’t want to re-invent the wheel, so I found a post where I got the main idea:¬† . This post describes a way of grabbing the data from a non-Windows environment.

Important: I am not (nor is the post above) grabbing the data directly from the inverter, but I am leveraging the GoodWe portal information. An idea I have is to sniff the traffic the inverter sends to goodwe, capture it and re-utilise it, however I’m not too interested in that yet as the solution I have works fine.

Edit 20180605: GoodWe has started using HTTPS, so the previous script got broken because of this. I’ve updated it (just changed the URL from http to https) and the script is working as usual.


Read More

Syncing data to Amazon Cloud Drive via command line

ATTENTION: The below no longer applies due to Amazon changing their mind on both rclone (which has been banned) and their unlimited plan which is no longer unlimited. ūüôĀ However, rclone still works with Google Drive, Dropbox etc.

Amazon Cloud Drive (ACD) would be great if only could come with a better software to support it. Unfortunately, at today (Jan 2017), the application provided by Amazon is kind of useless for any type of user because you have to manually select and sync/upload folders and there’s no way to schedule such process. This is even more annoying if you are already used with¬†Dropbox, One Drive or Google Drive that automatically sync all of your changes.

So I was looking for an application that would allow me to syncing data to Amazon Cloud Drive via command line and, after searching a bit around, I came across¬†rclone ( Personally, I do not like using 3rd party applications, but I totally suggest the use of this one. It¬†doesn’t require to be installed and it’s very small in size (10MB).
It also allow you to sync other cloud accounts to another cloud account: so for instance you could sync everything on your Google Drive to ACD!

With just a single line of code, you will be able to start a sync (one way only), a copy or a check (there are more features available, look on rclone’s website for an up to date list). The only thing that is required before starting syncing data to Amazon Cloud Drive via command line is the initial configuration. In the below procedure, we’ll use the Windows version.

Configuring rclone for the first time

  • Open a new command prompt.
  • Browse to the rclone folder (you may download the latest version from¬†
  • Type the following and press Enter.
  • As this is the first time you run the configuration, rclone will complain that the config file isn’t found. Just press¬†n to create a¬†New remote.
    • rclone_config_first-time
  • You are now prompt for the¬†name. I used “amazonclouddrive”.
  • It’s time to choose the remote service, as you can see there are many available, we will obviously select Amazon Drive by typing the number associated to it, so¬†1.
    • rclone_amazonclouddrive
  • Press enter once prompt for the Application Client ID.
  • Press enter again for the Application Client Secret.
  • Press¬†Y to use the auto config.
    • rclone_application-client-id-blank_application-client-secret-blank_auto-config-yes
  • Now your default browser will open to the Amazon Login page. Login with your credentials and then grant access to rclone.
    • rclone_amazonclouddrive_login
    • rclone_amazonclouddrive_success
    • rclone_amazonclouddrive_success_2
    • Once you’re back in the command prompt, you will see it successfully configured it.
  • You may run through the same steps again with Dropbox, Google Drive or anything else listed in rclone’s config.
  • This is rclone’s official procedure for Amazon Cloud Drive:¬†
  • The configuration is saved by default in the user’s profile folder. You may copy the configuration on other machines (I recommend password protecting the config file though).
    • In order to password protect the config file, run¬†rclone.exe config again, then type¬†s to set a password, type¬†c, and finally specify the password. Next time you run the config, it’ll be asking you for a password.


Syncing data to Amazon Cloud Drive via command line

Now that we have ACD set up, let’s see how to run a basic sync of the folder c:\users\myuser\desktop\mydata to the folder Backups\mydata which sits on ACD:

That simple. Imagine now that you could add this to a complex script or just to a scheduled task.

You can also sync from a network share (and doesn’t need to be mapped):

If you’re copying a folder with a large number of files, I suggest you to increase the number of checkers and the number of transfers (multithreading – Default Checkers is 8 and default Transfers is 4):

Finally, if you want to sync let’s say Dropbox to ACD:

Remember that Dropbox will have to be configured before proceeding. As you can see, I’m selecting the whole Dropbox account.

Read More

Global Service Desk

helpdeskThis article is meant to give more attention to what I didn’t think was a big issue around the IT world: how to get the best our of your Global Service Desk team! I saw a question on LinkedIn where somebody was going to implement a Global Service Desk team and wanted some suggestions and also shared his fear about the possibility of Service Desk representatives leaving tickets in the global queue because “somebody else” will pick them up.

I was the first to answer the question, then over 120 other people did, so I started reading and reading as I was really curious what others thought. Many agreed with me, many gave extra arguments and some where bad comments such as “Don’t centralize it then!“. The problem is not only the fact that they were suggesting not to proceed with what I think it’s an improvement, but these comments were missing the reasons why they shouldn’t have done that. Note also that centralising a team doesn’t mean you have to have them in the same office! They can even be in different Countries.

So this article wants to try to grab all of the comments I found and put them together even though the main steps that somebody must follow to achieve this are pretty much two:

  • Get a Ticketing system in place, possibly a high-customizable one that will allow you to create Categories and Reports.
  • Get one or more Team Leaders/Managers depending on how big the team is.

There are many variables in play, so nobody will be able to describe a combination of scenarios that match your enviornment from an article like this one, but I can try to give a high level of ideas that will help you. One of the things I don’t know for instance is how many other teams you have (i.e. second level support, 3rd level Unix support, 3rd level Infrastructure support, Windows and so on). Let’s just say there’s one or more teams you’re able to escalate tickets to. (more…)

Read More