blog.kaniski.eu I just wanna learn!

24Apr/173

Scheduling a PowerShell script… with arguments

Let’s say that you have a neat PowerShell script, which you want to run on some kind of a schedule (a script which will collect some data and send you an e-mail, every day in the same hour… ‘til the end of time – maybe this one) – how can you do it? Smile

The answer is simple. Yes, there is a tool included with Windows operating system, which can help you… and it’s called… well – Task Scheduler. Smile

And… if you never used the Task Scheduler in Windows, maybe this is the time to start.

It’s a rather simple tool – you click through a simple wizard, select what you need (a program) and when you need it, and you’ve created a scheduled task.

image

OK… so, I can run a program? What about a PowerShell script?

The real question here is “Who/what will run my PowerShell script?”. And then, the answer is simple – the PowerShell engine.

This means that your “program” is powershell.exe. This also means that in your scheduled task you should enter something like this:

image

(note the full path to powershell.exe – C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)

Now we have a scheduled task which will start PowerShell in designated time, every single day. Usually, this is not enough, and we need to add some arguments to the command running (like the path to the script we want to execute).

image

(argument field in this case contains -Command "& 'C:\Scripts\Get-HyperVReport.ps1'"be really careful about the single/double quotes here!)

Configured like this, our scheduled task will execute the following command:

Two remaining things that we have to check is to have our Get-HyperVReport.ps1 script saved in C:\Scripts and that user, under whom this task is running, has the appropriate permissions to run it. Also, if task should be running unnatended (usually it should), make sure to configure it so.

One other thing that may be useful – with this script, we need to specify some additional parameters (like ClusterName or if it will send us an e-mail when completed). In this case, we can easily add the required parameters to the arguments field, like this:

image

(argument field in this case contains -Command "& 'C:\Scripts\Get-HyperVReport.ps1' -ClusterName MyCluster -SendMail $true -SMTPServer smtp.mail.com -MailFrom hyper-v@mail.com -MailTo some.mail@mail.com")

The whole command is then:

Hope this helps!

Cheers!

P.S. One other other thing (yes, it’s written twice… live with it Smile) that can be useful – you can also use PowerShell to create scheduled task which will run this PowerShell script (instead of using “the lame wizard”). Pssst… take a look at the New-ScheduledTask command. Winking smile

P.P.S. You can also make use of Adam's function, which will make your life easier - https://github.com/adbertram/Random-PowerShell-Work/blob/master/Scheduled%20Tasks/New-ScheduledScript.ps1. Thanks, Adam!

23Apr/170

How PowerShell keeps my photo collection neat

As I love taking photos, sometimes it might be difficult to keep my photo collection “neat”. My camera is set to save every photo in two formats (one for editing, and another one for “long term storage”, as I like to say Smile) – .ARW and .JPG.

When I come home from a “photo trip”, I go through the photos taken and delete the ones I don’t really like. As I need to delete both copies, sometimes it happens that I forgot to delete .JPG or .ARW file of the same photo (which leads to “inconsistencies” in my collection… which is not “neat” Smile).

To overcome this, I’ve come up with a solution – a simple PowerShell script to check if there are any .ARW files which are missing it’s corresponding .JPG file (basically, I’m looking for files sharing the same name, but with different extension):

So, output will be something like this (with right arrow in results meaning that I have .ARW files for which I don’t have the corresponding .JPG file, which further means that I’ve deleted the .JPG file which I didn’t like and now I need to delete .ARW as well):

image

Arrow on the left side means that I have .JPG files for which I don’t have the corresponding .ARW file. No results will mean that I have the files in sync – for each .ARW file, there is a corresponding .JPG file.

Next step will be to tweak the script and probably automate the deletion process. For now, I’m satisfied with PowerShell providing me info about the duplicates and deleting the files manually. Smile

Cheers!

10Oct/160

Reinstalling your Hyper-V hosts

Have you ever reinstalled your Hyper-V hosts?

I know, there is not much need for it (as everything usually works just fine), but still… there is a “Windows Server 2016 re-installation wave” coming and maybe you’ll find the the next pieces interesting and useful.

