Failed Domain Relationships on SQL Server Cluster Nodes

You know what’s scary as hell? When one node of an important cluster loses its trust relationship with the domain and you see the error “the trust relationship between this workstation and the primary domain failed”. That happened to me late last year with one of my SQL Server 2008 R2 nodes. The scary part was that I just didn’t know what to expect. The fix could be simple, or it could require a node rebuild.

Generally, I address this issue not by removing servers from the domain, but by using PowerShell v3 by executing the following with an admin prompt:
Reset-ComputerMachinePassword

In this case, however, I did not have access to v3, so at an Admin prompt, I executed

netdom resetpwd /s:dc.ad.local /ud:ad\adminaccount /pd:*

This successfully reset the password and I was able to login again with a domain account. I then started the cluster service, and failed my SQL Server over with no issues. I then failed back and rebooted the server for good measure (and to ensure the trust still existed). I tested this as well on Windows 2012 R2 with a SQL 2014 cluster with success, though Reset-ComputerMachinePassword was easier to remember and worked just as well.

What caused the loss of trust? I haven’t figured it out yet, but I’m assuming that node probably cheated with his ex.

Posted in Active Directory, PowerShell, SQL Server

PowerShell Conference EU 2016, psconf.eu

I’ll be presenting some sessions about SQL Server and PowerShell at the PowerShell Conference EU in Hanover, Germany on April 20-22! You should join me, because the speaker and session lineup is amaaazing!┬áRegister at psconf.eu.

presenter

Want to know more? Check out PowerShellMagazine.com and psconf.eu!

Posted in PowerShell, SQL Server

PowerShell Tip: You may not need to load the DLL for that Assembly that can’t be found

Recently, I wanted to access the System.Windows.Forms.Integration.ElementHost type, which uses the System.Windows.Forms.Integration namespace.

When I tried to load that namespace, PowerShell complained that The assembly ‘System.Windows.Forms.Integration’ could not be found.

I read up and found that the WindowsFormsIntegration.dll would need to be loaded, and I begrudgingly added this to my script:

It’s ugly though, and I wondered if there was another way. Then I thought maybe I can just add the base name of the DLL file, and sure enough, this works!

So the next time that you see an assembly can’t be found can be loaded by using the DLL, try just the DLL name instead!

Posted in PowerShell

ShowDialog() Sucks: Use ApplicationContext and Run Instead

Update Jan 7: Added KeyboardInterop so that TextBoxes will work.

One of the lessons learned when making Popups and NotifyIcons in PowerShell and WPF is that the WPF window must be run the the proper context.

I was tipped off to this by Denniver Reining’s PowerShell / NotifyIcon article and Johnny J’s “Doing a (C#) NotifyIcon program the right way“.

While neither of the articles were using WPF, extensive testing showed that using ApplicationContexts and Application.Run instead of ShowDialog() made WPF (and WinForms) work far better. The NotifyIcon issue took a number of days to resolve as I battled with unresponsiveness when clicking on the ContextMenu to Exit. Then it would take about 5 seconds to disappear.

For about a week, this was the story of my life:

aero_busy

And it seemed especially true after hiding the Powershell taskbar application. So I scoured the Internet and found that a lot of other people had the same issue. Just search Google for “NotifyIcon doesn’t disappear.

In addition, the mouse sometimes showed as busy when hoovering over the popup window itself and sometimes the right clicking worked only once.

notify-wait

System.Windows.Forms to the rescue (!?)

So none of the solutions I found worked, but I remembered that Denniver’s NotifyIcon script was responsive, so I went back and noticed his app ended with this important line

[void][System.Windows.Forms.Application]::Run($form1)

This information, along with Johnny J’s article about NotifyIcons helped me figure out that the following would probably work, even for WPF. Now these two lines, along with Hide-PowerShell are always included in my finalized PowerShell-based WPF scripts.

$appContext = New-Object System.Windows.Forms.ApplicationContext
[void][System.Windows.Forms.Application]::Run($appContext)

According to Microsoft, “Application.Run begins running a standard application message loop on the current thread, with an ApplicationContext, which specifies the contextual information about an application thread.”

I’ll be honest, I’m probably not the best person to explain ApplicationContexts in depth. If you’d like to learn more, check out this article titled Use the ApplicationContext Class to Fully Encapsulate Splash Screen Functionality where the author goes into detail about the ApplicationContext class.

The code below (which is also the code from my previous post) shows a fully functioning WPF GUI App that runs in an application with an ApplicationContext. It also has a few other cool techniques you may enjoy.

Icon-Overlay

Note that this code should’t be copy/pasted into the console because the PowerShell window will disappear before it can complete the paste. Be sure to download the script or paste the code below into a .ps1 file and execute. It does appear to work well in the ISE, though!

Even though I had changed my own usage of WPF forms, it didn’t occur to me to blog about it until Doug Finke mentioned on Twitter that his WPF apps are sometimes unstable and crash. When I saw the ShowDialog() in his code, I knew exactly what the problem was, and knew I had to get the word out.

As of today, one of my servers has been running its WPF/PowerShell based-monitor for almost a month!

timespan

In conclusion, you should probably never use ShowDialog() to run your primary window in PowerShell unless you’re testing it (and I should probably update my old blog posts to clarify that).

Also, I don’t totally know what I’m doing, so take this with a grain of salt ;) It’s just worked better for me than ShowDialog(). I will probably revisit this one day with a PowerShell implementation of InitializeComponent().

