Tag Archives: PowerShell

Removing dead library servers and hosts from SCVMM

A while back I was setting up TFS Lab management so that we can start automating a lot of our integration testing. In the process of testing the deployment of the servers and the auto configuration scripts I was creating a lot of temporary virtual servers on the domain and using up IP’s on the network before they could be released. Therefore I wasn’t too popular with the OP’s guys and they decided to put the lab server in it’s own domain.

Unfortunately this caused a few issues as it meant that the SCVMM backed could not contact the hosts and the library servers as they had moved domain. Removing the hosts was a simple matter of dropping to PowerShell (There is a handy button at the top of the SCVMM Admin console) and running the following command:

Remove-VMHost "[SERVER_FQDN]" -Force

However the PowerShell command for removing dead library servers is not so helpful as it doesn’t have a “Force” flag and it comes up with an error saying it can’t be contacted:

Error (406) - Access has been denied while contacting the server

Luckily as the SCVMM backend is controlled by Sql server it wasn’t too difficult to find where the definitions of the library server were stored. Just connect to the SCVMM database instance ([YOUR_SCVMM_SERVER]\MICROSOFT$VMM$) and the table you need to look at is the tbl_ADHC_Library table. Firstly do a select on the table to get the GUID/UNIQUEIDENTIFIER of the library server(s) you need to delete:

use VirtualManagerDB
go

select
    LibraryID,
    ComputerName
from dbo.tbl_ADHC_Library

From this query take the LibraryID field and for each of the library servers you need removing, run the following command:

use VirtualManagerDB
go

exec prc_ADHC_Library_Delete '[LibraryID]'

Hey presto all the dead servers are now gone!

As always please make sure you have a backup and don’t blame me if you break your SCVMM server!!

Invoke-SqlCmd throws exception when trying to set the database via parameter

This was properly doing my nut today! Basically I created a generic script to fix orphaned users and replaced the database with the standard SqlCmd variable syntax:

use $(DatabaseName);

When running in SSMS setting the variables like this:

:setvar DatabaseName MyDatabase

works fine, as it does when setting as a param to the sqlcmd.exe:

sqlcmd -i C:\PathToScript.sql -v DatabaseName=MyDatabase

however when using the PowerShell Invoke-SqlCmd Cmdlet from the SqlServer PowerShell extensions it throws an exception that gives you absolutely no clue as to why:

Invoke-Sqlcmd : Object reference not set to an instance of an object.
At C:\Users\mayesr\Desktop\PowerShellScratchPad.ps1:22 char:14
+ Invoke-Sqlcmd <<<< -InputFile "$g_ScriptLocation\FixUser.sql" -ServerInstance "localhost" -Verbose + CategoryInfo : InvalidResult: (localhost:PSObject) [Invoke-Sqlcmd], NullReferenceExcep tion + FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptComm and

I eventually noticed that the Invoke-SqlCmd has a parameter that you can set the database with. Once that was set and the use statement removed, the script worked fine! Luckily it didn’t take me too long to notice but hopefully if you come across this post it will save you some time!!

UPDATE

After I thought I had fixed this I realised that it wasn’t the Database param at all and in-fact was any param! I eventually found out there is a bug in the MSDN docs (and/or code)!! The docs say that the following format should be used:

$MyArray = "MyVar1 = 'String1'", "MyVar2 = 'String2'"
Invoke-Sqlcmd -Query "SELECT `$(MyVar1) AS Var1, `$(MyVar2) AS Var2;" -Variable $MyArray

However this doesn’t work and the following should be used instead (remove the spaces around the ‘=’):

$MyArray = "MyVar1='String1'", "MyVar2='String2'"
Invoke-Sqlcmd -Query "SELECT `$(MyVar1) AS Var1, `$(MyVar2) AS Var2;" -Variable $MyArray

I just wish I’d scrolled down to the bottom of the page as other people had added comments saying this!! 😡

Killing database connections with PowerShell

Recently I have been setting up a PowerShell script to automatically setup our PY (usually aka SIT) environment. One of the tasks in the script is to Kill all the connections to a database so I can restore a backup. I managed to find a function that met my needs using the SMO libraries. Unfortunately as we are still on Sql2000 (I know, I know! Its not for much longer now tho! 😀 ) the SMO Libraries were not available on our database server so I had to come up with an alternative. Basically I decided to go the route that required the least dependancies and this is what I came up with!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function KillDBConnections([string]$serverName, [string]$DBName)
{
    $ConnectionString = "Data Source=$serverName;Initial Catalog=master;Integrated Security=SSPI"
    $connection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString);
    $command = New-Object System.Data.SqlClient.SqlCommand;

    $command.Connection = $connection;
    $command.CommandType = [System.Data.CommandType]::Text;
    $command.CommandText = "SELECT spid FROM master..sysprocesses WHERE dbid=db_id('$DBName')";

    $connection.open();
    $reader = $command.ExecuteReader();
    $stringBuilder = New-Object System.Text.StringBuilder

    while ($reader.Read())
    {
        $stringBuilder.AppendFormat("kill {0};", $reader.GetValue(0));
    }

    $reader.Close();

    $command.CommandText = $stringBuilder.ToString();

    if($command.CommandText)
    {
        $command.ExecuteNonQuery();
    }
   
    $connection.Dispose();
}