Board logo

标题: [转贴] 批处理 + powershell [打印本页]

作者: newswan    时间: 2021-7-23 00:46     标题: 批处理 + powershell

本帖最后由 newswan 于 2021-7-23 14:24 编辑

1 批处理 调用 powershell 脚本

两个文件,同名,放在一个目录里,像这样:
  1. my-script.bat
  2. my-script.ps1
复制代码
ps1 是 PowerShell 脚本文件
bat 内容如下
  1. @ECHO OFF
  2. PowerShell -ExecutionPolicy Bypass -File "%~dpn0.ps1" %1
复制代码
powershell RunAs
  1. PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}";
复制代码

作者: newswan    时间: 2021-7-23 00:47

本帖最后由 newswan 于 2021-7-23 15:30 编辑

2 powershell 脚本包装成 批处理
在powershell 脚本前面插入1行,保存为bat
  1. @cd /d %~dp0 & powershell -command "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & pause & exit/b
复制代码

作者: xczxczxcz    时间: 2021-7-23 15:54

要想发挥 powershell  的强大威力, .NET 你值得拥有。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2