如何将嵌入式脚本通过管道传输到powershell
我从这里的一个解决方案中改编了一些代码,但当作为bat脚本执行时,它对我不起作用。在bat脚本中执行,它给出以下错误。在命令行上执行(使用脚本计算的正确行号代替%line%),可以正常工作 关键是如何将嵌入式脚本通过管道传输到powershell,powershell,batch-file,Powershell,Batch File,我从这里的一个解决方案中改编了一些代码,但当作为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脚
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脚本添加什么才能执行相同的代码
编辑:请注意,当我调整此脚本以遵循Npcmaka推荐的多行powerscript注释方法时,上面基于更多但没有跳过行号的现有代码起作用。但是,我并不完全相信这能解决问题。我相信上面的代码在某一点上可以很好地工作,并为最初提出基本代码的人工作。
<# :
@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;
写主机“sss”;
出口
试试这个。我认为这是一种更好的混合方式,允许将参数传递到powershell脚本。请记住,每一行都应该以
;
结尾,所以您希望在一个文件中包含批处理和powershell代码?是的。让这个问题工作起来是这个问题的唯一目的。这是一篇非常古老的帖子,但我认为我可能发现了问题如果你仔细看“钥匙”在上面的列表行中,您会注意到,从标准输入读取的-似乎比-c中的-长一些。通过适当地剪切和粘贴到记事本中并保存为UTF-8,结果表明,长-实际上是U+2013 EN破折号,它在Ansi模式下由记事本保存为0x96,这是OEM字符。因此,结果是“关键”行实际上是“更多+%line%%0 | powershell-cú”这适用于powershell的简单片段。但它不适用于我计划使用的实际代码。请参阅更新的问题,该问题提取了一个zip存档,其中包含一个包含文件的目录。@yourpublicdisplayname-问题可能是一行注释。请尝试使用
。请记住,这种混合可以不完美。Powershell对扩展非常严格。您也可以尝试将其与[ADS]()混合使用。问题不是单行注释。如果删除第一条注释,则会在下一行代码中出错。我希望问题是前导空格。