The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Archive for the ‘CommandLine’ Category

Portable PowerShell – ShellTools

Posted by jpluimers on 2011/08/01

As a contractor, it is far easier to have your tools with you on a USB stick, than to depend on software distribution mechanisms or installation policies at clients.

Hence I like the Portable PowerShell from ShellTools: a way to run (side-by-side!) various versions of PowerShell from a USB stick.

–jeroen

Posted in CommandLine, Power User, PowerShell | Leave a Comment »

Formatted sourcecode in WordPress: uses SyntaxHighlighter 3.0; complete list of supported languages

Posted by jpluimers on 2011/01/18

In the past I wrote a few blog posts on posting sourcecode in WordPress.

Nick Hodges‘ last Flotsam and Jetsam blog post pointed me to the SyntaxHighlighter JavaScript that is used by WordPress and many other engines/sites.

Their site contains an even more elaborate list of supported languages.

I had the basic list right in my last post, but was missing all the aliases (which often are easier than the longer proper names).

This is the new table adapted from their list: Read the rest of this entry »

Posted in .NET, Batch-Files, C#, CSS, Database Development, Delphi, Delphi for PHP, Development, HTML, HTML5, Java, PowerShell, RegEx, Scripting, SQL, VBS, Web Development, WordPress, XML, XML/XSD, XSD | 5 Comments »

PowerShell: the 2 most common error messages for starters

Posted by jpluimers on 2010/11/11

I’ve waited for PowerShell to become pretty mature before diving into it:
Version 2 has been out for a year, no service packs have been needed, so time to get into it.

PowerShell code can be stored in scripts.
The convention is to use the .ps1 extension for that (even when running PowerShell 2.0. Lesson learned: don’t put version numbers in file extensions).

Having lots of scripting and programming experience, I was a PowerShell beginner, and similar people usually bump into a few error messages.

Luckily, lots of people have gone through that phase, so there is lots of help on the internet.
So this post is not only to show you about some common things you bump into when starting with PowerShell, but also about the power of the internet as a collective pool of knowledge.

This was the first error message I bumped into:

C:\bin>powershell first-script.ps1
The term ‘first-script.ps1’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:16
+ first-script.ps1 <<<<

A message by mosoto (Marcel J. Ortiz Soto) answered this as one of the first in the search results for “is not recognized as a cmdlet, function, operable program, or script file”.
The reason for this error is that unlike cmd.exe (but similar to unix shells), PowerShell does not include the current working directory (.) in the search path.

Solution: prepend the directory for your script file, in this case by prefixing it with “.\”.

This immediately leads to PowerShell rookie error message 2:

C:\bin>powershell .\first-script.ps1
File C:\bin\first-script.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
At line:1 char:18
+ .\first-script.ps1 <<<<

Even though searching for “cannot be loaded because the execution of scripts is disabled on this system” quickly reveals a few posts describing the cause, the error message gives a hint: get-help about_signing.

Executing this on the command-prompt gets youa lot of help about signing:

c:\bin>PowerShell get-help about_signing

This is the summary:

  • PowerShell scripts can be signed.
  • PowerShell as a global execution policy defaulting to Restricted (see below).
  • By default, PowerShell does not want to run any scripts at all.
  • You can assign these values to the execution policy: Restricted, AllSigned, RemoteSigned and Unrestricted
    (note if not specified, the policy is global, so changing this changes it for your whole system, so better define the scope)

Documentation is at:

If you set the execution policy in all scopes to Undefined and the Group Policy is not set, the default execution policy, Restricted, is effective for all users of the computer.

This is how you ask for the current policy:

C:\bin>powershell Get-ExecutionPolicy

This is how you set the current policy to only require remote scripts to be signed:

C:\bin>powershell Set-ExecutionPolicy RemoteSigned

Now you can run unsigned local scripts.
Beware: if anyone sends you a virus in an unsigned PowerShell script, that can now be executed too!

Hope this helps a few other PowerShell rookies too :-)

–jeroen

Posted in CommandLine, Conference Topics, Conferences, Development, Event, PowerShell, Software Development | 2 Comments »

Twitter / Danny Thorpe: New programmers are drawn …

Posted by jpluimers on 2010/10/21

Brilliant and so true:

New programmers are drawn to multithreading like moths to flame, with similar results.

Edit: And this is just one example on how tiny little details can impact threading so much.

–jeroen

via Twitter / Danny Thorpe: New programmers are drawn ….

