@rem http://computer-technology.hateblo.jp/entry/20131025/p1
setlocal
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@rem setlocal DISABLEDELAYEDEXPANSION
@rem 説明:
@rem Template of JScript and PowerShell script in Batch file.
@rem Usage:
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "BAT_Output_Args" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "BAT_Output_Hello_Batch" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "BAT_Output_Process_Id" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "BAT_Output_Process_Id2" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "BAT_Output_Version" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "JS_Output_Args" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "JS_Output_Hello_JScript" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "JS_Output_Version" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_Args" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_Hello_PowerShell" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_Parent_Process_Id0"
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_Parent_Process_Id1" "0"
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_Parent_Process_Id2"
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_Version" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_PowerShellScriptLinesRange" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "PS1_Output_PowerShellScriptLinesRange" "Hello JScript and PowerShell script in Batch file.ps1.js.bat" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat"
@rem "Hello JScript and PowerShell script in Batch file.ps1.js.bat" "xxxxxxxx" " 1 " " 22 二弐 " " 333 333 333 "
@rem echo errorlevel=%errorlevel%
@rem ####################
@rem Batch.
@rem ####################
@rem ....................
@rem Batch main.
@rem ....................
set rv=11
if "%~1"=="" ( call :L_call_BAT_NoArgs
) else if "%~1"=="BAT_Output_Args" ( call :L_BAT_Output_Args %*
) else if "%~1"=="BAT_Output_Hello_Batch" ( call :L_BAT_Output_Hello_Batch %*
) else if "%~1"=="BAT_Output_Process_Id" ( call :L_BAT_Output_Process_Id %*
) else if "%~1"=="BAT_Output_Process_Id2" ( call :L_BAT_Output_Process_Id2 %*
) else if "%~1"=="BAT_Output_Version" ( call :L_BAT_Output_Version %*
) else if "%~1"=="JS_Output_Args" ( call :L_call_JS %*
) else if "%~1"=="JS_Output_Hello_JScript" ( call :L_call_JS %*
) else if "%~1"=="JS_Output_Version" ( call :L_call_JS %*
) else if "%~1"=="PS1_Output_Args" ( call :L_call_PS1 %*
) else if "%~1"=="PS1_Output_Hello_PowerShell" ( call :L_call_PS1 %*
) else if "%~1"=="PS1_Output_Parent_Process_Id0" ( call :L_call_PS1 %*
) else if "%~1"=="PS1_Output_Parent_Process_Id1" ( call :L_call_PS1 %*
) else if "%~1"=="PS1_Output_Parent_Process_Id2" ( call :L_call_PS1 %*
) else if "%~1"=="PS1_Output_Version" ( call :L_call_PS1 %*
) else if "%~1"=="PS1_Output_PowerShellScriptLinesRange" ( call :L_call_PS1 %*
) else ( call :L_call_BAT_OtherArgs %*
)
@rem ....................
@rem Exit.
@rem ....................
@rem echo.
@rem pause
@rem echo 終了するには何かキーを押してください . . .
@rem pause>nul
exit /b !rv!
goto :eof
@rem ....................
@rem No args.
@rem ....................
:L_call_BAT_NoArgs
echo BAT %0 %* ....................
call :L_call_JS
call :L_call_PS1
call :L_call_PS1
set /a rv=!errorlevel!+9
goto :eof
@rem ....................
@rem Output Args.
@rem ....................
:L_BAT_Output_Args
echo BAT %0 %* ....................
set /a y=0
for %%z in (
%*
) do (
set /a y=!y!+1
echo BAT %%!y!=%%~z.
)
set /a rv=!errorlevel!+1
goto :eof
@rem ....................
@rem Output Hello Batch.
@rem ....................
:L_BAT_Output_Hello_Batch
echo BAT %0 %* ....................
echo ハロー Batch.
set /a rv=!errorlevel!+2
goto :eof
@rem ....................
@rem Output Process Id.
@rem ....................
:L_BAT_Output_Process_Id
echo BAT %0 %* ....................
for /f "usebackq tokens=1 delims=, " %%z in (
`call "%~f0" "PS1_Output_Parent_Process_Id0" "%~f0"`
) do (
set /a pid1=%%z+0
if !pid1! geq 1 (
set pid=%%z
echo !pid1!
)
)
set /a rv=!errorlevel!+2
goto :eof
@rem ....................
@rem Output Process Id2.
@rem ....................
:L_BAT_Output_Process_Id2
echo BAT %0 %* ....................
for /f "usebackq tokens=1 delims=, " %%z in (
`call "%~f0" "PS1_Output_Parent_Process_Id2"`
) do (
set /a pid1=%%z+0
if !pid1! geq 1 (
set pid=%%z
echo !pid1!
)
)
set /a rv=!errorlevel!+2
goto :eof
@rem ....................
@rem Output Version.
@rem ....................
:L_BAT_Output_Version
echo BAT %0 %* ....................
ver
set /a rv=!errorlevel!+3
goto :eof
@rem ....................
@rem Other args.
@rem ....................
:L_call_BAT_OtherArgs
echo BAT %0 %* ....................
call :L_call_JS %*
call :L_call_PS1 %*
set /a rv=!errorlevel!+8
goto :eof
@rem ....................
@rem Echo ErrorLevel.
@rem ....................
:L_echoErrorLevel
@rem echo BAT L_echoErrorLevel ....................
echo BAT %0 R errorlevel=!errorlevel!
@rem echo.
goto :eof
@rem ....................
@rem Call JScript.
@rem ....................
:L_call_JS
echo BAT %0 %* ....................
set "jsargs=%*"
if "!jsargs!"=="" (
cscript.exe //nologo //e:JScript "%~f0"
) else (
cscript.exe //nologo //e:JScript "%~f0" !jsargs!
)
call :L_echoErrorLevel
goto :eof
@rem ....................
@rem Get PowerShellScriptLinesRange.
@rem ....................
:L_getPowerShellScriptLinesRange
echo BAT %0 %* ....................
for /f "usebackq tokens=1,2 delims=, " %%y in (
`call "%~f0" "PS1_Output_PowerShellScriptLinesRange" "%~f0"`
) do (
set /a ps1Sta1=%%y+0 2>nul
set /a ps1End1=%%z+0 2>nul
if "!ps1Sta1!,!ps1End1!"=="%%y,%%z" (
set "ps1Sta=%%y"
set "ps1End=%%z"
)
)
goto :eof
@rem ....................
@rem Call PowerShell script.
@rem ....................
:L_call_PS1
@rem http://qiita.com/cd01/items/82829ba0ec0f59e1b04d
@rem http://scripting.cocolog-nifty.com/blog/2007/07/powershellps1_bd41.html
echo BAT %0 %* ....................
set "ps1args=%*"
set "ps1line=$ps1sb = (\"\");"
set "ps1line=!ps1line!$ps1obj = (Get-Content \"%~f0\");"
if "!ps1Sta!,!ps1End!"=="," (
if "%~1"=="PS1_Output_PowerShellScriptLinesRange" (
set "ps1line=!ps1line!$ps1Sta = ($ps1obj | Select-String (\"^goto \:eof$\") | Select-Object -Last 1).LineNumber + 1;"
set "ps1line=!ps1line!$ps1End = ($ps1obj | Select-String (\"^@\" + \"end$\") | Select-Object -First 1).LineNumber - 1;"
set "ps1line=!ps1line!if ($ps1End -eq -1) {$ps1End = $ps1obj.Length};"
) else (
call :L_getPowerShellScriptLinesRange
set "ps1line=!ps1line!$ps1Sta = !ps1Sta!;"
set "ps1line=!ps1line!$ps1End = !ps1End!;"
)
) else (
set "ps1line=!ps1line!$ps1Sta = !ps1Sta!;"
set "ps1line=!ps1line!$ps1End = !ps1End!;"
)
set "ps1line=!ps1line!$ps1sb += for ($cc = 0; $cc -lt $ps1Sta; ++$cc) {$ps1sb += \"`n\"} <# Dummy blank lines for line number in PowerShell message. #>;"
set "ps1line=!ps1line!$ps1sb += ($ps1obj | Select-Object -First $ps1End | Select-Object -Skip $ps1Sta) -Join \"`n\";"
set "ps1line=!ps1line!$ps1sb = [scriptblock]::create($ps1sb);"
set "ps1line=!ps1line!&$ps1sb"
if "!ps1args!"=="" (
powershell -NoProfile -ExecutionPolicy Unrestricted "!ps1line!"
) else (
set "ps1args=!ps1args:"='!"
rem '
powershell -NoProfile -ExecutionPolicy Unrestricted "!ps1line!" "!ps1args!"
)
call :L_echoErrorLevel
goto :eof
# ####################
# PowerShell script.
# ####################
# ....................
# No args.
# ....................
function global:NoArgs {
Write-Host "PS1 NoArgs ...................."
# Write-Host "PS1 Argc=$($Args.Length)."
Write-Host "PS1 There are no arguments."
return 9
}
# ....................
# Output Args.
# ....................
function global:Output_Args([string[]]$ps1args) {
Write-Host "PS1 Output_Args ...................."
Write-Host "PS1 [$($ps1args.Length)]$($ps1args)."
for($idx = 0; $idx -lt $ps1args.Length; ++$idx) {
Write-Host "PS1 Args[$($idx)]=$($ps1args[$idx])."
}
return 1
}
# ....................
# Output Hello PowerShell.
# ....................
function global:Output_Hello-PowerShell {
Write-Host "PS1 Output_Hello-PowerShell ...................."
Write-Host "PS1 ハロー, PowerShell World!!"
return 2
}
# ....................
# Output Parent process id 0.
# ....................
function global:Output_Parent_Process_Id0 {
Write-Host "PS1 Output_Parent_Process_Id0 ...................."
Write-Host (Get-WmiObject Win32_Process -Filter ProcessId=$pid | Foreach-Object {$_.ParentProcessId;})
return 2
}
# ....................
# Output Parent process id 1.
# ....................
function global:Output_Parent_Process_Id1([string[]]$ps1args) {
Write-Host "PS1 Output_Parent_Process_Id1 ....................$($ps1args[0])"
Write-Host (Get-WmiObject Win32_Process -Filter ProcessId=$($ps1args[0]) | Foreach-Object {$_.ParentProcessId;})
return 2
}
# ....................
# Output Parent process id 2.
# ....................
function global:Output_Parent_Process_Id2 {
Write-Host "PS1 Output_Parent_Process_Id2 ...................."
$pid1 = (Get-WmiObject Win32_Process -Filter ProcessId=$pid | Foreach-Object {$_.ParentProcessId;})
Write-Host (Get-WmiObject Win32_Process -Filter ProcessId=$pid1 | Foreach-Object {$_.ParentProcessId;})
return 2
}
# ....................
# Output Version.
# ....................
function global:Output_Version {
Write-Host "PS1 Output_Version ...................."
Write-Host "PS1 PSVersionTable.PSVersion=$($PSVersionTable.PSVersion)"
Write-Host "PS1 HOST.Version=$($HOST.Version)"
return 3
}
# ....................
# Other args.
# ....................
function global:OtherArgs([string[]]$ps1args) {
Write-Host "PS1 OtherArgs ...................."
Write-Host "PS1 [$($ps1args.Length)]$($ps1args)."
for($idx = 0; $idx -lt $ps1args.Length; ++$idx) {
Write-Host "PS1 Args[$($idx)]=$($ps1args[$idx])."
}
return 8
}
# ....................
# Output PowerShellScriptLinesRange.
# ....................
function global:Output_PowerShellScriptLinesRange([string]$pathPowerShellScript) {
$ps1Sta = 0
$ps1End = 0
if (Test-Path $pathPowerShellScript) {
$ps1obj = Get-Content $pathPowerShellScript
$ps1Sta = ($ps1obj | Select-String ("^goto \:eof$") | Select-Object -Last 1).LineNumber + 1
$ps1End = ($ps1obj | Select-String ("^@" + "end$") | Select-Object -First 1).LineNumber - 1
if ($ps1End -eq -1) {$ps1End = $ps1obj.Length}
}
Write-Host "$ps1Sta,$ps1End"
return 7
}
# --------------------
# PowerShell script main.
# --------------------
$rv = 20
if ($Args.Length -eq 0) {
$rv = $rv + (NoArgs)
} else {
if (0 -eq 1) {
$rv = $rv + 1
} elseif (($Args.Length -ge 1) -and ($Args[0].Equals("PS1_Output_Args"))) {
$ary = $Args[1 .. ($Args.length - 1)]
$rv = $rv + (Output_Args ($ary))
} elseif ($Args[0].Equals("PS1_Output_Hello_PowerShell")) {
$rv = $rv + (Output_Hello-PowerShell)
} elseif (($Args.Length -ge 1) -and ($Args[0].Equals("PS1_Output_Parent_Process_Id0"))) {
$rv = $rv + (Output_Parent_Process_Id0)
} elseif (($Args.Length -ge 2) -and ($Args[0].Equals("PS1_Output_Parent_Process_Id1"))) {
$ary = $Args[1 .. ($Args.length - 1)]
$rv = $rv + (Output_Parent_Process_Id1 ($ary))
} elseif (($Args.Length -ge 1) -and ($Args[0].Equals("PS1_Output_Parent_Process_Id2"))) {
$rv = $rv + (Output_Parent_Process_Id2)
} elseif ($Args[0].Equals("PS1_Output_Version")) {
$rv = $rv + (Output_Version)
} elseif (($Args.Length -ge 1) -and ($Args[0].Equals("PS1_Output_PowerShellScriptLinesRange"))) {
$rv = $rv + (Output_PowerShellScriptLinesRange ($Args[1]))
} else {
$rv = $rv + (OtherArgs ($Args))
}
}
exit $rv
@end
// ####################
// JScript.
// ####################
// ....................
// No args.
// ....................
function NoArgs() {
WScript.Echo("JS NoArgs ....................");
// WScript.Echo("JS Argc=" + WScript.Arguments.length + ".");
WScript.Echo("JS There are no arguments.");
return 9;
}
// ....................
// Output Args.
// ....................
function Output_Args(jsargs) {
WScript.Echo("JS Output_Args ....................");
WScript.Echo("JS [" + jsargs.length + "]" + jsargs + ".");
for(var idx = 0; idx < jsargs.length; ++idx) {
WScript.Echo("JS Args[" + idx + "]=" + jsargs[idx] + ".");
}
return 1;
}
// ....................
// Output Hello JScript.
// ....................
function Output_Hello_JScript() {
WScript.Echo("JS Output_Hello_JScript ....................");
WScript.Echo("JS ハロー, JScript World!!");
return 2;
}
// ....................
// Output Version.
// ....................
function Output_Version() {
WScript.Echo("JS Output_Version ....................");
WScript.Echo("JS ScriptEngineMajorVersion()=" + ScriptEngineMajorVersion());
WScript.Echo("JS @_jscript_version=" + @_jscript_version);
return 3;
}
// ....................
// Other args.
// ....................
function OtherArgs(jsargs) {
WScript.Echo("JS OtherArgs ....................");
WScript.Echo("JS [" + jsargs.length + "]" + jsargs + ".");
for(var idx = 0; idx < jsargs.length; ++idx) {
WScript.Echo("JS Args[" + idx + "]=" + jsargs[idx] + ".");
}
return 8;
}
// --------------------
// JScript main.
// --------------------
var rv = 30;
if (WScript.Arguments.length == 0) {
rv = rv + NoArgs();
} else {
var args = new Array();
for (var idx = 0; idx < WScript.Arguments.length; ++idx) {
args[idx] = WScript.Arguments(idx);
}
var ary = new Array();
ary = args.slice(0, args.length);
if (0 == 1) {
rv = rv + 1;
} else if ((WScript.Arguments.length >= 1) && (WScript.Arguments(0) == "JS_Output_Args")) {
ary = args.slice(1, args.length);
rv = rv + Output_Args(ary);
} else if (WScript.Arguments(0) == "JS_Output_Hello_JScript") {
rv = rv + Output_Hello_JScript();
} else if (WScript.Arguments(0) == "JS_Output_Version") {
rv = rv + Output_Version();
} else {
rv = rv + OtherArgs(ary);
}
}
WScript.quit(rv);