Category Archives: PowerShell

Set up an SMTP Replay (or smarthost) in Windows 2012 R2 with PowerShell

I just set up for our servers that need to send mail, we have a smart host relay to sendgrid.

Part of the setup is done via PowerShell, part through the GUI. If you can direct me on how to configure the additional steps via PowerShell I would be keenly interested and would update this site.

Start by setting up the SMTP Server, which is managed by the IIS 6 Admin console, with these commands from an elevated PowerShell prompt:

Import-Module ServerManager 
Add-WindowsFeature SMTP-Server,Web-Mgmt-Console,WEB-WMI

Then follow the steps 2-8 on this SpiceWorks Post

I would test from every server that must send email. Here’s the PowerShell way to send a test email, using splatting by building $params as a hash table for the parameters then passing in @params. Note that values can be single quoted if they’re not expanding, meaning they’re not containing any variables.

$MyDomain = 'daveslog.com'
$MyEmailPrefix = 'dcobb'
$SMTPRelayServerName = 'SMTPSERVER1'

$params = @{
From = "testing@$MyDomain"
Subject = 'Testing SMTP relay from PowerShell'
To = "$MyEmailPrefix@$MyDomain"
SmtpServer = $SMTPRelayServerName
Verbose = $true
}
Send-MailMessage @params

I want to do the whole process via PowerShell, and hope to investigate further.

 

My first book review! SQL Server 2014 with PowerShell v5 Cookbook

Has been a very busy year for me, haven’t posted lately, but was very excited to share this.

I’ve had the opportunity to be a technical reviewer for three PowerShell books this year!

One that I’ve really enjoyed reviewing has just been released, and the publisher Packt is offering an end of year sale for $5 eBook editions.

Donabel Santos (sqlbelle.com & @sqlbelle) is a wonderful blogger, author, trainer, and Microsoft MVP. This is her second PowerShell cookbook for SQL, and it’s an incredibly useful resource for learning the practical details of PowerShell, both in general and when working with SQL Server. All her examples come with code samples so you can leverage her scripts to get productive right away.

You can check out the book on the Packt website. It’s only $5 for a great PowerShell & SQL eBook, or $30 for the eBook and print version. It’s a phenomenal resource, and a great deal for DBAs looking to take the plunge into PowerShell, and DBAs familiar with PowerShell ready to expand their knowledge about all that’s possible with PowerShell in SQL.

Please let me know if you found her book useful, and have a Merry Christmas, Happy New Year, and enjoyable holiday season. 🙂

 

 

Get your Public IP and Geographic Data with PowerShell

 

Always fascinated by what can be done very tersely with PowerShell.

I saw an interesting tip on PowerShell.com that gives the geopraphic info on a given public IP. They had a post I dug up a couple years back that determines your public IP dynamically, so I connected the Lego blocks… here is a script that determines your public IP and tells you where you are, using a web page and a REST API.

#Determine Public IP Dynamically
$ipaddress = (Invoke-WebRequest 'http://myip.dnsomatic.com' -UseBasicParsing).Content
$infoService = "http://freegeoip.net/xml/$ipaddress"
#Get Geographic Data based on my IP
$geoip = Invoke-RestMethod -Method Get -URI $infoService
$geoip.Response
 
#As a One-Liner
(Invoke-RestMethod -Method Get -URI "http://freegeoip.net/xml/$(Invoke-WebRequest 'http://myip.dnsomatic.com' -UseBasicParsing)").Response

 

PowerShell brain teaser

I’ve been following the buzz about the PowerShell Scripting Games on Twitter, and came across this PowerShell brain teaser from beefycode.

Here’s my answer, wish me luck!

In PowerShell, when is the following statement true? Explain why.
 

     ( $a -eq $b ) -ne ( $b -eq $a )

 

The key to this puzzle can be found in PowerShell help:

 
help about_Comparison_Operators -ShowWindow
 
Here’s the relevant paragraph from the help result:
 

When the input to an operator is a scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values. If there are no matches in a collection, comparison operators do not return anything. 

 
When we use the -eq operator in PowerShell, most assume it’s the same as the equals sign (=) in other programming languages, and using it would return a boolean result, and would return $true if the compared values match, and $false of they do not. And it would make sense that the order variables are specified shouldn’t matter, so if $a -eq $b then of course $b -eq $a
 
But the paragraph from the help topic above tells us that -eq and other comparison operators perform an additional function; if one of the compared values is a collection, like an array, it will return any matching values in the array, rather than a boolean result.
 
Let’s take an example. If $a is an integer, and $b is an array that contains that integer, the expression ( $a -eq $b ) is like asking “Hey PowerShell, is this integer variable $a equal to this array $b?”. However, if I switch the variables around, the expression ( $b -eq $a ) is like asking “Hey PowerShell, what values in this array $b are equal to this integer variable in $a?”
 
This PowerShell behavior is how we could get a $true result for the brain teaser expression:
 ( $a -eq $b ) -ne ( $b -eq $a ), like so:

 PS C:\Users\Dave> $a = 1

PS C:\Users\Dave> $b = 1,2

PS C:\Users\Dave> $a.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType

