Author Archives: Rob

About Rob

http://gumption.co.nz/about-me/

SQLSAT582: Resolving the “WinRM firewall exception will not work since one of the network connection types on this machine is set to Public” Error.

At SQL Saturday Melbourne, an otherwise excellent event, my demo failed on me because the DBAtools module failed to load with the following error:

Set-WSManQuickConfig : <f:WSManFault xmlns:f=”http://schemas.microsoft.com/wbem/wsman/1/wsmanfault”
Code=”2150859113″ Machine=”localhost”><f:Message><f:ProviderFault provider=”Config provider”
path=”%systemroot%\system32\WsmSvc.dll”><f:WSManFault
xmlns:f=”http://schemas.microsoft.com/wbem/wsman/1/wsmanfault” Code=”2150859113″
Machine=”<machinename>”><f:Message>WinRM firewall exception will not work since one of the
network connection types on this machine is set to Public. Change the network connection type to either Domain
or Private and try again. </f:Message></f:WSManFault></f:ProviderFault></f:Message></f:WSManFault>
At line:116 char:17
+ Set-WSManQuickConfig -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Set-WSManQuickConfig], InvalidOperationException
+ FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.SetWSManQuickConfigCommand

For a piece of code that had run successfully a dozen times this was of course a VERY annoying error, not least because I set it to run in another window and carried on with my talk, so didn’t really have time to either troubleshoot it or re-run it once I was back.  I had initially believed the problem was due to an earlier deviation I had made from the order I was going to do things and therefore related to execution policy.  But I hadn’t even read the error message properly during the demo because I just wanted to curl up in a corner and cry.

But running it again later I remembered the other change that I had made to my laptop from every other time I had run the code.  I had adjusted my network settings from a static IP address to allow me to connect to the university wireless.  That was a dumb rookie mistake, and I really had no excuse having not re-run my code in the new environment.  My session was right after lunch, so I had all lunchtime to step through my code, and had done so with everything but the particular script that failed.  Lesson learnt the hard way I guess.  Speakers be warned.

Anyway, the error is obviously related to the change in wireless settings.  I was quickly pointed in the correct direction by this post from Matt Wrock who provides the powershell commands needed to set all your network connections to private:

$networkListManager = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")) 
$connections = $networkListManager.GetNetworkConnections()
# Set network location to Private for all networks 
$connections | % {$_.GetNetwork().SetCategory(1)}

As the article referenced above explain the implications and manual process better than I could I won’t add additional junk to the internet, except to say that the failure of the import of the DBA tools module appears to be due to a failure of Set-WSManQuickConfig  in the PS-Remoting module.  Also not that making your networks private actually has less security associated with it than having them “Public” which is a bit counter intuitive.  A “Public” network setting means you are on a public network, don’t trust it so security is high, whereas a private network means you trust the network and are prepared to let traffic do a few extra things – like ps-remoting for instance….ahem.

This is the first session which has been so heavily reliant on demo code, normally I do a lot of speaking from a slide deck.  The turnout was really good, but I felt my presentation in general was pretty poor.  It’s much easier to deviate from what you planned when you are talking from a slide deck, because the slide deck brings you back to the intended presentation.  When you are just typing code into a powershell console and someone asks you a question that takes you away on a tangent it’s harder to come back on topic.  In general I think the presentation went a little bit all over the place, which is exactly what people don’t need when they are trying to grasp the fundamentals of using a programming language.  The structure I had in place was good, but my execution on the day was poor.  So apologies to the audience on the day, but hopefully the core message of the usefullness of powershell still got through.  You can run through my demo scripts by downloading them from the SQL Saturday site at:  http://www.sqlsaturday.com/582/Sessions/Schedule.aspx

 

 

Resolved – A job step received an error at line 3 in a PowerShell script. The corresponding line is ‘$space.ForegroundColor

I was working on some Powershell automation for a weekly process for one of my colleagues and set up the powershell script to run via a SQL 2014 agent job.  After some initial permissions errors I ran into this error:

