Category Archives: PowerShell

Creating Azure Resource Groups Safely With PowerShell

A few posts back I mentioned that I had become a SimpleTalk author, and that my first post for them was PowerShell Functions For Reusability and Restartability in Azure (read it at http://bit.ly/acred01)

I’ve created a companion video for it, which focuses on creating Resource Groups. You can see it below, or on YouTube at http://bit.ly/acredyt01.

As I mention in the video, you can find the code samples on my github site, at http://bit.ly/acredgit01

Advertisements

I’m Now a SimpleTalk Author!

RedGateSimpleTalkLogoVerticalAdding to my other activities, I’m now writing for SimpleTalk, RedGate’s community hub. My first article just went live.

PowerShell Functions for Reusability and Restartability in Azure is the title, in it I describe how to implement the concepts of reusability and restartability in PowerShell. The functions were written against the Azure platform, however the concepts are valid for any PowerShell implementation. You can read the full article at:

https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-functions-reusability-restartability-azure/

If you want to keep up with all my articles, I’ve setup a special URL which will jump you to my author page at SimpleTalk:

http://arcanecode.red

It’s small now, but expect it to grow quickly. Happy reading!

Eric Ligman’s FREE Microsoft eBook Giveaway–Revising the download script

Every year, Eric Ligman, director of Sales Excellence for Microsoft, creates a blogpost in which he gives away tons of FREE Microsoft eBooks. This year has 361 in the list.

You name it, it’s in the list. SQL Server, Azure, PowerShell, .NET, BizTalk, SharePoint, Windows Server, and more. You can find Eric’s post at:

https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free-microsoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-ebooks-again-including-windows-10-office-365-office-2016-power-bi-azure-windows-8-1-office-2013-sharepo/#comments

While there are individual links to each file, what if you want every one of them? He explains on the post why he doesn’t provide a big zip file. He does, however, provide a PowerShell script (attributed to David Crosby) that will do the job.

However, I found some issues with the script. Not that it didn’t work, it did, but there were several things I felt could be done to improve it.

First, there was no progress message issued during the download. As a user, I had no idea which file I was on, so had no concept of how much longer it would take. Thus, I’ve added a little progress message.

I then thought “Hmm, what if my downloads were interrupted, I don’t want to have to start all over”. So, I added some code that sees if the file we’re downloading already exists. This way it won’t re-download a file it already has.

But then another problem arose. What if it had partially downloaded a file? Just checking the file names wouldn’t catch that. So I added further code to compare the file size at the source with the file size on disk. If different, then it will re-download.

So far so good, now it will skip the file only if the file name is already on the local disk, and the file sizes match.

I now encountered my next concern. Crappy internet. I live out in the country, and while I love my privacy and rural living, my internet sucks. It is prone to go down or drop packets. If it had issues during a  download I didn’t want it to crash, but instead go onto the next file.

Thus I added a try/catch error handler, which displays an error message and continues on.

At this point I thought I was done. Just I was about to call it finished though, a typical afternoon Alabama thunderstorm came up. Kaboom! House rattled and power blinked.

This presented my final concern, what if the power went out? I’d want to know where it got to with the downloads. So I added some further code such that when the downloading starts it creates a new log file and appends each message to it.

I realize some of you have superfast gigabit internet and will be able to download these almost instantly. (I hate you by the way. #jealous). Therefore I made logging optional, so it wouldn’t create an extra file if you didn’t want it. Just set the $log variable to $false, and it will skip logging.

So there you go, a revised download script that will handle stopping and restarting the script gracefully, will look for errors, and adds logging so you can track progress.

You’ll find the revised script on my GitHub site, in the PowerShell folder:

https://github.com/arcanecode/PowerShell

Just look for the file “Eric Ligmans Microsoft eBook Giveaway Revised Download Script.ps1

There’s also a readme style file by the same name, which echoes this blog post.

What happened to Save-AzureRmProfile?

I’ve been working a lot in the Azure PowerShell area of late. One thing I wanted to be able to do is have my scripts login automatically to Azure. In many examples the cmdlet Save-AzureRmProfile was used to save your Azure credentials, then later you could use Import-AzureRmProfile to import them.

But, when I attempted to run Save-AzureRmProfile I got the error ‘Save-AzureRmProfile is not recognized as the name of a cmdlet, function, script file, or operable program’.  Huh? I checked the docs, and it does include a listing for Save-AzureRmProfile.

https://docs.microsoft.com/en-us/powershell/module/azurerm.profile/save-azurermprofile?view=azurermps-3.8.0

This is a case of the PowerShell AzureRM module getting ahead of the docs. After beating my head against the wall, I found the cmdlets had been replaced with the new noun of AzureRmContext.

To use them, first login to Azure manually. Then, use the new Save-AzureRmContext to save your information to a file.


# Setup – First login manually per previous section
Add-AzureRmAccount

# Now save your context locally (Force will overwrite if there)
$path = "C:\Azure\PS\ProfileContext.ctx’
Save-AzureRmContext -Path $path -Force

Once that’s done, from then on you can use the Import-AzureRmContext to automate the login.


# Once the above two steps are done, you can simply import
$path = C:\Azure\PS\ProfileContext.ctx’
Import-AzureRmContext -Path $path

Be warned, this does present a security issue. If someone were to steal your context file, they could then login as you. You need to be sure your context file is stored in a safe location no one can get to.

IT ops and news talk–Episode 3 Secure that Jump Server

I owe everyone an apology, I missed blogging about this at the time it occurred. Last December I was a guest on Don Box’s podcast, “IT ops and news talk”. I appeared on Episode 3, Secure that Jump Server.

In the podcast we discuss the testing of PowerShell code with Pester. After that we got into an interesting discussion on the current state of DevOps. Give it a listen I think you’ll enjoy, it’s about half an hour in length. Don is a great interviewer it was a lot of fun.

As a follow on to the discussion, you might want to learn more about Pester. I have a complete course on the subject in my Pluralsight course Testing PowerShell with Pester. If you don’t know anything about PowerShell, but want to learn, then I’d suggest my Beginning PowerShell Scripting for Developers. Note that even though it says “…for Developers” we don’t mean programmers (although it could), instead it refers to people who wish to develop scripts in PowerShell.

What? What was that? You in the back row waving your hand? You say you don’t have a Pluralsight subscription? Hey, no problem. Just email me, free <at> arcanetc.com and I can send you a code good for 30 days of free access to Pluralsight, with which you can watch not just my courses, but any course from Pluralsight’s library of over 5,000 courses.

PowerShell Book Suggestions from IT/Dev Connections

In my session today at IT/Dev Connections in Las Vegas, several people asked me for book recommendations on PowerShell in general, and DSC (Desired State Configuration) in particular.

The book I’ve used for a while is from APress, Windows PowerShell Desired State Configuration Revealed.

I found a second DSC book today, Learning PowerShell DSC. To be honest I just found it, but from looking over the table of contents it looks pretty nice. I’ll report back later when I’ve had time to go over it in more depth. (Disclaimer, the publisher this book is also the publisher of my upcoming book, otherwise I don’t have any other affiliation).

A good all around yet deep book is PowerShell In Depth. Two of the authors, Don Jones and Jeffery Hicks, are here with us at IT/Dev Connections. Note the link is to the paper version, if you want the Kindle / iBooks / PDF version you’ll have to go directly to the publishers website. 

Finally, if you are seeking something that would be a fast read but still provide great value, I’d suggest “Learn PowerShell in a Month of Lunches”. The next version will be out in December of this year, but if you can’t wait, you can find the current version here. As with the previous recommendation, you can get the electronic version from the publishers website.

ArcaneCode–Headed your way!

I’ve not done much blogging, as I’ve been swamped with other activities. In addition I’ll be doing quite a bit of speaking, so let me catch you up.

Recently I did a webinar for Pluralsight, “Why you should invest in PowerShell”. If you missed it the recording is now up, take a look, it’s free!

http://go.pluralsight.com/C0010781

Next, I just completed the first draft of my fifth book, SQL Server 2016 Reporting Services Cookbook. I’m coauthoring with another MVP and great guy, Dinesh Priyankara. The book is available from PACKT Publishing in Alpha form.

https://www.packtpub.com/big-data-and-business-intelligence/sql-server-2016-reporting-services-cookbook

I’ll make my debut appearance at IT/Dev Connections next week. I’ll be doing two sessions, the first is on October 11th, 2016: Zero to Hero with PowerShell and SQL Server. We’ll begin with a quick overview of PowerShell, then dive into using it with SQL Server. You’ll see examples of using it for both maintenance and development tasks.

The next day is my second session is “So You Think MDX is Hard?”. This is for people who are new to MDX, and want to learn. You’ll see how to start from no knowledge all the way to building calculated members and sets.

If you’ll be at IT/Dev Connections feel free to come by and say hi, would love to meet as many as possible.

As if that’s not enough, on Saturday October 15th 2016 I will be at the DevSpaces Conference in Huntsville AL. At 4pm I’ll be presenting “High Class PowerShell: Objects and Classes in PowerShell”. You’ll see how to create your own classes using PowerShell. We’ll cover techniques valid in PowerShell versions 3 and 4, as well as see how the new class types in PowerShell version 5 work.

As they say on TV, but wait! There’s more!

On November 1st 2016 I will be coming to Atlanta to the Atlanta SQL Server BI user group. My presentation “Shiny and New: SQL Server 2016 Reporting Services” should be a lot of fun, and introduce you to the new features in SQL Server 2016.

Whew! There’s two more Atlanta based events in Nov/Dec it looks like I’ll be at, once those are finalized I’ll let everyone know.

I’ve also established a GitHub repository for my various samples. You’ll find it at https://github.com/arcanecode. As I move forward I’ll keep this repository updated.