## Rename a lot of databases with PowerShell

I was given the task of renaming a bunch of databases named something like “name,” “name_1,” “name_2,” etc., to “nameold,” “nameold_1,” “nameold_2,” etc.  Our convention is for the database name to match the file names.  So…

$controlfile=”path\to\listofdatabases.txt”$server=”servername\instance”
$mdfpath=”c:\path\to\Data”$ldfpath=”c:\path\to\Log”
$dbnameappend=”old”$dbnameappendarchive = $dbnameappend + “_” foreach ($dbname in get-content $controlfile) {$newdbname = $dbname +$dbnameappend
if ($dbname -match ‘_[0-9]’) {$newdbname = $dbname -replace ‘_’,$dbnameappendarchive}
Write-Host $newdbname$mdf=$dbname + “.mdf”$ldf=$dbname + “_log.ldf”$newmdf=$newdbname + “.mdf”$newldf=$newdbname + “_log.ldf”$query = “ALTER DATABASE ” + $dbname + ” SET SINGLE_USER WITH ROLLBACK IMMEDIATE;” sqlcmd -E -S$server -Q $query$query = “ALTER DATABASE ” + $dbname + ” MODIFY NAME = ” +$newdbname
sqlcmd -E -S $server -Q$query

$query = “ALTER DATABASE ” +$newdbname + ” SET offline”
sqlcmd -E -S $server -Q$query

$query = “ALTER DATABASE ” +$newdbname + ” MODIFY FILE (NAME = ‘[namingconvention]_log’, FILENAME = ‘” + $ldfpath + “\” +$newldf + “‘)”
sqlcmd -E -S $server -Q$query

$query = “ALTER DATABASE ” +$newdbname + ” MODIFY FILE (NAME = ‘[namingconvention]’, FILENAME = ‘” + $mdfpath + “\” +$newmdf + “‘)”
sqlcmd -E -S $server -Q$query

Move $ldfpath\$ldf $ldfpath\$newldf -Force
Move $mdfpath\$mdf $mdfpath\$newmdf -Force

$query = “ALTER DATABASE ” +$newdbname + ” SET online”
sqlcmd -E -S $server -Q$query

$query = “ALTER DATABASE ” +$newdbname + ” SET MULTI_USER;”
sqlcmd -E -S $server -Q$query

}

This was so fast that I decided to share the joy. 🙂

(I plan to use this script if I ever need to bring my log-shipped databases online, hence the append.  I might need to do thing_oldprod, stuff_oldprod, whatsit_oldprod, etc.)

Comments Off on Rename a lot of databases with PowerShell

Filed under powershell, scripting

## Art is never finished, only abandoned.

I expect that I’m about to frustrate a lot of people, but that’s the way it is.

Back in 2007, I started a “Hurricane Katrina” story.  I wandered around in my post-apocalyptic Probably New Orleans, got lost, deleted 5,000 words, rewrote it a bazillion times, sent it through my writer’s group several times, and had a friend tell me to give up on the story in exasperation.  Another friend kind of felt it was too soon.

Trunk time.

In 2013 I fished it out.  It was better than I remembered.  Also, it kind of predicted the Occupy movement.  I sent it through my writer’s group, sent it to a friend who told me it was a novel, and sent it to my sister.  My sister felt that there wasn’t enough voodoo (fixed!), and advised me to fly to New Orleans and soak up atmosphere (appealing, but…).

I sat on it for another three months, but as I am unlikely to add 80,000 words to my 7,250 word story, I decided to send it out instead.  So, “The Salvage of Nola City” is now making the rounds.

Chinequa could feel the network congestion, almost like sinus pressure. Too much information. She tried to tune it out so she could concentrate on her job, but she could still feel it, trickling at the edge of her consciousness like a crowd in the distance. It didn’t help that Mr. Libby had the door open so he could load his transport, and the heat and humidity were getting in, nor did it help that she didn’t even know if her sister Keisha was still in the city. She hadn’t seen Keisha since Grandma’s funeral, which hurt, because they’d been inseparable as children. But if she got fired the bank would repossess her neural interface implants, so she needed to think.

