Saturday, October 13, 2018

Calculating Password Entropy

Needed to calculate password entropy, for a strength calculator. Used formula reference and strength categorization from https://www.pleacher.com/mp/mlessons/algebra/entropy.html

Update:

Implemented an entropy calculator in Google Sheets here

Friday, October 12, 2018

Create a C# POCO w/ Navigation Properties from MSSQL Schema

I use this every once and a while. Its pretty handy. It determines primary key by the column ending "_ID". So obviously that won't work for everyone.


Wednesday, October 10, 2018

Change your Adapter order for proper DNS

I do most of my work over a VPN. It works really well using either the CISCO AnyConnect client or the OpenConnect client here.
https://openconnect.github.io/openconnect-gui/

 But... after recent system changes, I was getting some peculiar behavior. In particular some internal addresses were getting resolved externally. What was going on?

Well, after banging my head against the keyboard...


I finally realized that my address was getting resolved by a different dns server under different circumstances. Doing a plain Jane

nslookup internalurl.com

resolved to one IP, while

ping internalurl.com
resolved to another. Looking further I found that Windows 10 manages the order of adapters. Using a VPN, I have my actual ethernet adapter, and my virtual adapter defined by the VPN. Its the virtual adapter that has the internal DNS servers, so I needed it to be first in priority. Got some great info here:
https://www.windowscentral.com/how-change-priority-order-network-adapters-windows-10

This is how I fixed it via PowerShell

This command gave me the list of my adapters...
Get-NetIPInterface
I found the VPN adapater's interface index, and set its interface metric to a value less than the ethernet adapter's metric like this:
Set-NetIPInterface -InterfaceIndex 24 -InterfaceMetric 30

Instantly, my internal domains started resolving correctly again.


Friday, September 28, 2018

Healthy Websites

I wanted a reusable health page that would indicate a website is up an running. I included a check to make sure the sql server is working as well.

Wednesday, July 02, 2014

Invoke-Sqlcmd - Where's my error?


So, I noticed some weird behavior when executing sql from powershell using the Invoke-Sqlcmd cmdlet.

I wanted to make sure that when I got an error, I updated the return value.

So, I modified my script file I was executing to simply :
 SELECT 1/0  

No problem, right? But everything worked fine.

So, I check the call to my script - made sure ErrorAction was set to stop, as suggested here

 $retValue = 0  
 try {  
      Invoke-Sqlcmd -Query "select 1/0;" -ServerInstance localhost -Database "tempdb" -Username "sa" -ErrorAction Stop -ErrorLevel 0 -SeverityLevel 0 | Out-Default  
 } catch {  
   #$_ | Out-Null  
      echo "Error!"  
      Write-Host($_)  
      $retValue = -1  
 }  
 return $retValue  

Results? What????
 PS SQLSERVER:\> C:\temp\test.ps1  
 0  

Now, its not like the error handling doesn't work. If I do anything successful before the divide-by-zero, it looks correct:
 $retValue = 0  
 try {  
      Invoke-Sqlcmd -Query "select 'anything successful'; select 1/0;" -ServerInstance localhost -Database "tempdb" -Username "sa" -ErrorAction Stop -ErrorLevel 0 -SeverityLevel 0 | Out-Default   
 } catch {  
   #$_ | Out-Null  
      echo "Error!"  
      Write-Host($_)  
      $retValue = -1  
 }  
 return $retValue  

Results... Ok...
 PS SQLSERVER:\> C:\temp\test.ps1  
 Column1                                                                                                         
 -------                                                                                                         
 anything successful                                                                                                   
 Error!  
 Divide by zero error encountered.  
 -1  

But whats going on? After a little research, it appears to be a BUG in the invoke-sqlcmd cmdlet. In fact, it seems they almost fixed it in CTP2 of SQL 2014, but for some reason chose to leave it out. The fix was a new parameter: "IncludeSqlUserErrors"
 invoke-sqlcmd -ServerInstance . -Database tempdb -Query 'select 1/0' -IncludeSqlUserErrors;  

But I can't find any documentation on this in the actual release. So, I'm guessing for whatever reason it was rolled back. Of course, I'm not sure why I should have to pass an additional parameter to get my error. In short, I don't think I can trust invoke-sqlcmd. I really like the syntax and its integration with powershell. But how will I ever feel confident that my script actually ran, with this sort of behavior?

So far, the divide by zero as the first statement in the batch is the only way I've found to replicate theis behavior.





Friday, June 06, 2014

Why is @@ServerName NOT the Name Of My Server?

I couldn't figure out why it was failing? It worked perfectly in the prototype environment that this was based on. In fact, these were clones of the machines.

Finally, I tracked it to the line

 select @replica_id = replicas.replica_id   
 from master.sys.availability_replicas as replicas   
 where   
    upper(replicas.replica_server_name COLLATE Latin1_General_CI_AS) = upper(@@SERVERNAME COLLATE Latin1_General_CI_AS) and group_id = @group_id  

 It turns out, that @@ServerName was returning a value that I had never heard of. It certainly wasn't my Sql Server name! But... it WAS the name of the cloned machine before it had been sysprepped and handed to me! It seems that @@ServerName uses a stored version of the servername.

If you want the current running name, use:

 SERVERPROPERTY('ServerName')   

Much better.

Friday, May 10, 2013

A Grammar Book For You And I Oops Me. This

A Summary of Test Document - The Book!

This is my test document. I summarized the book "How to make a test document" by Fred Fredrickson. The book was really good, but some of the information was less than clear. I disagreed with the author on his points regarding x and z, but his take on Y was really excellent.