Executed as user: <Agent Account>. A job step received an error at line 3 in a PowerShell script. The corresponding line is ‘$space.ForegroundColor = $host.ui.rawui.ForegroundColor ‘. Correct the script and reschedule the job. The error information returned by PowerShell is: ‘Exception setting “ForegroundColor”: “Cannot convert null to type “System.ConsoleColor” due to enumeration values that are not valid. Specify one of the following enumeration values and try again. The possible enumeration values are “Black,DarkBlue,DarkGreen,DarkCyan,DarkRed, DarkMagenta,DarkYellow,Gray,DarkGray,Blue,Green,Cyan,Red,Magenta,Yellow,White”.”  ‘.  Process Exit Code -1.  The step failed.

  This was really confusing at first because there was certainly no code that manipulated the console output – I was running from an agent job.  After a little testing I found the error to be with a piece of code within my script which DOES manipulate the console output – specifically the native CLS function.  As that was obviously part of the process of me testing the code and not anything I was going to need for the piece of automation I was working on removing the “CLS” from my script resolved the problem.

The error message was really misleading here, and I hope that it is addressed by Microsoft in the future.  It references a specific line of code in an agent job step, but for me that line corresponded with an echo statement.  At the least it should indicate that the error is in a called piece of code, not in the source script, and preferably shouldn’t generate an error at all, as all the output from the job step is in plain text.

 

Speaking at SQL Saturday Sydney 2017

I’m very happy to once again be making a trip across the Tasman and will be speaking about Increasing your SQL Server performance at SQL Saturday Sydney on February 18th.  Check out the list of speakers here:

SQL Saturday Sydney 2017

This will be my second time presenting at the Sydney event after an enjoyable trip there last year.  If you are in the Sydney area I look forward to seeing you there.

Speaking at SQL Saturday Melbourne 2017

I’m very happy to once again be making a trip across the Tasman and will be speaking about adding Powershell to your DBA toolkit at SQL Saturday Melbourne on February 11th.  Check out the list of speakers here:

SQL Saturday Melbourne 2017

It looks like the Melbourne team have once again pulled together a great list of speakers and it will be a fantastic days SQL learnings.  If you are in the Melbourne area I look forward to seeing you there.

How to Force a Database to A Specific Database_ID

I walked into the office this morning and got asked a strange question:  “Does a database you detach and re-attach have the same database id before and after the detach?”.  While it may appear that it does in a simple test, it actually doesn’t – it just takes the next available id.  If you detach database id 5 and then re-attach it, it will retake id 5, and this will mostly work – unless other databases have been removed from the server at some point in the past and there are ‘holes’ in the list of available id’s.  So…”How do you force a database to have a specific database id?”

First of all, you can get the current database_ID of all your databases by running:

select name, database_id from sys.databases

What you will likely notice is that the numbers go from 1(master) to roughly the number of databases on your instance. SQL Server assigns the next available database id. What that means is that if you have a clean install database id’s 1-4 will be taken by the system databases and the first database id you create will be 5 and the second 6 and so on. The exception is when you delete\detach a database – this creates a hole which is the next assigned number.  So if you have created databases with ID’s 5-10 and detach the database with id 7, the next database you attach or create will take database_id 7.

The upshot of all this is that if you ever want to assign a specific database id to a new database you need to do the following:

  1. If all your database id’s are sequential and the next available number is the id you want – just create the database.
  2.  If all your database_id’s are sequential and the database_id is already assigned, detach the database that is currently using it, create the database you want to take that ID and then re-attach the database you dropped.
  3. If you want to assign a number that is greater than the currently available database_ids, simply create databases until the next available id is the number you are after, then create the database you want to take that id, then remove all the filler databases you created to get there.

I’m still unsure what the usecase for this is – but now you know how to do it.

SQL Saturday Auckland

SQL Saturday Auckland is on October 15th and is being hosted in the Microsoft Office in the Auckland Viaduct.  I’m fortunate enough to have been selected as a speaker which will mean dusting off my HA and DR talk which I last presented back at the start of the year in SQL Saturday Sydney, and adding some new content to it.

It’s a talk I enjoy giving, because although it is very much an entry level talk it gives people an introduction to technologies they may not be familiar with or use in their own SQL environment, and usually generates some good questions and discussions about why you would choose one technology rather than another.

So if you happen to be in Auckland next weekend come along and get some great free SQL training:  http://www.sqlsaturday.com/571/eventhome.aspx

 

SQL Sentry Plan Explorer

I’m all for spreading good news as far and wide as possible.  So I wanted to make sure everyone knows that the good folks at SQL Sentry have made the pro edition of SQL Sentry plan Explorer FREE!  See this blog post for details.

Plan Explorer is a great tool, and we were lucky enough to have Sofia Ng take us through some uses for it at one of our Nelson SQL Server User Group Lightning Talks last year.  Now with all the pro features in there there’s even more reason to check it out if you haven’t already.