我在这里修改了一些解决方案的代码,但是当它作为bat脚本执行时它对我不起作用。在bat脚本中执行,它给出了以下错误。在命令行执行(使用脚本计算的正确行号代替%Line%,它可以正常工作。
关键是
more +%Line% %0 | powershell -c –
错误是
û : The term 'û' is not recognized as the name of a cmdlet, function, script file, or operable program.
完整的Bat脚本是
@echo off
set fn=Archive-Extract
set fnp0=C:\RMT\VCS\GIT\Games\soulfu\build\dependencies2\libogg-1.2.2.zip
set fnp1=C:\RMT\VCS\GIT\Games\soulfu\build\dependencies2
::::::::::::::::::::::::::::::::::::
REM Set %A to the line number of all lines starting with ':', this leaves %A with the line number of the last ':'.
for /f "delims=:" %%a In ('findstr /Bn ":" %0') do set /A Line=%%a
REM Send the content of this script past the last line starting with ':' to powershell.
more +%Line% %0 | powershell -c –
::::::::::::::::::::::::::::::::::::
dir *.bat
pause & exit /b
::::::::::::::::::::::::::::::::::::
function Archive-Extract([string]$zipFilePath, [string]$destinationPath) {
# This will get added when paths are joined, and path comparison will need it to be absent.
$destinationPath = $destinationPath.TrimEnd("\");
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem');
$zipfile = [IO.Compression.ZipFile]::OpenRead($zipFilePath);
# Determine how many top level entries there are.
$ziplevel0files = @{};
$zipfile.Entries | foreach {
$s = ($_.FullName.TrimEnd("/") -split "/")[0];
if ($ziplevel0files.ContainsKey($s)) {
$ziplevel0files[$s] = $ziplevel0files[$s] + 1;
} else {
$ziplevel0files[$s] = 0;
}
}
if ($ziplevel0files.count -ne 1) {
Write-Host "Zip archives are (at this time) expected to contain one top-level directory, and all content within it.";
return 1; # Failure
}
$zipDirPath = Join-Path -Path $destinationPath -ChildPath $ziplevel0files.Keys[0];
# If the directory does not exist, extract the zip archive into the current folder.
if (Test-Path -LiteralPath $zipDirPath) {
Write-Host "Top-level extraction directory already exists.";
return 2; # Failure
}
$zipfile.Entries | foreach {
$extractFilePath = Join-Path -Path $destinationPath -ChildPath $_.FullName;
$extractFileDirPath = Split-Path -Parent $extractFilePath;
# Skip the top-level directory everything comes under.
if ($extractFileDirPath -ne $destinationPath) {
if (-not (Test-Path -LiteralPath $extractFileDirPath -PathType Container)) {
New-Item -Path $extractFileDirPath -Type Directory | Out-Null;
}
# Sometimes a directory comes after a file within the directory (the latter causes it to be created implicitly above).
if (-not $extractFilePath.EndsWith("\")) {
try {
[IO.Compression.ZipFileExtensions]::ExtractToFile($_, $extractFilePath, $true);
} catch {
Write-Host "Failed to extract file:" $extractFilePath;
return 3; # Failure
}
}
}
}
return 0; # Success
}
# Anything that calls should execute the powershell and set the parameters.
$fn = (Get-ChildItem Env:fn).Value;
$f = (get-item -path function:$fn);
Write-Host "sss1" $fn;
if ($fn -eq "Archive-Extract") {
Write-Host "sss2";
$archivepath = (Get-ChildItem Env:fnp0).Value;
$destinationpath = (Get-ChildItem Env:fnp1).Value;
$err = & $f.ScriptBlock $archivepath $destinationpath;
exit $err;
} else {
Write-Host "sss3";
Write-Error "Failed to match function: "+ $fn;
exit 1000;
}
需要添加到BAT脚本中以执行与在命令行上逐行执行代码时相同的内容吗?
编辑:请注意,当我调整此脚本以遵循npocmaka建议的多行幂写评论方法时,上面的现有代码基于更多但没有跳过的行号。但是,我并不完全相信这解决了这个问题。我相信上面的代码在某一点上工作正常,并且为那些提出原始基本代码的人开始工作。
答案 0 :(得分:0)
<# :
@echo off
echo --START POWERSHELL--
:: $cmdargs variable will contain the command line.
powershell $cmdargs="""%*""";$exp=$(Get-Content '%~f0') -replace """`n""",""";""" ;invoke-expression """$exp"""
echo --START BATCH--
dir *.bat
pause & exit /b
#>
Write-Host "sss";
exit;
试试这个。我认为这是杂交的更好方法,并允许将参数传递给PowerShell脚本。请记住,每一行都应以;