Posted in .NET, Agile, CommandLine, Debugging, Delphi, Development, Opinions, Software Development | 10 Comments »

TFS: using tf to move files

Posted by jpluimers on 2010/09/30

Recently, I had a “server binding” “status=invalid” in TFS when moving quite a bit of files from CVS to TFS.
The most problematic where getting the solution, and a webservice project correctly.

The FAQ on solution binding indicated I should fix my workspace (which is simple: just a root directory), so that was not of much help.

I tried various other things, but in the end, the easiest fix was to:

  1. rename the directories of the bad solution and project,
  2. create an empty solution
  3. create an empty ASP.NET web site project
  4. then move all of the old files to the new directory structure

Moving files around from within Visual Studio is a tedious process: you can only move one file at a time.

TF to the rescue: it allows you to call perform TFS tasks from the commandline.

Time to automate that with a little batch file. Read the rest of this entry »

Posted in CommandLine, Development, Source Code Management, TFS (Team Foundation System) | Leave a Comment »

SQL Server 2008: restoring backup of previous SQL Server version

Posted by jpluimers on 2010/09/21

When restoring a backup made in SQL Server 2005 to a SQL Server 2008 environment, you don’t see the underlying upgrade of the on-disk physical version in the UI (you only see the percentage completion moving from 0% to 100%).

When performing the same on the command-line you can actually follow the physical version gets updated:

C:\SCRIPT>"%sqlcmd%" -S . -E -Q "RESTORE DATABASE [MyDBA] FROM DISK = N'C:\BACKUP\MyDB.bak' WITH REPLACE, STATS = 10, MOVE N'MyDB' to N'C:\DATA\MyDB.mdf', MOVE N'MyDB_log' to N'C:\DATA\MyDB_log.ldf' ;"
19 percent processed.
29 percent processed.
39 percent processed.
49 percent processed.
59 percent processed.
69 percent processed.
78 percent processed.
88 percent processed.
98 percent processed.
100 percent processed.
Processed 1296 pages for database 'MyDBA', file 'MyDB' on file 1.
Processed 2 pages for database 'MyDBA', file 'MyDB_log' on file 1.
Converting database 'MyDBA' from version 611 to the current version 655.
Database 'MyDBA' running the upgrade step from version 611 to version 621.
Database 'MyDBA' running the upgrade step from version 621 to version 622.
Database 'MyDBA' running the upgrade step from version 622 to version 625.
Database 'MyDBA' running the upgrade step from version 625 to version 626.
Database 'MyDBA' running the upgrade step from version 626 to version 627.
Database 'MyDBA' running the upgrade step from version 627 to version 628.
Database 'MyDBA' running the upgrade step from version 628 to version 629.
Database 'MyDBA' running the upgrade step from version 629 to version 630.
Database 'MyDBA' running the upgrade step from version 630 to version 631.
Database 'MyDBA' running the upgrade step from version 631 to version 632.
Database 'MyDBA' running the upgrade step from version 632 to version 633.
Database 'MyDBA' running the upgrade step from version 633 to version 634.
Database 'MyDBA' running the upgrade step from version 634 to version 635.
Database 'MyDBA' running the upgrade step from version 635 to version 636.
Database 'MyDBA' running the upgrade step from version 636 to version 637.
Database 'MyDBA' running the upgrade step from version 637 to version 638.
Database 'MyDBA' running the upgrade step from version 638 to version 639.
Database 'MyDBA' running the upgrade step from version 639 to version 640.
Database 'MyDBA' running the upgrade step from version 640 to version 641.
Database 'MyDBA' running the upgrade step from version 641 to version 642.
Database 'MyDBA' running the upgrade step from version 642 to version 643.
Database 'MyDBA' running the upgrade step from version 643 to version 644.
Database 'MyDBA' running the upgrade step from version 644 to version 645.
Database 'MyDBA' running the upgrade step from version 645 to version 646.
Database 'MyDBA' running the upgrade step from version 646 to version 647.
Database 'MyDBA' running the upgrade step from version 647 to version 648.
Database 'MyDBA' running the upgrade step from version 648 to version 649.
Database 'MyDBA' running the upgrade step from version 649 to version 650.
Database 'MyDBA' running the upgrade step from version 650 to version 651.
Database 'MyDBA' running the upgrade step from version 651 to version 652.
Database 'MyDBA' running the upgrade step from version 652 to version 653.
Database 'MyDBA' running the upgrade step from version 653 to version 654.
Database 'MyDBA' running the upgrade step from version 654 to version 655.
RESTORE DATABASE successfully processed 1298 pages in 0.738 seconds (13.736 MB/sec).

