Archive | March 2014

Creating an ODBC connection

With Powershell 4 on Windows 8 and 2012, they’ve improve the support for ODBC connections. You just need to import the Wdac module to be able to use the different ODBC functions.

Get-Command *ODBC* shows the following functions:

Add-OdbcDsn
Disable-OdbcPerfCounter
Enable-OdbcPerfCounter
Get-OdbcDriver
Get-OdbcDsn
Get-OdbcPerfCounter
Remove-OdbcDsn
Set-OdbcDriver
Set-OdbcDsn

For instance, if you were to create a System ODBC connection, with a 32 bit SQL driver, use the following code:

Import-Module Wdac
$OdbcDriver = Get-OdbcDriver -Name *SQL* -Platform 32-bit
If(!$OdbcDriver.Count) # Only continue if 1 SQL ODBC driver is installed
{ Add-OdbcDsn -Name "ODBC Connection Name" -DriverName $OdbcDriver.Name -Platform 32-bit -DsnType System -SetPropertyValue @("Server=SQL ServerName\SQL Server Instance", "Trusted_Connection=Yes","Database=SQL DatabaseName") }

Making IT…

Making IT come to life

Quick command to open explorer from the current directory in Powershell

Here is a quick command if you want to open Windows Explorer from the current directory in Powershell:

ii .

(note the space between the ‘i’ and the dot)
ii is powershell’s alias for invoke-item. Adding a dot to that command will open the current directory using the default explorer.

Edit: If you’d like to achieve the same at a command prompt, you can run

explorer .

Run a powershell script if the computer contains the correct powershell version

Several times I encountered that a computer I was running my scipt on, didn’t support a certain parameter or a certain command because it didn’t have Powershell 4 installed. Thus I thought it would be time to create a default error-handling for each of my scripts which checks if the computer runs the correct Powershell version.
Since I mostly use powershell 4 on my own systems, I created this check for this version.

If ($PSVersionTable.PSVersion.Major -ge 4)
{
# Enter PS 4.0 code here
Write-Host("Your powershell version is 4.0")
}
Else
{
# Enter code here
Write-Error("You need at least Powershell 4.0 to run this program. Powershell is part of the Windows Management Framework and can be downloaded here: http://www.microsoft.com/en-us/download/details.aspx?id=40855") -Category NotInstalled -ErrorId "PSVersion < 4.0"
}

This way you can be sure the code will only run if Powershell 4 or higher is installed. If not, it’ll display an error message with a download link.

System Administrators…

System Administrators who don’t script, are doing IT wrong

Hi there and welcome

Hi there and welcome to my Powershell blog. I’ve always loved scripting to automate things or to make things easier. Now that I’ve encountered Powershell, I fell in love with it. Through this blog I hope to spread the love 😉

But seriously, Powershell is a great way to automate things. Even when you’re no expert, it’s not hard to learn it’s basics… and since it’s always nice to be the creator of something, why not give Powershell a try?

To me, the great thing about Powershell is that it keeps amazing me in its possibilities, I love that it works cross-platform and can connect and run things on connected computers as well, just like the fact that it can be found on nearly any computer.

Those who forge…

Those who forget to script, are doomed to repeat their work