Posts Tagged ‘Powershell Scripting’

Console input with Powershell

I’m looking at building some Powershell scripts that can accept user input to perform different tasks with a wizard style interface. As it happens this is fairly easily achieved with the Read-Host cmdlet. Here’s a quick script showing how such a powershell script may look.

?View Code POWERSHELLfunction mainMenu()
{
Clear-Host;
Write-Host "============";
Write-Host "= MAINMENU =";
Write-Host "============";
Write-Host "1. Press [...]

Managing Index Fragmentation with Powershell

Here’s a Powershell script that can be used to manage index fragmentation in SQL Server databases. The strategy I’ve used in the script is based on a recommendation from Pinal Dave (blog | twitter) in his article  Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script. Just set the $sqlserver and $database variables [...]

Powershell Random sort

Here’s a Powershell snippet that randomly sorts the lines in a file. The snippet below reads a text file, called file.txt, located in your user profile directory. The data in the file will be written back with the lines in a different order.

?View Code POWERSHELL$data = New-Object System.Object;
$data = Get-Content "$Env:USERPROFILE\file.txt";
# Random sort the lines [...]

Using .Net libraries in Powershell

One of the great things about Powershell is its ability to take advantage of the .Net platform. If Powershell can’t do it you bet there’s a .Net library that can. Not only is this great for extending your scripts but I also like to use Powershell to test some of my classes. Rather than fire [...]

Discover SQL Servers with Powershell via the registry

Chuck Boyce Jr (blog | twitter) recently commented on a limitation of the script from my post Discover SQL Servers with Powershell. The script does require that the SQLBrowser service is running for discovery to occur which may be a major issue for some. Here’s an alternative method that does not have this limitation. All [...]

Converting CSV FileS to XML with Powershell

Powershell is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml.

?View Code POWERSHELL# csv file to convert
$csv [...]

Trimming Whitespace with Powershell

A few days ago I was working with a client that was providing an export of data from Oracle. The file being produced was choking my SSIS package due to various formatting issues. After working with the client and getting a file that looked good to the naked eye I discovered that a large amount [...]

Splitting csv files with Powershell

I’ve blogged before about the usefulness of Powershell for data tasks. A few weeks ago I had a requirement at work for merging csv files and recently I needed to split a single csv file into several files.
While this is easy to do using SSIS and a bit of T-SQL it is a little tedious, [...]

Using Progress Bars within the Powershell Console

Progress bars can be a nice visual indicator as to how a far a task is into its workload. Windows Powershell provides us with the ability to create these within the console fairly easily.
This simple code will demonstrate the basics of the Write-Progress cmdlet, which allows us to deploy progress bars in our scripts.

?View Code [...]

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;
}

If this doesn’t seem to find all your sql servers [...]