Posts Tagged ‘Powershell Scripting’

Discover SQL Servers with Powershell

With Powershell and SMO you can easily discover SQL Server instances running on your network in just a few lines of code. ?View Code POWERSHELL[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null; $smoObj = [Microsoft.SqlServer.Management.Smo.SmoApplication];   # This gets the sql servers available $sql = $smoObj::EnumAvailableSqlServers($false)   foreach($sqlserver in $sql) { Write-Host -ForegroundColor Green "Discovered sql server: " $sqlserver.Name; } […]

Merging CSV Files with Powershell

Powershell is really useful for documenting and managing your servers but it’s also a pretty good tool for working with data. I’ve been using it to merge csv files, with an identical structure, into a single file. Now this is pretty easy, if rather tedious, to do using SQL Server Import / Export functionality or […]

Writing filenames to a text file with Powershell

I had a task to do today that required me to get all the names in a directory of files into a database. This seemed like a ideal job for Powershell and I’ve posted the (very simple) script here. There’s a few variables you need to change to fit your environment; $directory – this should […]

More Powershell Nuggets

In a previous post I provided a few small Powershell code blocks suitable for beginners to digest. Here are a few more that anyone starting with Powershell might like to experiment with. A simple For Loop in Powershell Here’s just a simple for loop in Powershell. ?View Code POWERSHELL# A simple for loop for($i = […]

Check for failed SQL Agent Jobs with Powershell

Checking for failed SQL Agent jobs should be part of any DBA workplan. Here’s another Powershell script that makes checking the last run outcome easy on multiple SQL Servers. To run this script you need to create a list of your SQL Servers in a text file called sqlservers.txt. Place this text file in your […]