Ain’t command-lines cool :-)

A few notes on version numbers:

655 – 2008
661 – 2008 R2

http://www.sqlskills.com/BLOGS/PAUL/post/Search-Engine-QA-13-Difference-between-database-version-and-database-compatibility-level.aspx
http://www.sqlskills.com/BLOGS/PAUL/post/SQL-Server-2008-R2-bumps-the-database-version.aspx
http://serverfault.com/questions/58039/sql-server-2008-2008r2-backup-compatibility
http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/f8f8db38-3c20-45e4-ae8b-4cc097eb7f0d

–jeroen

Posted in CommandLine, Database Development, Development, Software Development, SQL Server | Leave a Comment »

Batch file trickery: finding the first file from a list in a certain directory

Posted by jpluimers on 2010/09/14

Using the Windows command shell, I recently needed a batch file that operated on the first occurrence of a file in a certain directory, where the filename was dynamically obtained from a list.

Wait: that sounds too complex; lets make this clear with an example. Read the rest of this entry »

Posted in CommandLine, Development, Power User, Software Development | Leave a Comment »

Script to Restart VMware Tools Remotely | Daily Hypervisor

Posted by jpluimers on 2010/08/30

Sometimes the VMware tools need to be restarted in order to get the shared clipboard functionality working again.

From inside a Windows VM, you can do that like this:

net stop vmtools

This will create this output:

The VMware Tools Service service is stopping.

The VMware Tools Service service was stopped successfully.

Then start the service again:

net start vmtools

Giving this output:

The VMware Tools Service service is starting.

The VMware Tools Service service was started successfully.

From outside your virtual machine, you can do it like on this URL using schttp://www.dailyhypervisor.com/2009/03/25/script-restarting-vmware-tools-remotely/

–jeroen

Posted in Batch-Files, CommandLine, Development, Scripting, Software Development | Leave a Comment »

SQL Sever: batch files to find instances and sqlcmd.exe/osql.exe

Posted by jpluimers on 2010/08/19

In my VM’s, I often run different instances and/or versions of SQL Server.

Finding the right instance of SQL server, and the right version of SQLCMD.EXE / OSQL.EXE can be a pain.

That’s why I have written the two batch-files shown below for that.
They are not perfect, but they do work for me, and show a few interesting batch-file tricks.

As for preferring SQLCMD: [WayBack] sql server – What are the differences between osql, isql, and sqlcmd? – Stack Overflow

find-sql-sqlcmd.bat

This finds the most up-to-date SQLCMD.EXE (or OSQL.EXE for SQL Server 2000) and puts the location of it in the sqlcmd environment variable.

@echo off
rem find the highest version of SQLCMD.EXE or OSQL.EXE and put it in the sqlcmd environment variable
rem this prefers SQLCMD.EXE over OSQL.EXE

set sqlcmd=

for %%d in ("%ProgramFiles%", "%ProgramFiles(x86)%") do for %%v in (80, 90, 100) do for %%f in (OSQL, SQLCMD) do (
call :sqlcmdtest "%%~d\Microsoft SQL Server\%%v\Tools\Binn\%%f.EXE" %1
)

if !!==!%sqlcmd%! for %%f in (OSQL, SQLCMD) do (
call :find-on-path %%f.EXE
)

if !%1!==!! echo SQLCMD: %sqlcmd%
goto :exit

rem be carefull not to specify the .EXE in the %%f FOR loop-expression, otherwise it tries to dine SQLCMD.EXE and OQSL.EXE in the current directory

rem http://msdn.microsoft.com/en-us/library/ms178653.aspx
rem 80 = SQL Server 2000
rem 90 = SQL Server 2005
rem 100 = SQL Server 2008 and 2008 R2

:find-on-path
set sqlcmd=%~f$PATH:1
if not ""=="%sqlcmd%" set sqlcmd="%sqlcmd%"
goto :exit

:sqlcmdtest
if exist %1 if !%2!==!! echo found %1
if exist %1 set sqlcmd=%1

:exit

