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 / 问题 / 1646516
Accepted
bat_cmd
bat_cmd
Asked: 2021-05-04 22:26:07 +0800 CST2021-05-04 22:26:07 +0800 CST 2021-05-04 22:26:07 +0800 CST

在批处理文件中链接 Powershell 命令?(视窗)

  • 772

我在批处理文件中运行 Powershell 命令来替换文本。

这可行,但每次替换某些东西时它都会运行一个 Powershell 实例:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | Set-Content $_.FullName}"

:: Replace foo2 with bar2
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2' | Set-Content $_.FullName}"

:: Replace foo3 with bar3
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

& ^有没有办法通过在第二条命令的末尾添加类似的东西来链接三个命令?

我想将每个-replace命令放在单独的行上,因为我有 100 多个命令要链接在一起。

我试图这样做,但它不起作用:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | & ^

:: Replace foo2 with bar2
 | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2' | & ^

:: Replace foo3 with bar3
 | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

我也试过上面没有&字符的方法。

我尝试ForEach-Object {(Get-Content $_)从中间和最后一行删除,不起作用。

它是:: comments造成问题的原因吗?

我想既然这是用双引号括起来的 Powershell 命令,我会尝试这种方式,每行末尾的反勾号和分号应该告诉 Powershell 继续执行命令并将下一行视为相同的命令:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1';`

# Replace foo2 with bar2
 | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2';`

# Replace foo3 with bar3
 | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

(不起作用)

我尝试取出一些 Powershell 命令:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1';`

# Replace foo2 with bar2
'foo2', 'bar2';`

# Replace foo3 with bar3
'foo3', 'bar3' | Set-Content $_.FullName}"

(不起作用)。

我已经尝试完全删除评论,不起作用。

我已经尝试添加回-replace每行的开头,但不起作用。

试图把它放在一条线上是行不通的:

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1'; -replace 'foo2', 'bar2'; -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

放置管道而不是分号也不起作用:

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | -replace 'foo2', 'bar2' | -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

可能有几十种不同的组合试图让它发挥作用,但我不知道正确的方法是什么,而且我没有想法。

;当与其他答案中描述的类似命令链接在一起时,这些命令不起作用。

干杯。

windows command-line
  • 2 2 个回答
  • 758 Views

2 个回答

  • Voted
  1. Best Answer
    LPChip
    2021-05-04T22:53:06+08:002021-05-04T22:53:06+08:00

    您想将许多命令连续放置并从批处理文件中调用它吗?

    您的方法不仅不起作用,因为您不能在批处理文件中的一个命令中放入这么多字符,而且也不可能那么容易地将它们链接起来。

    这里明显的解决方案是要么只使用 PowerShell,要么将所有命令放在 .ps1 文件中,并将批处理文件中的 powershell 命令更改为:

    start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -File MyFile.ps1
    

    要编辑 .ps1 文件,请使用 PowerShell ISE。这不仅为您提供了语法高亮显示,您还可以通过运行一个命令而不是整个脚本来非常轻松地测试您正在执行的操作,并且您在脚本中设置的每个变量在执行后都会保留,因此您可以在脚本运行后在控制台中进行实时测试运行以调试工作不正常的地方。

    您将在批处理文件中运行的每个命令也可以在 PowerShell 中运行。即使是逻辑等,PowerShell 也提供了功能,因此您也可以编写更易于阅读的代码。

    • 2
  2. Io-oI
    2021-05-05T02:07:34+08:002021-05-05T02:07:34+08:00

    您可以尝试一条线与倍数.replace():
    .replace('foo1','bar1').replace('foo2','bar2').replace('foo3','bar3')...

    start "" /wait /min /wait Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | foreach {(Get-Content $_) .replace('foo1','bar1')。 replace('foo2','bar2').replace('foo3','bar3') |设置内容 $_.FullName -force}"
    • 0

相关问题

  • Python 的“pass”参数的批处理等价物是什么?

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

  • 在 Windows 上与 Docker 守护进程通信

  • 资源管理器侧面板中的桌面外壳快捷方式

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
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • 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
    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