goto - Dave's Blog

Search
My timeline on Mastodon

PowerShell Script Batch File Wrapper

2011 May 22, 7:20

I'm trying to learn and use PowerShell more, but plenty of other folks I know don't use PowerShell. To allow them to use my scripts I use the following cmd.exe batch file to make it easy to call PowerShell scripts. To use, just name the batch file name the same as the corresponding PowerShell script filename and put it in the same directory.

@echo off
if "%1"=="/?" goto help
if "%1"=="/h" goto help
if "%1"=="-?" goto help
if "%1"=="-h" goto help

%systemroot%\system32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -Command . %~dpn0.ps1 %*
goto end

:help
%systemroot%\system32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -Command help %~dpn0.ps1 -full
goto end

:end

Additionally for PowerShell scripts that modify the current working directory I use the following batch file:

@echo off
if "%1"=="/?" goto help
if "%1"=="/h" goto help
if "%1"=="-?" goto help
if "%1"=="-h" goto help

%systemroot%\system32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -Command . %~dpn0.ps1 %*;(pwd).Path 1> %temp%\%~n0.tmp 2> nul
set /p newdir=
PermalinkCommentspowershell technical programming batch file console

INTERCAL -- the Language From Hell

2010 May 10, 5:21An old article by Charles Stross on INTERCAL the satirical programming language. It contains great features such as 'come from' the inverse of 'goto'.PermalinkCommentsc programming humor technical language software charles-stross intercal goto

Two-for Script File

2007 Aug 6, 5:40I was messing with the XSLT to XSL Converter source which is a javascript file that can be run with cscript.exe. I've changed it to be like a very basic version of xsltproc that simply runs an XML file through an XSLT. I also wanted to run this from the command prompt without writing "cscript ..." everytime. I decided to make like perl programmers I've seen and make a JS file that works as a batch file and a JS file at the same time.

Here's a basic version of what I ended doing applied to a 'hello world' script named helloworld.cmd:
/* 2> NUL
@echo off
cscript /e:javascript /nologo "%~f0" %*
@goto :eof

    Hello World
        Says 'Hello world.' when you run it.
*/

var outText = 'Hello world.';
WScript.Echo(outText);
Running this on a command prompt gives the following:
C:\Users\davris>helloworld

C:\Users\davris>/*  2>NUL
Hello world.
However, after a little more experimentation I found this was slightly overkill for my purposes since if I rename the file to helloworld.js and just type its name like a command it is run by cscript:
C:\Users\davris>helloworld
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Hello world.
So this time I didn't need all that but if ever in the future I need to run a batch file then a JS file I can do it with one file...PermalinkCommentscmd js technical cscript batch xslt xsl javascript

Language Log: Considered harmful

2007 Jul 3, 9:25The Language Log folk explain where the 'x Considered Harmful' snowclone came from. Spoiler: Its not Dijkstra.PermalinkCommentsconsidered-harmful goto dijkstra etymology language snowclone

COMEFROM - Wikipedia, the free encyclopedia

2007 Jul 3, 9:22The complement of goto is comefrom. Pretty funny.PermalinkCommentshumor programming comefrom goto

Go To Statement Considered Harmful

2006 Dec 7, 1:15Dijkstra's famous anti-goto paper. I agree with him. Due to goto in BASIC through 3rd and 4th grade I thought goto was a word.PermalinkCommentsarticle history humor language goto code programming dijkstra acm read
Older Entries Creative Commons License Some rights reserved.