Comments Off on Art is never finished, only abandoned.

Filed under short fiction

## SQL Server Express Maintenance

Don’t you love looking at your old scripts?

I’ve rewritten all of our maintenance jobs, including the jobs in task manager that handle SBSMonitoring.  I’ve rid myself of Windows Internal Database, but was still using the script I wrote for it.  Not any more!

I decided to use and abuse Ola Hallengren’s scripts, so what I’m using for SBSMonitoring is:

Download and install CommandExecute.sql, DatabaseIntegrityCheck.sql, and IndexOptimize.sql. Then, create the following .sql file:

Declare @4wks datetime
set @4wks = DateAdd(hh, DateDiff(hh, 0, GETDATE()) – 672, 0)

EXECUTE [dbo].[IndexOptimize] @Databases = ‘ALL_DATABASES’, @OnlyModifiedStatistics = ‘Y’
EXECUTE [dbo].[DatabaseIntegrityCheck] @Databases = ‘ALL_DATABASES’

exec msdb.dbo.sp_delete_backuphistory @oldest_date = @4wks

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘dbo’ AND TABLE_NAME = ‘CommandLog’))
BEGIN
DELETE FROM [dbo].[CommandLog] WHERE DATEDIFF(dd,StartTime,GETDATE()) > 30
END

IF @@SERVERNAME = ‘MYSBSSERVER\SBSMONITORING’
BEGIN
EXECUTE [SBSMonitoring].[dbo].[CleanupDatabase]
END

This can be called with sqlcmd in Task Scheduler.  (“sqlcmd -E -SmyServer -i c:\path\to\scriptname.sql”)

I’ve also started using Microsoft’s script to reindex SUSDB.  I migrated that into SQL Server, so it’s an actual job, but SBS doesn’t support migrating the SBSMonitoring database out of the SBSMonitoring 2005 Express instance that’s installed by default, hence the scripty maintenance.

Comments Off on SQL Server Express Maintenance

Filed under sql

## Backups FTW!

I could say it’s because I’m a DBA and SysAdmin and it’s an occupational hazard, and frequently do.  I could also say it’s because I’m neurotic.  😉  But it’s probably the most honest to say, “Because I once experienced a catastrophic data loss…”

For whatever reason–pick one or more above–I’m compulsive about backups of my home equipment.  I actually use two different backup programs for different use cases:

1. “My house burned down.”
2. “I accidentally deleted a file.”
3. “My computer is so dead that I can’t boot it to get the data off it, so I bought a new one.”
4. “My hard drive died.”

For the first three, just about any cloud-based solution will work.  The first product I started using I picked for a highly dorky reason:  Neil Gaiman, my author crush, tweeted about it.  Crashplan protects me against data loss caused by stolen equipment and burning houses, restores accidental deletions, and when my old laptop died it painlessly put my data on the new laptop.  (Okay, yes, I could have cracked the case, put the old drive in an external enclosure, etc.  The point is that I didn’t have to.)

However, it’s not really intended for use #4.  Well.  You could use it for use #4, after reinstalling the OS and all your apps, or putting the vendor image back on and reinstalling your apps, or whatever.  However, what I really want for use #4 is something that pulls an image.  For that, I use Acronis, which I picked because my office uses it.

Acronis has a cloud service, but my main desktop has iTunes, and multiple season passes of NCIS and Doctor Who and the like, so I’m using 390 GB.  I’m a little… iffy… about uploading 390 GB worth of data a second time.  I also haven’t compared the cloud storage prices.  However, I <3 Acronis as of last week, because my hard drive failed.  In my main desktop.  The one with all that data on it.

I replaced the hard drive with a similarly-sized non-dead drive, booted it off the previously-burned recovery media, set it to restoring, and went to bed.  The next morning, it was like nothing ever happened.  Well.  I had to reapply a Windows update and recover a file.

So, my point is… Neil Gaiman is hot enough that I buy backup programs because he tells me to?  😉

Seriously, that could have been painful and traumatic, and instead it just ran overnight and all was love and goodness.  Because I do backups.  Backups FTW!