One of the “messy” tasks with Windows reinstallation is networking… and by “messy” I mean “you have more than one network cable in your Hyper-V hosts” and you need to know which networks are connected where. Smile

What happens is that Windows somehow always forgets your network device order, all the pretty names you’ve applied and you get stuck with names like “Ethernet”, “Ethernet 2”, etc.

There is a way to fix this (there are many, actually) – we can ask our younger colleague to go to the server room and unplug the cables one by one and then plug them back in, following the rename on our (Windows) side. This way we are certain that all the corporate, DMZ, storage, live migration, etc. cables don’t get “confused” when added to their respective teams and if we labeled cables properly, everything will work with fresh Windows installation also.

But… there is another way. We can use what we already have – our documentation. Here I mean “our current setup” – we have our network adapters and teams already configured in our current Windows installation, why don’t we just export this info and use it after the reinstallation?

We can do this easily by using PowerShell!

The idea is to export network adapter names and MAC addresses of our physical network adapters (excluding the virtual and team adapters), in a CSV file, so that we can use it later, to rename our adapters after the reinstallation:

After reinstallation, we can use the following command to rename our adapters, as per our saved CSV file:

And voilà – our networks are named nicely again (and our colleague didn’t need to go to the cold server room… this time). Smile

Cheers!

11Jan/160

Playing with folders and permissions

This one will be short and sweet. Smiješak

Imagine you have an Active Directory full of users. You also have a file server in your environment. And, as it happens, each of your users needs to have a folder created just for himself, on this file server. Each folder should have inheritance disabled and each user should get full permissions on his folder.

What do you do?

Well, we can manually create the required folders, or we can use a PowerShell script which will do it for us. Obviously, I’ve chosen PowerShell (maybe not the nicest script in the world, but it does the job), or this post would be… lame. Smiješak

