Various PowerShell functions and scripts
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}
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:
- Added the option to pass in a PSCredential instead of a plaintext password
- Added PSObject output type to allow comparisons without odd [System.DBNull]::Value behavior:
- Previously, many PowerShell comparisons resulted in errors:
- With PSObject output, comparisons behave as expected:
- Previously, testing for nonnull / null values did not work as expected:
- With PSObject output, null values are excluded as expected
- Speed comparison between DataRow and PSObject output with 1854 rows, 84 columns:
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.
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.
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 : )
I've copied and will continue to update my TechNet gallery contributions within this repository.
- ConvertFrom-SID
- Get-ADGroupMembers
- Get-FolderEntry
- Get-GPPFile
- Get-GPPShortcut
- Get-InstalledSoftware
- Get-MSSQLColumn
- Get-NetworkStatistics
- Get-PropertyType
- Get-ScheduledTasks
- Get-UACSetting
- Get-UserSession
- Invoke-Parallel
- Open-ISEFunction
- Test-ForAdmin
Would love contributors, suggestions, feedback, and other help!