Posted in PowerShell, WPF

Adding Toolbar Icons to Your PowerShell WPF GUI

This post builds on some previous post titled Use base64 for Images in WPF-based PowerShell Forms.

Show-WpfIconExample.ps1, which can be found below and on github, demonstrates how to overlay the default PowerShell toolbar icon with an image that’s been transformed to a base64 string to System.Windows.Media.ImageSource.

An icon overlay is the mini icon that sometimes shows up on top of a taskbar application icon.

Icon-Overlay

The overlay icon is controlled by $window.TaskbarItemInfo.Overlay. I’m actually unsure if the PowerShell toolbar icon can be replaced entirely ($window.Icon usually takes care of that) without a ton of code.

As a bonus, I also included how to change the taskbar popup description from “Windows PowerShell” or nothing to the title of the $window.

The secret sauce of this is
$window.TaskbarItemInfo.Overlay = $bitmap
$window.TaskbarItemInfo.Description = $window.Title

If you’d like to learn more about the flag of my people, the Cajuns, check out the Wikipedia article “Flag of Acadiana“.

Note that this code can’t be copy/pasted into the console because the PowerShell window will disappear before it can complete the paste. Be sure to download the script or paste the code below into a .ps1 file and execute. It does appear to work well in the ISE, though!

Up next, I’ll be talking more about running your WPF GUIs using ApplicationContext instead of $window.ShowDialog()

Posted in PowerShell, WPF

PowerShell Invoke-RestMethod / CloudFlare API v4 Code Sample

If you’re looking to create your own CloudFlare API v4-based PowerShell module, and would like some syntax examples, here’s a simplified breakdown of my CloudFlareDynDns module on github, which I discuss in “Use PowerShell and CloudFlare API v4 to Dynamically Update CloudFlare DNS to your External IP“.

Posted in Networking, PowerShell

Use PowerShell and CloudFlare API v4 to Dynamically Update CloudFlare DNS to your External IP

cloudflare

Got a homelab and miss how DynDNS used to be free? Using PowerShell and a CloudFlare hosted domain, you can emulate similar functionality for free using PowerShell and a Scheduled Task. If you’re looking for a DDNS client that you can run on a router, check out CloudFlare’s ddclient or Scott Helme’s post Replacing DynDNS with CloudFlare DDNS.

Don’t use CloudFlare? You should consider it. I love CloudFlare because they provide free HTTPS and the DNS management UX is awesome. They’re also a CDN, all free. I can’t say one bad thing about the service. It’s a reverse proxy that works well, and nowadays, any new domain I get goes straight on it, even bepug.be, the Belgian PowerShell User Group :)

Introducing CloudFlareDynDns PowerShell Module

So last week, I created a PowerShell module, CloudFlareDynDns that uses the CloudFlare API to update a specified host name. The basic syntax for this module is:

cfupdate

(As of 22 Dec 2015, you can find your API key at: https://www.cloudflare.com/a/account/my-account -> API Key)

When Update-CloudFlareDynamicDns is executed, it’ll go out to ipinfo.io to get your External IP, then it’ll compare that to the current record at CloudFlare. If you use the -UseDns switch, it’ll check DNS instead of querying CloudFlare directly. If the record doesn’t exist, it’ll add it. If it needs to be updated, it’ll update it.

When making this a scheduled task, ensure the module is located in your path and successfully loads when you first open PowerShell. Then “Run whether user is logged on or not”, store the password, then for the Action, Start a Program -> powershell.exe and set the arguments to the full Update-CloudFlareDynamicDns command. Set to run every 5 minutes indefinitely.

Thanks to Jorrit Salverda and Posh-CloudFlare for getting me started. If you’d like to see a simplified breakdown of the code, see PowerShell Invoke-RestMethod / CloudFlare API v4 Code Sample.

Posted in Networking, PowerShell

My Very First Scripting Guys Article: Creating Pop-ups by Using PowerShell

So this happened about a month ago, but I’ve been out with the flu. Ed Wilson with Microsoft Scripting Guys invited me to write a few articles on WPF, and I excitedly said yes! In the first article of the series, I detail how to Create Pop-ups by Using PowerShell. The example used displayed local hard drive space.

The second article will detail how to use NotifyIcons and BalloonTips to display an alert if the disk capacity dips below 20%.

I’d like to thank Ed Wilson for such an amazing opportunity; I’ve followed the Scripting Guys pretty much since they first appeared, and writing for them is a total honor.

Posted in PowerShell, Windows, WPF

Migrating SQL Server?

dbatools is an awesome PowerShell module that helps you migrate entire instances with a single command.


now on github

SQLPS Needs You

SQLPS now has a dedicated engineer and Microsoft is asking for our input!


 
Upvote priorities and cmdlets now

Upvotes Needed

Help persuade Microsoft to open source SQL Server's PowerShell module, SQLPS.


 
Upvote now on Microsoft Connect

Authors


Chrissy LeMaire


Brandon Abshire
View Brandon Abshire, MCDBA's profile on LinkedIn

Awards

Chrissy has been awarded the Microsoft MVP for her work in the PowerShell community.

Join us!

Belgian PowerShell
User Group

  SQL PASS PowerShell
Virtual User Group