PowerShellによるバッチ処理を学習中です.以下の現象で困っています.
モジュールの宣言
メインの.ps1に以下のように書いております.
# Import
Import-Module "$($PSScriptRoot)\Module"
ModuleフォルダにはModule.psm1を配置し中に以下のような関数を書いています.
動いてくれる関数 (英語版SOの見よう見まねです)
# Invoke command specifying command path and arguments.
Function Invoke-Command ($commandPath, $commandArguments)
{
Try {
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $commandPath
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $commandArguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
[pscustomobject]@{
StdOut = $p.StandardOutput.ReadToEnd()
StdErr = $p.StandardError.ReadToEnd()
ExitCode = $p.ExitCode
}
}
Catch {
exit
}
}
こんな感じで使っています(あくまで該当箇所の抜粋)
# Saxon path(実行環境により修正する)
$saxon_path = 'D:\My_Documents/Java/SaxonPE10-6J/saxon-pe-10.6.jar'
# Java path(実行環境により修正する)
$java_path = 'C:/PROGRA~1/Java/jre1.8.0_261/bin'
$java_exec = $java_path + '/' + 'java.exe'
# ...
# Java command option & Saxon option/parameter
$java_option = ' -Xmx1024m '
$java_cmd_option = $java_option + " -jar " + $saxon_path + ' -xsl:' + $xsl_path + ' -it:' + $initial_template + ' -o:' + $out_bind_xml_path + ' PRM_XML_PATH=' + $data_xml_path
Write-Host "Start generating bind xml file from directory:'$data_xml_path' to file:'$out_bind_xml_path'" -ForegroundColor Cyan -BackgroundColor Black;
$start_date_time = Get-Date
$ret = Invoke-Command -commandPath $java_exec -commandArguments $java_cmd_option
Write-Host $ret.StdOut
Write-Host $ret.StdErr
Write-Host 'XSLT processing return code:' $ret.ExitCode -ForegroundColor Cyan -BackgroundColor Black
動作ログ(該当箇所のみ)
Start generating bind xml file from directory:'D:/My_Documents/XML2020/XXXXXX/docs/20211104-xml-php-data/xml-xxxxx-2021-08-yy-zz' to file:'C:/Users/toshi/AppData/Local/Temp/tmp214A.tmp'
XSLT processing return code: 0
Processing takes 0 minutes 2 seconds.
動いてくれない関数 (こちらも英語版SOの見よう見まねです)
Function Invoke-Executable {
# from https://stackoverflow.com/a/24371479/52277
# Runs the specified executable and captures its exit code, stdout
# and stderr.
# Returns: custom object.
# from http://www.codeducky.org/process-handling-net/ added timeout, using tasks
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$commandPath,
[Parameter(Mandatory=$false)]
[String]$commandArguments,
# [Parameter(Mandatory=$false)]
# [String]$sVerb,
[Parameter(Mandatory=$false)]
[Int]$TimeoutMilliseconds=1800000 #30min
)
# Write-Host $commandPath $commandArguments
# Setting process invocation parameters.
$oPsi = New-Object -TypeName System.Diagnostics.ProcessStartInfo
$oPsi.CreateNoWindow = $true
$oPsi.UseShellExecute = $false
$oPsi.RedirectStandardOutput = $true
$oPsi.RedirectStandardError = $true
$oPsi.FileName = $commandPath
if (! [String]::IsNullOrEmpty($commandArguments)) {
$oPsi.Arguments = $commandArguments
}
# if (! [String]::IsNullOrEmpty($sVerb)) {
# $oPsi.Verb = $sVerb
# }
# Creating process object.
$oProcess = New-Object -TypeName System.Diagnostics.Process
$oProcess.StartInfo = $oPsi
# Starting process.
[Void]$oProcess.Start()
# Tasks used based on http://www.codeducky.org/process-handling-net/
$outTask = $oProcess.StandardOutput.ReadToEndAsync();
$errTask = $oProcess.StandardError.ReadToEndAsync();
$bRet=$oProcess.WaitForExit($TimeoutMilliseconds)
if (-Not $bRet)
{
$oProcess.Kill();
# throw [System.TimeoutException] ($commandPath + " was killed due to timeout after " + ($TimeoutMilliseconds/1000) + " sec ")
}
$outText = $outTask.Result;
$errText = $errTask.Result;
if (-Not $bRet)
{
$errText =$errText + ($commandPath + " was killed due to timeout after " + ($TimeoutMilliseconds/1000) + " sec ")
}
$oResult = New-Object -TypeName PSObject -Property ([Ordered]@{
# "commandPath" = $commandPath;
# "Args" = $cArgs -join " ";
"ExitCode" = $oProcess.ExitCode;
"StdOut" = $outText;
"StdErr" = $errText
})
return $oResult
}
この関数はVSCodeのPowerShellで以下のエラーになってしまいます.
Start generating DITA topic path:'C:/Users/toshi/AppData/Local/Temp/e5a353ac-226e-4765-b23d-5d1e2bade516/topic'
Invoke-Executable: D:\My_Documents\XML2020\XXXXXX\docs\xml-php\build-tmc-xml-to-dita.ps1:129:8
Line |
129 | $ret = Invoke-Executable -commandPath $java_exec -commandArguments $j …
| ~~~~~~~~~~~~~~~~~
| The term 'Invoke-Executable' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct
| and try again.
このエラーは以下のような呼び出し記述の中で検出されています.
# Saxon path, Java pathは上記の使いまわしです.
# Java command option & Saxon option/parameter
$java_option = ' -Xmx1024m '
$java_cmd_option = $java_option + " -jar " + $saxon_path + ' -t -xsl:' + $xsl_path + ' -s:' + $out_map_path + ' -o:' + $out_xml_path + ' -ea:on' + ' PRM_BIND_XML_URL=' + $bind_xml_url + ' PRM_OUTPUT_DIR_URL=' + $out_topic_dir_url
Write-Host "Start generating DITA topic path:'$out_topic_dir'" -ForegroundColor Cyan -BackgroundColor Black;
$start_date_time3 = Get-Date
$ret = Invoke-Executable -commandPath $java_exec -commandArguments $java_cmd_option
Write-Host $ret.StdOut
Write-Host $ret.StdErr
Write-Host 'XSLT processing return code:' $ret.ExitCode -ForegroundColor Cyan -BackgroundColor Black
ともかくモジュールも関数も初めて作ったので勝手がわかりません.
原因わかりましたらご教示ください.
VSCodeのPowerShellのPSVersionTableの情報は以下の通りです.
PS D:\My_Documents\XML2020\XXXXXX\docs\xml-php> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.1
PSEdition Core
GitCommitId 7.2.1
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0