Skip to content

Latest commit

 

History

History
76 lines (55 loc) · 4.4 KB

README.md

File metadata and controls

76 lines (55 loc) · 4.4 KB

PowerShell

Various PowerShell functions and scripts

Instructions

These files contain functions. For example, Invoke-Sqlcmd2.ps1 contains the Invoke-Sqlcmd2 function.

#Download and unblock the file(s).
#Dot source the file(s) as appropriate.
. "\\Path\To\Invoke-Sqlcmd2"

#Use the functions
Get-Help Invoke-Sqlcmd2 -Full
Invoke-Sqlcmd2 -ServerInstance MyServer\MyInstance -Query "SELECT ServerName, VCNumCPU FROM tblServerInfo" -As PSObject -Credential $cred | ?{$_.VCNumCPU -gt 8}

Invoke-Sqlcmd2

I'm a fan of Invoke-Sqlcmd2. Props to Chad Miller and the other contributors for a fantastic function. I've added a few features with much help from others:

  • Added pipeline support, with the option to append a ServerInstance column to keep track of your results:
    • Add ServerInstance column
  • Added the option to pass in a PSCredential instead of a plaintext password
    • Use PSCredential
  • Added PSObject output type to allow comparisons without odd [System.DBNull]::Value behavior:
    • Previously, many PowerShell comparisons resulted in errors:
      • GT Comparison Errors
    • With PSObject output, comparisons behave as expected:
      • GT Comparison Fix
    • Previously, testing for nonnull / null values did not work as expected:
      • NotNull Fails
    • With PSObject output, null values are excluded as expected
      • NotNull Fails Fix
    • Speed comparison between DataRow and PSObject output with 1854 rows, 84 columns:
      • Speed PSObject v Datarow

That DBNull behavior is strange! Why doesn't it behave as expected?

I agree. PowerShell does a lot of work under the covers to provide behavior a non-developer might expect. From my perspective, PowerShell should handle [System.DBNull]::Value like it does Null. Please vote up this Microsoft Connect suggestion if you agree!

Major thanks to Dave Wyatt for providing the C# code that produces the PSObject output type as a workaround for this.

You clearly don't know SQL. Why are you working on this function?

I absolutely do not know SQL. If I'm doing something wrong please let me know!

I have a number of projects at work that involve PowerShell wrappers for SQL queries. Invoke-Sqlcmd2 has been my go-to command for this - now that I'm spending more time with it, I plan to add some functionality.

Why is Invoke-Sqlcmd2 here?

I copied the code here to avoid the automated tweets for Poshcode.org submissions. I make many small changes and didn't want to spam twitter : )

TechNet Galleries Contributions

I've copied and will continue to update my TechNet gallery contributions within this repository.

Help!

Would love contributors, suggestions, feedback, and other help!