Tricks used:

  1. Clearing an environment varible by asigning an empty string to it
    (set sqlcmd=)
  2. Nested for loops
    Note that when working with string literals in for loops, you should not put any file extension on it (if you do, the for loop will only match filenames).
    So: In stead of having (OSQL.EXE, SQLCMD.EXE), you see (OSQL, SQLCMD) in the for loop, and the .EXE is concatenated later on.
  3. Splitting the body of the for loop over multiple lines using parenthesis ().
  4. Put the value you prefer at the end of the for loop
    (so the last value that is found will be put in the sqlcmd environment variable)
  5. The mapping of SQL Server verions to numbers used in the directories (see also the documentation of the SQL Server sp_dbcmptleve stored procedure):
    80 = SQL Server 2000
    90 = SQL Server 2005
    100 = SQL Server 2008

find-sql-instance.bat

This batch file finds the SQL Server instances on the local machines from the naming of the SQL Server services that are running.
Note that it won’t work if you choose custom names for your SQL Server services (but that will probably break a lot of other stuff out there as well).

@echo off
rem find best matching instance of SQL Server on this machine
set sqlinstance=
set sqlservice=
for /f "usebackq tokens=1,2,3 delims=:$" %%i in (`sc query`) do (
rem %%j is " MSSQL" because we dropped space as a delimiter
if "%%i"=="SERVICE_NAME" call :bare-service %%j %%k
)
if !%1!==!! echo SQL Instance=%sqlinstance%
if !%1!==!! echo SQL Service=%sqlservice%
goto :exit

:bare-service
rem %1 equals "MSSQL" because of the command-line parsing trims spaces
rem the order is important: we favour named instances over unnamed:
if "%1"=="MSSQLSERVER" call :process-instance %1 .
if "%1"=="MSSQL" call :process-instance MSSQL$%2 .\%2
goto :exit

:process-instance
if !%1!==!! echo found service "%1" providing instance "%2"
if "%sqlinstance%"=="" set sqlinstance=%2& set sqlservice=%1
for /f "usebackq tokens=1,2,3,4" %%s in (`sc query %1`) do (
if "%%s"=="STATE" if !%1!==!! echo state of %1 is %%v
)
goto :exit

:exit

Tricks used:

  1. use the for /f command to parse the output of sc query;
    note the use of the usebackq, tokens and delims parameters in this command,
    and the use of `backquotes` to parse the output of `sc query` which outputs fragments like shown below.
  2. Use the ampersand (&) to run two commands on one line
    (the set sqlinstance=%2 and set sqlservice=%1).
  3. the sc query command, which queries the Service Controller for the configured services
  4. The use of call :bare-service to call a subroutine at label :bare-service inside a for loop
  5. The fact that a leading space in for loop variable %%j is trimmed when calling the :bare-service label.

sc query sample fragment:

SERVICE_NAME: MSSQL$SQLEXPRESS
DISPLAY_NAME: SQL Server (SQLEXPRESS)
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0	(0x0)
        SERVICE_EXIT_CODE  : 0	(0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

SQLRUN – tying it together

My final batch-file ties both together:

set sqlrun=
call %~dp0sql-find-sqlcmd.bat %*
call %~dp0sql-find-instance.bat %*
if not !!==!%sqlcmd%! if not !!==!%sqlinstance%! set sqlrun=%sqlcmd% -S %sqlinstance% -E
if !%1!==!! echo SQLRUN=%sqlrun%

The output of that batch-file is like this:

found "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\OSQL.EXE"
found "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE"
found "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE"
found "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
SQLCMD: "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
found ".\SQLEXPRESS"
state of service MSSQL$SQLEXPRESS with SQL Server instance .\SQLEXPRESS is RUNNING
found "MSSQLSERVER" (.)
state of service MSSQLSERVER with SQL Server instance . is RUNNING
SQL Instance=.\SQLEXPRESS
SQLRUN="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -S .\SQLEXPRESS -E

Hope this helps a few people.

–jeroen

Posted in CommandLine, Database Development, Development, Software Development, SQL Server | 8 Comments »

Deploying .EXE on a server share when it is in use by workstations (using Handles.exe and for)

Posted by jpluimers on 2010/06/15

In a lot of network environments, you (still ) find a situation where workstations run a .EXE from a network share.

In these situations, you cannot update the .EXE on the server: you get “Access Denied” errors.

Below is a simple batch-file trick to overcome this.
It kills all the handles that the workstations have to the .EXE
After you have run this batch-file, you can copy the .EXE.

Note:
There still is a small race condition: if between you kill the handles and copying the new .EXE, someone starts the old .EXE Read the rest of this entry »

Posted in CommandLine, Development, Power User, Software Development | Leave a Comment »