PS C:\Users\Dave> $b.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

PS C:\Users\Dave> $a -eq $b
False

PS C:\Users\Dave> $b -eq $a
1

PS C:\Users\Dave> ( $a -eq $b ) -ne ( $b -eq $a )
True

Any questions?

Great time at Florida .Net UG in West Palm

Whew! Presentation on PowerShell for Developers to the Florida .Net user group went well, and it was a very lively group. I went in expecting to dig deep into applying PowerShell for GUI application development, deployment and the like. But we had a lot of folks very new to PowerShell, and we had a very interactive presentation and discussion with lots of good questions. 

I instead focused on using the PowerShell 3 ISE as a starting point to new students to PowerShell, and taking advantage of this tool (along with the basic three Get Cmdlets, remember those? 🙂 ) to make those uncomfortable first steps to learning a new technology as easy as possible.

It is my hope that many in the group will get excited about learning something that’s not just a useful and marketable skill, but fun to do! If you attended and have follow up questions or need reminders of the resources I referred to, please contact me. I think the slides will be less useful than set of resource links to help learning the fundamentals of PowerShell. Looks like I have work to do….

I want to take a moment to thank thank three guys that provided me with some slides and demos to to share. Mike Robbins, Michael West, and Michael Cruz. (Yes, three Mikes helped me.) Thanks gents! Hope to ‘see’ you April 16th at our next Florida PowerShell user group virtual meeting!

Presenting PowerShell for Developers to Florida.net UG in West Palm

I’ve been asked to present to the Florida.Net user group in West Palm this week, and chose to present on PowerShell for Developers, and here’s why.

There are incredible possibilities and utility in PowerShell, most of the marketing and discussion on the web is for I.T. administrators, rather than developers (though there are some signs this is changing.) The conventional wisdom (and humor) tells us that people in I.T. must fit into either of these categories. Here’s another perspective.

As a consultant and trainer, I need broad familiarity with technology. I’m focused on Windows, with some geek love for Linux :). SQL Server is my technology focus, but Windows Server setup and configuration are important to ensure it runs well. I have built and also supported .Net applications that rely on SQL Server, so the Frameworks and how they connect and interact with my SQL Server are important as well. Like many I.T. professionals I work with many interrelated technologies, and staying effective means knowing about both administering my servers and developing and supporting applications.

So while the I.T. industry seems to push us to specialize, there will always be a place for people who excel in both systems administration AND development, particularly in small companies where wearing many I.T. hats is a necessity. And as the technology continues to develop, learning new technology is essential to staying relevant and valuable in the marketplace. But time is a limited resource, so getting familiar and productive quickly is essential.

This brings us to why I believe PowerShell to be so important, especially to developers. PowerShell is built on the .Net Framework, so developers get a jump on the rest of us in learning and applying it. While the learning curve can seem steep, with a basic knowledge of this tool you can then leverage the tools of others, to solve problems and develop new solutions quickly. Microsoft’s latest Server 2012 comes with dozens of PowerShell modules built in. There are PowerShell modules for Microsoft technologies like IISWindows Azure, and Hyper-V. But not just Microsoft:  Amazon AWS, VMWareDell Servers, and many other companies understand that leveraging PowerShell is good for the customer and for business. 

So any technology professional on the Windows platform, whether administrator, developer or somewhere in between, should seriously consider investing the time to learn PowerShell so they can be more effective.

I’m excited about PowerShell. I’ve just been learning it over the last few months, and it has already helped me solve problems for clients, and made me a better consultant. So my goal for my presentation this week is to introduce both the basics of PowerShell and its possibilities to the developers in attendance, so they might get excited about PowerShell and how they can leverage it to be better at what they do.

I’ve been too long without blogging… I mean, who really reads this stuff? But if you took the time to read this, please contact me or comment below and let me know what you think, and perhaps you can help inspire me to continue blogging regularly. 🙂 Thanks!

SQL 2012 and PowerShell 3 talk at Florida.Net UG

After some tuning I re-presented my talk on SQL 2012 and PowerShell 3 for the Florida.Net user group.

They let me ramble on much longer, so I had the chance to do more demos and step through the code more thoroughly.

Recieved some really great feedback, and saw more than a few people may have caught the PowerShell bug 🙂

The updated slides are on SlideShare.net.

If I get five or more requests via email or my contact form, I’ll post the code as well. Just want to check whether this internet thing is working….

SQL 2012 and Powershell 3 SFSSUG Talk

Spoke last night on SQL 2012 and Powershell 3 at South Florida SQL Server User Group today. Thanks guys for having me!

I had lots of content, and was ready to dive into changes from PowerShell 2 to 3, new SQL 2012 Cmdlets, and coding with SQL Management Objects. However most of the group was just starting out with PowerShell, so I had to shift direction, ended up talking more than demoing. 🙂  

Would be interested in doing a PowerShell 101 class for any user group interested in the future. PowerShell is just an incredible tool that any DBA, Server Admin or Developer should learn and apply.

I’ve posted the slides on SlideShare.net

I’ll post the demo code as well shortly if requested, it needs some cleanup before it’s ready to share.