当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

作者:精品下载站 日期:2024-12-14 04:47:59 浏览:15 分类:玩电脑

PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?


[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

PowerShell批处理文件的定义

批处理文件是Windows操作系统或DOS中的一系列命令或脚本,它可以一次运行单个命令或多个命令,并在本地或远程计算机上执行一系列任务,它具有.蝙蝠或。 cmd 扩展名.尽管PowerShell和Batch是不同的语言,但两者可以相互集成并帮助调用和执行彼此的任务。

语法:

要从批处理文件调用 PowerShell 脚本,我们可以在批处理文件中使用以下语法。

Powershell.exe -ExecutionPolicy Unrestricted -command "PowerShell file path"

要从 PowerShell 脚本调用批处理文件,

Start-Process -FilePath “BatchFilePath”

要控制命令提示符执行,

Start-Process "cmd.exe " "/c Batch File Path"

批处理文件在 PowerShell 中如何工作?

批处理文件是我们在命令提示符或 cmd.exe 中单独编写的一系列命令。它们使用与 PowerShell 不同的语法编写,并在 Windows 操作系统上运行。

要从 PowerShell 简单地调用 cmd.exe,我们可以使用以下命令。

Start-Process -FilePath cmd.exe

此命令将从 PowerShell 控制台打开新的命令提示符窗口。

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

如果使用 -NoNewWindow 参数,PowerShell 将仅在 PowerShell 控制台内启动 cmd.exe 进程,并且 PowerShell 控制台将转换为 cmd 控制台,如下所示。

Start-Process -FilePath cmd.exe -NoNewWindow

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

类似地,如果我们有批处理文件,我们可以从 PowerShell 控制台调用它。
例如,我们有以下 BatchFile 内容,文件名为 TestBatch.bat,我们需要使用以下命令执行批处理文件然后使用 PowerShell,

TestBatch.bat 文件:

@echo off
echo Hello WOrld
pause

调用批处理文件,

Start-Process -FilePath "C:\Temp\TestBatch.bat"

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

在带有 -NoNewWindow 参数的同一控制台中。

Start-Process -FilePath "C:\Temp\TestBatch.bat" -NoNewWindow

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

在开始如何从批处理文件调用PowerShell文件之前,我们将使用如何从命令提示符窗口调用PowerShell。

当 PowerShell 安装在计算机上时,它会在环境变量中创建一个条目,以便您可以从 cmd 执行 PowerShell 和 PowerShell 核心版本。

例如,

要从 cmd 调用 PowerShell,只需键入 PowerShell.exe,如下所示。

PowerShell.exe

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

要调用 PowerShell 核心版本(6.0 或更高版本),请键入 Pwsh.exe。

pwsh.exe

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

如果 Powershell 版本已正确安装和设置,我们就可以通过这种方式从 cmd 调用 PowerShell 进程。

下一步我们将从命令提示符执行 PowerShell 命令。

Powershell.exe -ExecutionPolicy Unrestricted -Command "Write-Output 'Hello from CMD'"

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

在上面的示例中,我们使用 PowerShell.exe 调用 PowerShell,将执行策略设置为 UnRestricted,这样当我们运行该命令时,它不应阻塞,然后使用 -Command 参数写入输出。
现在我们有了上面的内容-提到的命令,我们可以将此命令保存在批处理文件(TestPSBatch.Bat)中并从cmd窗口执行它,它应该可以工作。

TestPSBatch.Bat 文件:

@echo off
Powershell.exe -ExecutionPolicy Unrestricted -Command "Write-
Output 'Hello from CMD'"

执行批处理文件,

C:\Temp\TestPSBatch.bat

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

您还可以通过双击从该位置直接执行该批处理文件。
在上面的示例中,如果我们在批处理文件中使用 Pwsh 而不是 PowerShell,那么它也应该可以工作。

示例

让我们讨论 PowerShell 批处理文件的示例。

示例 #1:从批处理文件调用 PowerShell 脚本。

这已经讨论过了,要从批处理文件调用 PowerShell 脚本,我们可以使用下面的示例。假设我们有一个将文件从源复制到目标的 PS 脚本,我们需要使用批处理文件执行它。
这是我们需要使用批处理文件执行的 TestPS.ps1 内容。

Copy-Item C:\Temp\envvariables.csv -Destination C:\Temp\Test\ -Verbose

我们创建了一个批处理文件copy.bat,其内容如下。

@echo off
PowerShell.exe -ExecutionPolicy Unrestricted -File C:\temp\TestPS.ps1

当我们执行 Copy.Bat 时,它应该显示如下输出。

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

示例 #2:从 PowerShell 脚本调用批处理脚本

此示例展示了如何从 PowerShell 调用批处理文件。

测试批.bat 文件。

@echo off
echo Hello WOrld

我们需要执行 PowerShell 脚本来运行批处理文件。

Start-Process -FilePath C:\Temp\TestBatch.bat -NoNewWindow

输出:

执行Testps.ps1文件后,

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

要以管理员身份运行批处理脚本,请使用 -Verb 参数。

Start-Process -FilePath C:\Temp\TestBatch.bat -Verb Runas

示例 #3:从批处理文件运行 PowerShell 命令

例如1我们也可以直接在cmd中使用批处理文件内容来运行PowerShell命令。然而,它是一个单行命令。假设我们想从 cmd 窗口运行多个 PowerShell 命令,那么也是可以的。

在此示例中,我们将复制文件,重新启动服务并使用执行 PowerShell 命令的批处理文件终止记事本进程,如下所示。

PowerShell.exe -executionpolicy Unrestricted Invoke-command -scriptblock { Copy-Item C:\tempvms.csv -Destination C:\Test1 -Verbose ; Stop-Service Spooler -Verbose ; Stop-Process -Name "Notepad" -Force -Verbose }

输出:

[玩转系统] PowerShell 批处理文件 |批处理文件在 PowerShell 中如何工作?

要远程运行命令,我们需要提供计算机名称,如下所示。它将在名为 TestMachine 的远程服务器上的 C:\CMDDir 路径上创建一个新目录。

PowerShell.exe -executionpolicy unrestricted Invoke-Command -ComputerName TestMachine -scriptblock {New-Item C:\cmddir}

示例 #4:从 PowerShell 运行批处理命令

要从 PowerShell 运行批处理命令,我们可以使用前面解释的 Start-Process cmdlet,它会在服务器上执行 cmd 命令。

Start-Process "cmd.exe" '/c mkdir C:\cmddir' -NoNewWindow -Verbose

上面的命令在本地服务器上创建目录 cmddir。

结论

PowerShell 和批处理文件的组合非常有用,因为很多时候新用户不知道如何在可以轻松执行批处理文件的情况下运行 PowerShell。 PowerShell 批处理文件对于 BladeLogic 等第三方软件也很有用,该软件不直接支持 PowerShell 扩展,但支持批处理文件。在这种情况下,我们可以运行批处理作业来远程执行 PowerShell 命令。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