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)
Open a command prompt as administrator, and copy the file to PowerShell’s directory
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"
"APP_CONFIG_FILE", $filename + ".config")
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:
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
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.
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:
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]
-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)
-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
-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
-depth max. recursion depth
-dxt exclude tables in result
-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
-A current ANSI codepage
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.