Filed under geekiness

## The Career Venn Diagram

Every now and then, people ask me for career advice.  Or, you know.  They start complaining about their careers and I annoy them with unsolicited advice.  😉  Either way, my advice for choosing a career comes down to:

1. Things you can do
2. that people will pay you to do
3. that don’t suck.

As a Venn Diagram, it looks like this:

(Sorry about the graphic. MS Paint FTW!)

You’d be surprised–or maybe you wouldn’t–by the resistance I get to this suggestion.  “Well, that’s fine for you because you’re in IT, but no one will pay me to sing opera/write poetry/play World of Warcraft/play ice hockey/go to Mars/etc.”

I didn’t specify that it had to be your lifelong dream.  I know that little children usually say they want to be an astronaut, or a ballerina, or write books, or be the President, and don’t generally say they want to be accountants or purchasers or write limericks about snack foods.  I just said a job that doesn’t suck.  Apparently, many people believe that if it doesn’t suck, no one will pay them to do it.  Not true!

Lots of people settle for two out of three.  (Things you can do that people will pay you to do is the only viable two out of three.  You can do things you can do that don’t suck for no money if you’re independently wealthy, or you can try to do things that don’t suck that pay well but that you’re ill-suited to doing.  For most people, neither of those will last long.)  Of course, you need to eat, but I always urge people to go for three out of three.

So, if you’re someone who’s considering a transition–like someone I know–think of the Venn diagram when you’re doing your research.  Don’t invest a lot of time and energy into moving into a high-paying direction you hate, or a high-unemployment and/or low-paying direction.  They pay some people to play ice hockey, but if you’re 45 and have bad knees it probably won’t be you.  That doesn’t mean your life has to suck.  There are alternatives.

Comments Off on The Career Venn Diagram

Filed under career

## Fun with PXE, redux.

You may recall that I’m serving up not only Windows Deployment Services capture and deploy but also Acronis recovery media over PXE.

Well, now I’m also serving up Windows Defender Offline. When chosen from the PXE boot menu, it boots into Windows PE, automagically launches Defender, updates the definitions, and runs a full scan.  And then sits there and waits for you to see the results and hit reboot.

Here’s what I did.  This assumes you have Windows Deployment Services and the Windows Automated Installation Kit.

2. Run the tool to create an ISO (“to burn to CD/DVD later”).
3. Mount the ISO with Virtual CloneDrive (or similar product).
4. Fish boot.wim out of /sources on the virtual CD drive.
5. Create a directory to mount the wim into if one doesn’t already exist.
6. Mount boot.wim into that directory. Dism /Mount-Wim /WimFile:C:\temp\boot.wim /MountDir:C:\temp\wim /Index:1
7. If you have a directory full of drivers for your environment, you can pre-emptively add them with: Dism /Image:C:\temp\wim /Add-Driver /Driver:c:\drivers /Recurse /ForceUnsigned. (If not, cross your fingers and carry on with the next step.)
8. Copy mpam-fex64.exe (or similarly named file for x86)  into the top level of the wim from the top level of your virtual CD drive.
9. Save and unmount the wim.  Dism /Unmount-Wim /MountDir:C:\temp\wim /Commit

I’ve also rewritten a lot of my batch scripts to be PowerShell, but I think that’s a post for another day.

Comments Off on Fun with PXE, redux.

## New story available! and bonus snippets.

Thing the first:  My story “Ondine’s Curse” is available in the latest (last, alas) issue of Electric Velocipede.  The Kindle version is here.  The Nook version is here.

Thing the second:  I have two more flash pieces in circulation and forgot to post snippets!

Aiele’s gold bracelets jangled as she adjusted her veil in the mirror, making certain that the bruises didn’t show. She didn’t want anyone to know that she had already failed to please her new husband Notan. Bruise or no bruise, limp or no limp, she had food to order for Notan’s dinner party.

“Hello, Bianca,” the wizard said as he walked through the sitting room door. He was old and fat and rich, and smelled of horrid things–pickled frog’s eyes, perhaps. His hair was greasy and white, and he was dressed in black from head to toe.

