AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1527355
Accepted
bckelley
bckelley
Asked: 2020-02-22 14:01:10 +0800 CST2020-02-22 14:01:10 +0800 CST 2020-02-22 14:01:10 +0800 CST

将批处理文件重写为powershell

  • 772

我有一个批处理文件,它查看 inf 文件并找到 open= Open= 或 OPEN= 并让程序运行。

@echo off
setlocal ENABLEDELAYEDEXPANSION

set _drive=W:
set _cmd=

cd /d !_drive!\
for /f "tokens=1,2* delims==" %%i in (autorun.inf) do (
  if "%%i"=="open" set _cmd=%%j
  if "%%i"=="Open" set _cmd=%%j
  if "%%i"=="OPEN" set _cmd=%%j
)
if not defined _cmd (
  echo Unable to parse autorun.inf and find 'open='
) else (
  !_drive!\!_cmd!
  START !_drive!
)

endlocal

我希望有人比我自己更擅长powershell,因为我一直在网上寻找一些东西。所以我应该提到这是这里的OP

powershell batch-file
  • 2 2 个回答
  • 1441 Views

2 个回答

  • Voted
  1. Best Answer
    Anaksunaman
    2020-02-23T00:45:50+08:002020-02-23T00:45:50+08:00

    免责声明:这是在短时间内通过一些基本的谷歌搜索完成的。因此,可能有更有效和/或更稳健的方法来处理下面的脚本(例如,通过管道命令一起)。


    假设您只是想读取autorun.inf并启动.exe在 eg 之后找到的文件(或您拥有的文件)open=,您可能想尝试类似以下的操作:

    前任。搜索_INF.ps1

    # Open "autorun.inf", search for a string containing ex. "open=", then perform
    # actions based on the string found.
    
    # --- Variables ---
    
    $_drive = 'W:'
    $_file = 'C:\path\to\autorun.inf'
    
    # --- Main ---
    
    # Use Select-String to find a given -Pattern in the $_file.
    $_match = Select-String -Path $_file -List -Pattern 'open='
    
    # Check the value of $_match.Line. If there were no matches (i.e. $_match.Line
    # is $null), let the user know and exit the script.
    
    if ($_match.Line -eq $Null) {
    
        Write-Output ''
        Write-Output 'Unable to parse autorun.inf and find "open="'
        Exit
    
    }
    
    # Returns just the matched string (ex. "open=program.exe") from $_match.
    $_program = $_match.Line
    
    # Slice off the first five characters (i.e. "open=") of e.g. "open=program.exe"
    # (leaving just ex. "program.exe")
    $_program = ($_program).Substring(5)
    
    # Build the command string.
    $_cmd = $_drive + '\' + $_program
    
    # Start the program (ex. as "W:\program.exe")
    START $_cmd
    
    # Open the specified $_drive in Explorer. Equivalent to e.g. "start W:" in CMD.
    CMD /C START $_drive
    

    要运行上面的脚本,请从命令行调用powershell,确保您指定脚本的路径,例如:

    powershell .\search_inf.ps1
    

    (假设命令窗口与您的文件在同一目录中.ps1打开)。

    请注意,在您明确允许之前,Windows 可能不允许您运行 PowerShell 脚本。


    笔记

    • 在上面的脚本中,Select-String最初设置$_match为 eg Path\to\autorun.inf:2:open=program.exe。

    • 使用该-List选项,Select-String仅返回给定输入文件的匹配文本的第一个实例。

    • open=不区分大小写(Select-String据我所知,这是 的默认值)。所以它应该涵盖open=,Open=并且OPEN=在原始脚本中。

    • open=可能会shellexecute=用于某些autorun.inf文件。

    • $_match.Line只返回匹配的行(例如open=program.exe)而不是 ex。Path\to\autorun.inf:2:open=program.exe.

    • 根据粗略的测试,该CMD /C START $_drive语法似乎只适用于裸盘符(例如W:)。

    • $_file = 'C:\path\to\autorun.inf'是占位符。这可能是例如$_file = $_drive + '\autorun.inf',假设autorun.inf驻留在 ex 的根目录中。W:\.


    PowerShell 资源

    微软

    • 变量

    • 写输出

    • 选择字符串

    • 匹配信息对象

    • 启动过程

    ThinkPowerShell.com

    • $空

    StackOverflow.com

    • 出口

    SS64.com

    • 子串
    • 4
  2. Keith Miller
    2020-02-23T10:07:13+08:002020-02-23T10:07:13+08:00

    电源外壳。短而甜。将任何命令复制到网络搜索中以阅读 MS 文档。

    编辑:代码改进以处理命令行参数并检查“ShellExecute”

    $Drive = 'W:'
    Set-Location $Drive
    $Run = ( Get-Content autorun.inf | ?{ $_ -match 'Open=|ShellExecute=' }) -replace 'Open=' -replace 'ShellExecute='
    If ( !($Run) ) {
        echo 'No "Open" or "ShellExecute" statement in the "autorun.inf" file.'
    } Else {
        $Run = $Run.Split(' ')
        $Splat = @{ 'FilePath' = $Run[0] }
        If ($Run.Count -gt 1) {
            $Splat += @{ 'ArgumentList' = $Run[1..$Run.Count] }
        }
        Try {
            If ( Test-Path $Splat.FilePath ) {
                $Splat.FilePath = (Resolve-Path $Splat.FilePath).Path
                Start-Process @Splat
            } Else {
               echo "'$($Run[0])' is not a valid path to executeable."
            }
        } Catch {
            Echo "An error occured when attempting to run '$($Run[0])'"
        }
        explorer "."
    }
    
    • 1

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve