Monthly Archives: December 2011

Backup and restore SQL databases using powershell

I needed a powershell script to quickly backup or restore SQL database’s before doing maintenance work, but without having to remote into an SQL server. With this task in hand I wrote the below module. I’ve sealed this as a module to be imported on runtime so you can quickly run a backup before making any changes to infrastructure utilising sql, safe with the knowledge you have a backup, and more importantly restore if you make a mess of it!

This module should be used before using my xenapp 6 backup, reset and restore commands or Edgesight scripts. You can get a copy of it here.

To import the module, run the below command (assuming you have saved the module to c:).

import-module c:sql.backup.psm1

Module functions:

  • Backup-SQLdatabase
  • Restore-SQLdatabase

Backup-SQLdatabase

This module allows you to connect to an SQL server and use native SQL commands to perform the backup to a file from pretty much any windows machine with powershell installed.

This module supports trusted and untrusted connections and has built in error checking to catch typical problems and returns a job report as an object. The module will first try to connect and ensure you have the neccessary access rights, attempt to create the backup file, and assuming all has gone well then perform the task at hand:

[sourcecode language=”Powershell”]
Backup-SQLdatabase -SQLServer "sqlserver" -SQLDatabase "database" -Path "servershare$backup.bak" -SQLTrustedConnection
[/sourcecode]

(Private information ommitted)

Restore-SQLDatabase

This module allows you to connect to an SQL server and use native SQL commands to perform a restore from a file from pretty much any windows machine with powershell installed.

This module supports trusted and untrusted connections and has built in error checking to catch typical problems and returns a job report as an object. The module will first try to connect and ensure you have the neccessary access rights, attempt to locate the backup file, set the sql database to single user mode and assuming all has gone well then perform the task at hand.

[sourcecode language=”Powershell”]
restore-SQLdatabase -SQLServer "sqlserver" -SQLDatabase "database" -Path "servershare$backup.bak" -SQLTrustedConnection
[/sourcecode]

(Private information ommitted)

If for any reason, the restore doesn’t work correctly the function will take the corrective action of setting the database back to multi-user.

A full copy of the module to be viewed can be found after the jump below:

Continue reading

Move objects in active directory which have been inactive for x days.

This is just a quick script I was asked for assistance with recently. The person in question wanted to move all computers and users to defined ou’s when they were inactive for 90 days.

The script is fairly self explanatory but quite scary if you get it wrong, for that reason I’ve included the -whatif parameter to show you what will happen if you overzealously just copy and paste the code. Once you are happy it works, remove the whatif parameters.

This script relies on the powershell module for active directory, you can see if its installed as below:

I’m also aware this code is quite inefficient by searching twice, but it was the cleanest appearance I could muster to ensure the end user understands what is happening.

[sourcecode language=”Powershell”]

#Load the required Snapins
if (!(import-module "activedirectory" -ea 0)) {
Write-Host "Loading active directory module." -ForegroundColor Yellow
import-module "activedirectory" -ea Stop
}#endif

#users
foreach ($user in search-adaccount -UsersOnly -AccountInactive -TimeSpan 90.00:00:00){
move-adobject -identity $user.DistinguishedName -targetpath "OU=Old Users,DC=some,DC=domain,dc=net" -whatif
}

#computers
foreach ($computer in search-adaccount -Computersonly -AccountInactive -TimeSpan 90.00:00:00){
move-adobject -identity $computer.DistinguishedName -targetpath "OU=Old Computers,DC=some,DC=domain,dc=net" -whatif
}
[/sourcecode]

The idiots guide to load balancing App-V LWS via a Citrix Netscaler:

I set about recently to load balance app-v lightweight streaming servers traffic across Netscalers. I found this task a little more tricky than I had hoped and decided it would be useful to break this task down to its most basic steps and publish this to help anyone else who needs to do this in future.

Despite great articles on Technet and hakabo.com, the sooner doesn’t have any fault tolerance for RTSP specific issues and focuses on the command line, which many administrators morbidly fear on the Netscaler. The later is geared at management servers which have slightly different requirements to LWS servers, the monitor recommended utilises commands which the LWS server does not support. Which results in downstate App-V servers at all times.

So below you will find an idiot proof guide (tested by this idiot) to marry two of my favourite pieces of technology into a kick ass solution.

What you will need:

  • Netscalers!
  • at least 2 App-V Light Weight streaming servers!
  • One netscaler Virtual IP address to load balance the traffic
  • About 30 minutes of time (including testing).

1: To start, take a note of the ip addresses currently configured on your App-V LWS servers.

2: Log into your wonderful netscalers.

First up, we’ll configure the monitor. This monitor will poll your App-V servers and ensure they are up and working.

3: From the configuration page, select Load Balancing > Monitors > Add:

4: Name the monitor something specific and easily identifed, then configure the below options:

  • type = RTSP
  • Interval =20
  • Destination port = 554

5: Click the Special Parameters tab, Then set the below values:

  • RTSP Request = Options *
  • Response Codes: 401

(yes yes, 401 is an error response, but the LWS server has to be actively accepting connections to throw this error)

6: Once finished, hit Create:

And that’s it, monitor configured!

Next, we’ll create a service group, this service group is a logical collection of our App-V servers. Here we will configure the ports and monitoring configuration to catch outages.

7: Browse to Load Balancing > Service Groups, then choose Add:



8: Name the Service group something specific and easily identifed, then define the following options:

  • Protocol = Any

9: Enter each IP address of the App-V server, ensure to choose * as the port and click Add. Repeat this step for each subsequent App-V server.

10: Once you’ve entered all your IP addresses, it should look as below: (ip addresses omitted)

11: Now flick to the Monitors tab and select the service group we created earlier:

12: Once finished, save your changes, the service group is done.

We’re on the home straight, now we’ll tie it all to a virtual IP and configure the load balancing specific settings:

13: Now to the Virtual server, browse to Load Balancing > Virtual Servers > Add.

14: Name the Virtual Server something specific and easily identifed, then define the following options:

  • Protocol = Any
  • IP address = the virtual ip address to be hosted by the netscaler.
  • Port = *

15: Flick to the Service Groups tab, then select the service group we just created:

16: Flick to the Method and Persistence tab, then configure the following options:

  • Method = Least connection
  • Persistence = Source IP
  • ipv4 Netmask = 255.255.255.255

17: Now flick to the Advanced tab, then configure the following options:

  • Redirection mode = IP Based

Thats it, we’re done! time to test.

18: Open up a telnet client, and attempt to telnet to the newly configured virtual server:

19: If all has gone to plan, the window should empty its contents and appear as below:

20: Now go celebrate your success and genius. If for some reason it has not worked, feel free to drop a comment and I’ll see what I can assist you with.

Configuring an “Unattended Install” shell extension for quick msi installation.

We’ve all been there, A list of msi’s necessary for a clients application and clicking next next next begins to wear thin. For me I had 7 servers to install 4 msi’s and scripting wasn’t an option as the software in question couldn’t be run in a non-interactive session.

To alleviate the issue, I added a simple shell extension to perform unattended installs of each of the msi’s:

I’ve made the registry file available here for download should you also be stuck with this task at some point.

The registry file is very simple as you can see below: