This is an easy one this week, but it hand me wondering for a while if i could do it and when it worked i did have to sit back and smile.

So you load SQLPS and your connected to your local instance.. and your knee deep in DIRing against your tables.

SQLPS01

and you decide you want to connect to a dev server or whatever and see what dbs are in there. you might be thinking of bringing out Invoke-SQLCMD but there is an easy way. you can simply just change the connection. say with something like

## new server you want to query
$SecondServer = "ServerName"

## build out the path you want to connect to
$DatabasesPath = "SQLSERVER:\sql\$SecondServer\default\databases"

## save your current location to come back to
Push-Location

## change to new location
Set-Location $DatabasesPath

## do something
Get-ChildItem

## come back 
Pop-Location

SQLPS02

And there you have it… by simply changing the name of the server in the path you connect to the other server, I use push and pop to allow myself to come back to the orginal server in a quick way.

As i said not a massive brain teaser. but i think its pretty handy to know!

Advertisements