Bianca resisted the urge to scoot closer to her sister Esmerelda on the couch and shivered, but not from the cold. She cast her eyes down in an effort to be demure, staring at her dress–blue silk and lace. Esmerelda’s dress was dark and brown, like the wood paneling on the walls, and not as nice as Bianca’s. Bianca felt guilty about that. She didn’t know why her parents liked her best.

Comments Off on New story available! and bonus snippets.

Filed under short fiction

## They’re using HOW MUCH space?

This assumes a convention where databases are named after the responsible party with an underscore and a version number or description.  It also assumes that running this script is faster than remoting to the server and looking at the directory.  😉

declare @dbname varchar(255), @CurrentRow int, @RowsToProcess int, @SQL nvarchar(max), @size float

CREATE TABLE #dbs ( RowID int not null primary key identity(1,1), dbname varchar(255), owner varchar(80), size float )

insert into #dbs (dbname) select name from sys.databases where name not in ('master','msdb','model','tempdb') set @RowsToProcess=@@ROWCOUNT

SET @CurrentRow=0 WHILE @CurrentRow<@RowsToProcess BEGIN SET @CurrentRow=@CurrentRow+1 SELECT @dbname=dbname FROM #dbs WHERE RowID=@CurrentRow set @SQL = 'update #dbs set size=(SELECT sum(size) FROM ' + @dbname + '.sys.database_files) where dbname=''' + @dbname + '''' EXEC SP_EXECUTESQL @SQL set @SQL = 'update #dbs set owner=(SELECT CASE WHEN CHARINDEX(''_'', dbname) > 0 THEN LEFT(dbname, CHARINDEX(''_'', dbname)-1) ELSE dbname END AS Owner FROM #dbs where dbname=''' + @dbname + ''') where dbname=''' + @dbname + '''' EXEC SP_EXECUTESQL @SQL END

select owner, COUNT(owner) as DBCount, ((((SUM(size) * 8192)/1024)/1024)/1024) as TotalGB from #dbs group by owner

drop table #dbs

Comments Off on They’re using HOW MUCH space?

Filed under scripting

## SFTP – copy smallest files first

I needed to SFTP a bunch of files ranging in size from 40 meg to 130 gig.  The default behavior of “mput *” is to copy the files in alphabetical order, but what happens then is that all the smaller files pile up behind the large ones.  This is bad because there’s a time limit and I want to copy as many as possible before the time limit passes.  The solution?

Pre-sort them by size, smallest to largest.

These are encrypted database backups, so the script requires a SQL Server instance name.  You can, of course, edit that behavior.

This depends upon PuTTY and Pageant.  Create an SSH profile with key-based authentication and use Pageant to handle the key as described in the link.  My SSH profile in this case is named “Atlanta” because that’s where the files are being copied.

This also assumes that you have directories in the SFTP server named with the pattern servername-instance because I’m running multiple streams.  Also, the last thing the database backup does is copy a file called “flag.txt” to the backup directory so the script knows when backups are done and can start copying.

set instance=%~1
if %instance%z==z goto :_help

set flag=”f:\full backups\%instance%\flag.txt”

:loop

IF EXIST %flag% (
GOTO file_found
) else (
ping 127.0.0.1
GOTO loop
)

:file_found

dir “f:\full backups\%instance%” /os /b /a-d-h > c:\temp\%instance%.txt

echo lcd “f:\full backups\%instance%” >> c:\temp\%instance%-control.txt
echo cd %computername%-%instance% >> c:\temp\%instance%-control.txt

FOR /F %%L IN (c:\temp\%instance%.txt) DO (
echo put %%L >> c:\temp\%instance%-control.txt
)

echo quit >> c:\temp\%instance%-control.txt

“c:\Program Files (x86)\PuTTY\psftp.exe” Atlanta -b c:\temp\%instance%-control.txt

del c:\temp\%instance%-control.txt
del c:\temp\%instance%.txt

goto :_end

:_help
@echo.

:_end
exit

Linked as a separate file because smart quotes are not smart.

Comments Off on SFTP – copy smallest files first

Filed under scripting