Wednesday, February 16, 2011

How to set the Windows Process Priority via Powershell and WMI

Sometimes you want to increase or decrease the priority of a windows service e.g. if you have single-core QA VMWare instances with limited capacity. This can be done via powershell quite easily

(get-wmiobject Win32_Process | where { $_.Name -eq "fabric_keeper.exe"}).SetPriority(32768)

You can get the list of valid values for the Process priority from MSDN. Make sure to check the return value
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 5
Return code Description
0 .. Successful completion
2 .. Access denied
3 .. Insufficient Privilege
8 .. Unknown Failure
9 .. Path Not Found
21 .. Invalid Parameter
In our case it was Access Denied and Insufficent Priviledge. A workaround would be to run the powershell as NT Authority\LOCAL SYSTEM. The easiest and most reliable way is to use psexec from the Windows SysInternals.

psexec -i -s C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

Execute the command again and you should get

__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0

Monday, January 18, 2010

ASP.NET MVC – Simple Remote Desktop connection controller

With the advancement of larger datacenters and upcoming cloud solutions it can become tedious to remember all the machine names to establish RDP connection for maintenance and debugging. Usually, after while in enterprise solutions, you start creating some maintenance applications listing all the hosts involved. Be it for gathering log-information, package deployment, … What I missed was a easy way to just click on a link and open a RDP session. Our continuous integration server (TeamCity from JetBrains) has a solution for this. It just simply streams a dynamic generated .RDP file. Here is a simple solution to implement this in ASP.NET MVC.

Controller

using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcTestWebApplication.Controllers
{
   public class RdpController : Controller
   {

       //
       // GET: /Rdp/hostname

       public ActionResult Index(string hostname)
       {
           // not hostname specified -- return a message
           if (string.IsNullOrEmpty(hostname))
               return View();

           var content =
               string.Format(
                   @"
screen mode id:i:1
desktopwidth:i:1024
desktopheight:i:768
smart sizing:i:1
session bpp:i:16
compression:i:1
keyboardhook:i:2
audiomode:i:2
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:1
displayconnectionbar:i:1
autoreconnection enabled:i:1
alternate shell:s:
shell working directory:s:
disable wallpaper:i:1
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:1
disable cursor setting:i:0
bitmapcachepersistenable:i:1
full address:s:{0}
domain:s:null
",
                   hostname);


           return new FileContentResult(
               Encoding.UTF8.GetBytes(content),
               "application/rdp")
                      {
                          FileDownloadName = hostname + ".rdp"
                      };
       }



   }
}

Global.asax

Register the Routes in Application_Start in global.asax

     routes.MapRoute(
                   "Rdp",
                   "Rdp/{hostname}",
                    new { controller = "Rdp",
                          action = "Index" });

Use it in a View

<%=Html.ActionLink("RDP localhost","Index", "Rdp",
                    new { hostname = "localhost"}, new {}) %>

A overview of the RDP file settings can be found here.

Monday, April 20, 2009

First experience with blog’s …

Since the blog community is such a big help in daily work, I wanted to see if I can give something back and also start a blog. I created one blogspot almost 3 years ago, but never followed up on it.

So – since I though I have something interesting to post, I gave it a try. I like good tools so I went on a mission to get my tools together. First step was to pick a place to host the blog. I have a web hosted environment on 1and1.com, so I tried their blog … don’t do it, that was the first disappointment. Wordpress.com was the next stop. For the heck of it, I couldn’t find a template that was NOT fixed wide. I hate those. What is the point of a wide screen monitor if only 750 pixels are used ? Especially since I would be posting code fragments.

Going back to blogspot – the templates are not any better, but after a lot of searching, try and error the one I used is at least not fixed width. I know it is ugly – but it serves the purpose.

Next stop – I like to see how it looks when I write. I read a lot of good things about Windows LiveWriter. The annoying part is that Microsoft REALLY want'’s you. If you not careful saying No to most of the components on the installer, it takes over your system …

It is actually a pretty good tool. Next stop was to find a way to pretty-print the source code fragments. What a odyssey – After trying numerous Plugin’s I settled for SyntaxHighlighter from Alex Gorbatchev along with SyntaxHighlighter for Windows Live Writer Plugin.

