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: https://brnrd.eu/misc/2016-03-13/goodwe-logging-to-pvoutput.html . 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. (more…)

Read More

Replicate all group members from Group A to Group B in Powershell

This is going to be a very quick article that will show you how to simply replicate all members from a group over to another group with the AD powershell module.

There are mainly two different goals:

  1. You want to replicate all group members from Group A to Group B in Powershell, as they are.
  2. You want to replicate all users that are in Group A recursively to Group B.

Case 1

Simple enough, this will grab every member as it is (either a user, a group or any other object) and add it to Group B.

Case 2

The difference between case 1 and 2 is -Recursive. This will grab all members including members of other groups. For instance if Group A had 3 members, 2 user objects and a group called “Group A1” which then contained 3 users, you will see that Group B will contain just the 5 users and not the groups.

Read More

Nearest Domain Controller without Powershell AD Module

Getting the nearest Domain Controller when the AD module is present, is fairly simple, all you would need to do is running the following:

But what if you want to achieve the same result on a client/server that doesn’t have the Powershell Active Directory modules installed?
Well, in cmd you can do something like this:

The above will come back with quite a few useless (to our scope) pieces of information (or an error).

dsgetdc-nearest-domain-controller-1

 

So we can run this instead, to just get the “DC”:

So now we have just one line with the Domain Controller (or the error).

dsgetdc-nearest-domain-controller-2

Now, let’s try to work with the above command in powershell. What we want to achieve is having a variable ($DC) that will either contain the domain controller name or any other value that we want if there’s an error, for instance we could assign the value $false to it.

The script is pretty crude so that you can modify it as you like; let’s explain what it does. It first tries to run the above command with a slight difference, I added .split(” “) that will automatically split the result in an array of sub-strings. But, if the command fails, this powershell command won’t be able to split anything and will throw an error. This is why we need a Try/Catch.
If the command fails to retrieve a domain controller then, $DC will be $false.
Now, if the .split command works, then the script runs through each object of the array of sub-strings and checks when the sub-string starts with two backslashes: that means that we’ve got what we’re looking for!.
Finally, $DC will get the name of the domain controller assigned . Note that .replace(‘\\’,”) will remove the two backslashes and will leave us just the domain controller’s hostname.
I hope this explains a bit more the idea behind it.

Read More

How to run a powershell script in the background

This article is meant to give a quick and simple example on how to run a powershell script in the background (hide the console) without using a 3rd party script (VBS scripts is what you see a lot on the internet).
Now, consider that the method I will show you will still show the Powershell console for a few instants before disappearing.

For instance, on a computer I own, I have a script that starts every hour and runs only when the user is logged on. The powershell console will appear in front of everything else, making a bit annoying, especially if you’re watching a movie or working on something.

So, in order to run the script in the backgroup, add the following code at the beginning of the script:

You may even keep the console opened and decide to hide it in the middle of the script, for instance:

Read More

Breaking a nested ForEach in Powershell

Breaking a nested ForEach in Powershell might be challanging if you don’t know that you can use labels to point the script to another location. Normally, you will require to stop a loop when you’re comparing objects from different arrays and want to terminate the loop when they match. There’s a command called “break” which is the key to breaking a nested ForEach in Powershell, however using just break will actually break all of the loops where the command is contained.

Let’s make an example. Let’s say we have two arrays, $arrayA and $arrayB. $arrayA contains the letters “a”, “d” and “f” whilst $arrayB contains “a”, “b”, “c” and “d”. We want to check each single element in $arrayA against each single element in $arrayB. This is when we will need a nested ForEach cycle.
Now, we want to compare them in order to report when an object in $arrayA is contained in $arrayB, in that case, we also need to quit the current ForEach to avoid a wrong report to show (keep reading to understand better this point).

This is how the script will look like:

o, when $a equals to $b, we must break the cycle because the object has been found and we no longer need to double check it against the other objects. If we do not use the break command, the script will continue.

Let’s try to use the script logic to explain why we need to break it, by showing what would happen when break isn’t used:

  • We’re comparing “a” (from $arrayA) with “a”. Good, it matches, so we set $temp_entryMatch to 1.
  • Now we compare “a” (from $arrayA) with “b”. Nope, they do not match, set $temp_entryMatch to 0 (zero).
  • Now we compare “a” (from $arrayA) with “c”, then “d” with the same results: setting $temp_entryMatch to 0 (zero).
  • Now that we’re done with the first nested foreach, we check if $temp_entryMatch has a different value than 0. It doesn’t, so we will write that “a” is NOT contained in ArrayB.

But hang on a minute! “a” it’s contained in $arrayB! What went wrong? Well, simply the fact that we didn’t break the loop once “a” matched “a”, so the temp variable ($temp_entryMatch) got reset to 0 (zero).
This is really important in these sort of loops where you store data to a temporary variable.

Here’s the output when I run the script as reported above:

Nested-ForEach-PowerShell_with-Break

And here’s what happens when I run it without the “break” command.

Nested-ForEach-PowerShell_WITHOUT-Break

As you can see, only “d” is reported to be contained in $arrayB just because of luck, in fact it was the last item in the loop and it happened to match. This is it on Breaking a nested ForEach in Powershell!

Read More