Invoke .Net 4 assembly with the assembly’s .config file using PowerShell

June 29, 2011

In a project developed using .Net 4, we needed to make some functionality available for scripting in PowerShell.

  • The first task was to configure PowerShell to be able to load .Net 4 assemblies:

The $pshome variable is the directory where the powershell.exe and its configuration file powershell.exe.config reside (the .config does not seem to be required on Win7 as it was not on my system). On 32-bit systems, the path is C:\Windows\System32\WindowsPowerShell\v1.0, 64-bit systems have C:\Windows\SysWOW64\WindowsPowerShell\v1.0.

Create a file called powershell.exe.config in a temp directory, and paste this code (found on SO)

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0.30319"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

Open a command prompt as administrator, and copy the file to PowerShell’s directory

copy .\powershell.exe.config
  c:\windows\system32\windowspowershell\v1.0\powershell.exe.config

Restart PowerShell, and you are able to load .Net 4 assemblies.

  • Next, we need to set up the .Net environment so that it uses the assembly’s configuration file instead of PowerShell’s.

Since the executable that is loading the assembly is powershell.exe, it is thus looking for the powershell.exe.config. To use the correct config file, we need to set the current AppDomain’s APP_CONFIG_FILE property (found on SO):

$filename = "c:\path\to\MyAssembly.dll"
[System.AppDomain]::CurrentDomain.SetData(
    "APP_CONFIG_FILE", $filename + ".config")
$assembly =
    [System.Reflection.Assembly]::LoadFrom($filename)

cmd.net: uniq

June 27, 2011

Finally the command that prompted the creation of cmd.net in the first place: uniq.

Before implementing the command, I had to find out which options the default Linux implementations provided.

I used these pages as guideline:

The resulting command filters unique lines from stdin (or a piped input file) to stdout, and supports these options:

uniq [parameters]

displays unique lines in sorted file

-u      unique lines only
-d      repeated lines only
-c      display line count
-i      ignore case
-f      [ignore fields]         ignore number of fields
-s      [skip characters]       skip number of characters
-w      [compare characters]    compare number of characters
-in     [encoding]      input encoding
-ci     [culture]       culture info
-out    [encoding]      output encoding

uniq 0.10.4117.37457
cmd.net 0.10.0.0 (c) by devio.at 2011

Note that uniq only analyzes subsequent lines of text in the file. Thus it only operates correctly on a pre-sorted file. If the input is not sorted, equal lines may be output more than once (but not subsequently).

The first version of cmd.net is available for download here, and contains the commands described in this post.


SMOscript 0.19

June 26, 2011

There has been a lot of feedback on SMOscript 0.18, and some feature requests are now implemented.

The “script objects” command adds the options “-use” to add a USE [database] command to the generated file, and -fa option to append to existing files.

The “list database” command adds filters for the status of databases: system database, read-only, offline.

Instead of passing the login information using the switches -s, -d, -u, -p, you can now pass a connection string literal (such as used in web.config or app.config) using the -c switch.

New commands  have been added as well:

  • List object dependencies
  • Find string literals
  • Find strings

If an error or exception occurs, the ERRORLEVEL variable will be set (1 for parser errors, 2 to execution errors).

The complete help screen displays like this:

smoscript 0.19.4194.20191 (c) by devio.at 2008-2011

    list and script databases and database objects.

    usage: smoscript [options (leading '-' or '/')] [command]

    connection options:

    -s server       server name
    -d database     database name
    -u username     username (default: integrated authentication)
    -p password     password (if -u is missing, password for sa)
    -c connection   connection string (sets -s, -d, -u, -p)

    scripting options:

    -o [schema.]object  object name

    -r              generate DROP statements
    -i              include IF NOT EXISTS statements
    -use            generate USE [database]

    -f filename     output to file
    -fa             append to file if exists
    -F directory    output to directory

    listing options:

    -l[x]a          list [in]accessible databases
    -l[x]ro         list databases [not] read-only
    -l[x]sys        list [non-] system databases
    -ls             list database status

    dependency options:

    -depth          max. recursion depth
    -dxt            exclude tables in result

    find options:

    -fx string      exclude this string literal (multi)
    -fxs string     exclude strings starting with this literal (multi)
    -fi             find string case-insensitive
    -fc             find string and display code

    output options:

    -A              current ANSI codepage
    -O              ASCII
    -T              Unicode
    -U              UTF8

    commands:

    l           list databases on server (implied by -s)
                list objects in database (implied by -s -d)
    s           script all objects (implied by -s -d -F/-f)
                script single object (implied by -s -d -o)
    db          list database properties
    dep         list object dependencies (-o)
    fs          find string literals
    f string    find string in modules

The latest version of SMOscript is available for download here.


First Quarter Million

June 26, 2011

The first quarter million is the hardest ;)

Thanks for visiting this blog and finding it useful.


Follow

Get every new post delivered to your Inbox.