The post that really got me going was How to display code (nicely) in Blogger posts | techqi

And the most important information in there

Additionally, the code might include break tags before lines. In Blogger, go to Settings>Formatting and set 'Convert line breaks?' to No. (Note- this will remove line breaks throughout your previous posts as well, and you'll have to resort to pasting lots of double-break tags.) Unfortunately, this seems like the only solution at this point.

 

Sunday, April 19, 2009

Creating a Subversion Changelog via MSBuild …

The other day, I thought it would be a good idea to provide a generated ChangeLog for our integration builds. We are using TeamCity and MSBuild projects.
After searching for a while, I found only one solutions – svn2cl by Arthur de Jong. It is pretty straight forward, using the xml output of the svn log command and applying a XSL Stylesheet. It supports the standard GNU Changelog text format and a basic html formated one. The tool comes standard as a korn-shell script for Unix only, but luckily there is a windows port by Iwasa Kazmi using csript.
I could have just called the script via a <Exec> task, but this would have been no fun. The script uses the MSBuild Community Extensions.
In addition to just the Changelog I wanted to include
  1. Creation time
  2. The Subversion Repository root and revision
  3. HTML format with revision links back to our trac – changeset browser
  4. Embed the style sheet, so the generated html file is self sufficient
So – let’s get on with it. In your MSBuild project, import the “svn2cl.targets” project, set some properties and call the target or set a dependency.

How to use it

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

 <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
 <Import Project="./Build/svn2cl/svn2cl.targets"/>

 <PropertyGroup>
   <!-- pick up the workspace repository root or overwrite here -->
   <!--<Svn2clRepositoryPath> ... </Svn2clRepositoryPath>-->
   <!-- the marker ## will be replaced with the revision number -->
   <Svn2clRevisionLink>https://trac-location/trac/project/changeset/##/</Svn2clRevisionLink>
   <Svn2clLimit>100</Svn2clLimit>
   <Svn2clChangelogFile>./Changelog.html</Svn2clChangelogFile>
   <!-- by default take the current workspace revision -->
   <!--<Svn2clTopRevision>HEAD</Svn2clTopRevision>-->
 </PropertyGroup>

 <Target Name="BuildChangeLog" DependsOnTargets="SvnChangeLog"/>
</Project>

Repository information

I am using the SvnInfo task from the Community Extensions to determine information about the workspace.
    <!-- If Svn2clRepositoryPath is not set, get the information from the current working directory -->
   <SvnInfo LocalPath="$(MSBuildProjectDirectory)" Condition="$(Svn2clRepositoryPath) == ''">
     <Output TaskParameter="RepositoryPath" PropertyName="Svn2clRepositoryPath"/>
     <Output TaskParameter="Revision" PropertyName="Svn2clTopRevision"/>
   </SvnInfo>

   <Error Condition="$(Svn2clRepositoryPath) == ''" Text="Could not determine the Svn2clRepositoryPath"/>

Embed the CSS Style Sheet

I didn’t like the fact that the CSS Style sheet was just referenced, so I changed the XSLT to include it.
<!DOCTYPE xsl:stylesheet [
<!ENTITY newl "&#38;#xA;">
<!ENTITY space "&#32;">
<!ENTITY css SYSTEM "svn2html.css">
]>
...
<xsl:template match="log">
 <html>
  <head>
   <title><xsl:value-of select="string($title)" /></title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
       &css;
    </style>      
  </head>
  <body>

Subversion UID’s to real user names with email

The XSL can also read author information from a xml file and include them in the Changelog.
<?xml version="1.0" encoding="utf-8"?>
  <authors xmlns:html="http://www.w3.org/1999/xhtml">
   <author uid="bscholze">
     Bernd Scholze &lt;<html:a href="mailto:bscholze@gmail.com">bscholze@gmail.com</html:a>&gt;
  </author>
  <author uid="nobody">
     Who's this &lt;nobody@example.com&gt;
  </author>
</authors>
Download the MSBuild task.