So, the interesting part of this script goes like this (I've added a few empty lines to make comments more visible):

Cheers!

18Jun/150

Open Cloud BBQ – Nano Server

There was a barbecue today… on the roof… of the Microsoft office! How cool is that??? Smiješak

I’ve had an honor to join my colleagues and friends, and contribute to this great event with a presentation called “Nano Server – budućnost je tu!”, which was actually inspired by Jeffrey Snover’s Ignite speech about Nano Server – the new installation option in Windows Server 2016.

10301057_10153392135462905_6070746253170533265_n

The whole Nano Server idea is very simple – let’s remove all the “unnecessary” roles and features (especially the famous GUI), and leave only the parts that we really need for whatever purpose (i.e. we’re getting the “purpose built servers”). All the purposes/roles that are supported in this preview version are Hyper-V, File Server and Failover Clustering, and they are working great on this “nano-sized” server operating system (even in this early preview).

I’m really excited about Nano Server, and I’m so glad that I’ve had a chance to talk about it at such great event. Because there were so many questions about it during the day/evening, I’ve noticed that Nano Server is a very cool subject for attendees also. Nice!

The presentation is available on SlideShare – check it, install the Nano Server and start rocking your datacenters! Smiješak

Cheers!

22Jan/152

Hyper-V reporting script

There’s something nice for all you Hyper-V admins out there – don’t know if you’ve seen it already, but Serhat Akinci (my MVP colleague) made a great script for reporting the health of your Hyper-V hosts, called Get-HyperVReport.

You can use it on local or remote Hyper-V hosts and clusters, schedule and e-mail the reports (something to read while enjoying the morning coffee… or tea Smile), and they look like this:

image

Highlights (from TechNet page):

  • More than 2600+ lines of PowerShell, HTML and CSS code examples
  • Creates a plain but detailed and user-friendly HTML report which is compatible with all modern browsers
  • Provides more detailed information via tooltips in the HTML report. (cells with asteriks and highlighted)
  • Checks and installs required runtime environment prerequisites like Hyper-V and Clustering PowerShell
  • Collects information by using standard Hyper-V and Clustering PowerShell cmdlets and custom WMI queries
  • Shows alerts in the report for certain situations (utilizations, VM checkpoints, replication status, etc.)
  • Can be used directly from command-line or as a scheduled Windows task
  • Supports report delivery via e-mail with advanced options. (authentication, TLS/SSL, multiple recipients)
  • Includes a mode that reports only alerts in the Hyper-V environment. (aka HighlightsOnly mode)
  • Advanced error handling and logging. (Console messages and log file)

Download of this script, and more information about it, is available at TechNet Gallery. And remember – don’t let your Hyper-V hosts run all by themselves! Smile

Cheers!

28May/140

Adventure of installing the Windows Azure Active Directory Module for PowerShell

Well, you know the story – “something needs to be done immediately, usually in the middle of the night, involving PowerShell, and you don’t have all the needed modules installed…”.

The solution seems easy enough – install the required modules, connect to Office 365 and do the job. Yeah… but no! Smile

More specific – I’ve tried to install the Windows Azure Active Directory Module for Windows PowerShell the other night. In the end, I’ve succeeded, but something kept me awake a little longer than necessary.

I’ve read an article on TechNet, explaining the management of Azure Active Directory using PowerShell. Why? Because I couldn’t do what was needed via the (nice) user interface.

So, instructions said “Install the Windows Azure AD Module” – I’ve downloaded the appropriate installer (Windows Azure Active Directory Module for Windows PowerShell (64-bit version)), and started the installation.

Almost immediately, I’ve got an error saying that the Microsoft Online Services Sign-In Assistant (version 7.0 or greater) needs to be already installed. OK, I’ve downloaded this piece of software as well (from here), and installed it. “Fortunately” it demands a machine reboot. Rebooted.

image

Now I’ve tried to install the Windows Azure AD Module again, and got the same error:

image

I must say that I’m little confused at this point, because I was convinced that I’ve installed this just a minute or two ago. Ok, it’s late. No big deal – I’ve ran the installation again, and got the following screen:

image

So, it is installed after all. Maybe it’s the wrong version (on the other hand, the TechNet article contains the link to download)? After a few moments of searching, I’ve found the more recent version of this Sign-In Assistant, called Microsoft Online Services Sign-In Assistant for IT Professionals BETA. I’ve installed this version now, and tried to install the Windows Azure AD Module afterwards. Now it finally worked!

image

The conclusion – this TechNet article is slightly out-of-date (linked to the wrong version of the Sign-In Assistant, which doesn’t work with the current version of Windows Azure AD Module) and, until this is resolved, you’ll need to install the BETA version from the link provided above (this one).

Cheers!

19May/140

Installing the KB2920189… successfully!

The other day, I’ve written about an issue with installation of update KB2920189 for Windows Server 2012 R2 (post called [TIP] Latest “Patch Tuesday” & errors installing update). So, the problem I was facing was failing installation on Generation 2 virtual machines, with Secure Boot enabled.

Actually, you can overcome this problem easily by reading the documentation before it happens or… with PowerShell after (OK, you can use GUI also, but who uses it nowadays?)! Smile

As I’ve already mentioned, all you need to do is just one thing – install the BitLocker feature on your server.

(yes, “-Restart” is optional – if you want, you can restart your server manually, of course)

image

After that, BitLocker is installed, and you can successfully install the KB2920189 also.

Second (not official) approach on installing this update on Generation 2 virtual machine, as I’ve written in the mentioned post, is to uncheck Secure Boot, install the update, and then check the Secure Boot option again.

This can be easily done using the following script:

We can now install this “problematic” update as the Secure Boot feature is disabled:

image

image

…and Secure Boot is enabled again (“nothing” happened)!

Of course, you can do all of this manually, but then again – why do we have PowerShell? Smile

Cheers!

P.S. I’m no PowerShell expert… just like to automate some things. Smile

17Aug/090

Besplatna PowerShell knjiga

Interesira vas PowerShell, a ne znate od kuda početi? Rješenje ovog problema nalazi se na sljedećoj adresi.

Na tom linku se nalazi BESPLATNA e-knjiga “Mastering-PowerShell” koju je napisao Dr. Tobias Weltner (Microsoftov MVP za PowerShell i pisac preko 80 knjiga).

Znači, ako trebate knjigu o PowerShellu, koju je napisao čovjek koji “zna nešto” o toj tematici – posjetite gore navedene stranice i uživajte…