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

[玩转系统] 从另一个脚本调用 PowerShell 脚本 [4 种方法]

作者:精品下载站 日期:2024-12-14 05:24:52 浏览:15 分类:玩电脑

从另一个脚本调用 PowerShell 脚本 [4 种方法]


[玩转系统] 从另一个脚本调用 PowerShell 脚本 [4 种方法]

1. 问题陈述简介

PowerShell 是一种功能强大的脚本语言和命令行 shell,广泛用于 Windows 环境中的自动化。 PowerShell 脚本编写的一个常见要求是从一个脚本调用另一个脚本,从而实现模块化编程和代码的可重用性。

假设我们有两个脚本:

  • ChildScript.ps1:此脚本执行任务并返回一个值。例如,它可以检查磁盘空间,如果有足够的空间则返回 true,否则返回 false。
  • MainScript.ps1:此脚本调用 ChildScript.ps1 并根据 ChildScript.ps1 返回的值采取操作。

2.ChildScript.ps1

子脚本.ps1:

# ChildScript.ps1

# Function to check disk space
function Check-DiskSpace {
    # Example condition: If free space is more than 10GB
    $freeSpace = Get-PSDrive C | Select-Object -ExpandProperty Free
    return $freeSpace -gt 10GB
}

# Calling the function and returning its output
Check-DiskSpace

让我们根据要使用的不同方法编写不同的主脚本。

3. 使用&运算符(Call Operator)

最直接的方法是使用 PowerShell 中的 & 运算符,也称为调用运算符。

MainScript.ps1(使用 & 运算符):

# MainScript.ps1

# Path to the child script
$childScriptPath = ".\ChildScript.ps1"

# Calling the child script and capturing the output
$isEnoughSpace = & $childScriptPath

# Making a decision based on the output
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available."
} else {
    Write-Host "Warning: Low disk space."
}

说明:

  • 设置子脚本的路径:脚本将 $childScriptPath 设置为 ChildScript.ps1 的相对路径。
  • 调用子脚本:它使用调用运算符 (&) 执行 ChildScript.ps1 并捕获其输出(预计为布尔值) )在变量$isEnoughSpace中。
  • 基于输出的决策if-else 语句检查$isEnoughSpace 的值。如果为 $true,脚本将打印“足够的可用磁盘空间”。否则,它会打印“警告:磁盘空间不足”。

此脚本演示了如何调用单独的 PowerShell 脚本并将其输出用于调用脚本中的条件逻辑。

性能:

此方法高效且性能开销最小。它是直接调用并在当前 PowerShell 会话中执行脚本。

4. 使用点源

点源是 PowerShell 中的一种在当前作用域中运行脚本的技术,允许主脚本访问子脚本中定义的变量和函数。

MainScript.ps1(使用点源):

# MainScript.ps1

# Path to the child script
$childScriptPath = ".\ChildScript.ps1"

# Dot sourcing the child script
. $childScriptPath

# Since the function is now available in the main script's scope, it can be called directly
$isEnoughSpace = Check-DiskSpace

# Decision based on the output
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available."
} else {
    Write-Host "Warning: Low disk space."
}

此脚本 MainScript.ps1 演示了如何在 PowerShell 中使用点源来调用另一个脚本 (ChildScript.ps1) 中的函数并使用其输出进行决策:

  1. 设置子脚本的路径:变量$childScriptPath被分配到ChildScript.ps1的相对路径。
  2. 点溯源子脚本:点溯源运算符 (.) 用于执行 ChildScript.ps1。这使得 ChildScript.ps1 中的所有函数和变量在 MainScript.ps1 范围内可用。
  3. 直接调用函数:直接调用ChildScript.ps1中的函数Check-DiskSpace,其输出(预计是一个指示磁盘的布尔值)空间状态)存储在$isEnoughSpace中。
  4. 根据输出做出决策:该脚本使用 if-else 语句来检查 $isEnoughSpace。如果是$true,则表示磁盘空间充足,并打印“Sufficient disk space available”。否则,它会打印“警告:磁盘空间不足”。

当我们想要直接在当前脚本的作用域内访问另一个脚本的函数或变量时,这种方法非常有用。

性能:

当主脚本需要访问子脚本中定义的资源时,点采购非常高效并且特别有用。它具有与呼叫操作员类似的性能特征。

5. 使用调用表达式

另一种方法是使用 Invoke-Expression cmdlet,它将字符串计算为 PowerShell 代码并执行它。

MainScript.ps1(使用调用表达式):

# MainScript.ps1

# Path to the child script
$childScriptPath = ".\ChildScript.ps1"

# Calling the child script and capturing the output using Invoke-Expression
$isEnoughSpace = Invoke-Expression $childScriptPath

# Making a decision based on the output
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available."
} else {
    Write-Host "Warning: Low disk space."
}

说明Invoke-Expression 将指定路径中的脚本作为 PowerShell 命令进行计算。子脚本的输出被捕获到 $isEnoughSpace 中,然后用于做出决定。

性能:

Invoke-Expression 解析字符串表达式的开销很小,但这通常可以忽略不计。对于大多数实际用途来说,它与呼叫操作员一样高效。

6. 使用启动进程

要完全独立地运行脚本,可以使用Start-Process。 对于Start-Process需要注意的是,它启动了一个单独的进程,这使得直接捕获输出变得更加复杂。为了捕获输出,我们通常会将输出重定向到文件,然后从该文件中读取。

MainScript.ps1(使用启动进程):

# MainScript.ps1

# Path to the child script
$childScriptPath = ".\ChildScript.ps1"
$outputFile = ".\output.txt"

# Starting the child script as a separate process and redirecting output to a file
Start-Process PowerShell -ArgumentList "-File `"$childScriptPath`" > `"$outputFile`"" -Wait -NoNewWindow

# Reading output from the file
$isEnoughSpace = Get-Content $outputFile -Raw

# Making a decision based on the output
if ($isEnoughSpace -eq $true) {
    Write-Host "Sufficient disk space available."
} else {
    Write-Host "Warning: Low disk space."
}

# Optionally, delete the output file if it's no longer needed
Remove-Item $outputFile

说明Start-Process用于在单独的PowerShell进程中运行ChildScript.ps1。输出被重定向到临时文件 (output.txt)。然后主脚本读取该文件的内容以捕获子脚本的输出。处理输出后,临时文件将被删除以进行清理。

此方法对于完全隔离子脚本非常有用,可以在单独的进程中运行它。这对于资源密集型脚本或需要在不同上下文中运行的脚本非常有用。

性能:

与其他方法相比,将脚本作为单独的进程运行会带来额外的开销。由于创建了新进程,它的速度较慢,但提供了隔离,并且在需要这种隔离时很有用。

7. 使用另一个脚本的参数调用 PowerShell 脚本

在高级 PowerShell 脚本编写中,通常不仅会从另一个脚本调用一个脚本,还会将参数传递给被调用的脚本。这种方法增强了脚本的灵活性,并允许根据不同的输入动态执行。

场景:继续前面的示例,假设 ChildScript.ps1 现在接受一个参数来指定要检查磁盘空间的驱动器。 MainScript.ps1 需要以特定盘符作为参数调用 ChildScript.ps1,然后根据返回值进行操作。

目标:我们将探索使用 MainScript.ps1 中的参数调用 ChildScript.ps1 的方法,捕获输出并在 中使用它>MainScript.ps1 进行进一步操作。

子脚本.ps1:

# ChildScript.ps1

param (
    [string]$DriveLetter
)

function Check-DiskSpace {
    param (
        [string]$Drive
    )
    $freeSpace = Get-PSDrive $Drive | Select-Object -ExpandProperty Free
    return $freeSpace -gt 10GB
}

Check-DiskSpace -Drive $DriveLetter

说明ChildScript.ps1 现在接受参数 $DriveLetterCheck-DiskSpace 函数使用此参数来检查指定驱动器的磁盘空间。

让我们使用不同的方法使用另一个脚本的参数来调用 powerShell 脚本:

7.1 使用带参数的调用运算符 (&):

MainScript.ps1(使用 & 运算符):

# Using the Call Operator with Parameters

$childScriptPath = ".\ChildScript.ps1"
$drive = "C"
$isEnoughSpace = & $childScriptPath -DriveLetter $drive
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available on drive $drive."
} else {
    Write-Host "Warning: Low disk space on drive $drive."
}

7.2 使用带有参数的点源:

MainScript.ps1(使用带有参数的点源):

# Using Dot Sourcing with Parameters

. $childScriptPath -DriveLetter $drive
$isEnoughSpace = Check-DiskSpace -Drive $drive
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available on drive $drive."
} else {
    Write-Host "Warning: Low disk space on drive $drive."
}

7.3 使用带参数的调用表达式:

MainScript.ps1(使用带参数的调用表达式):

# Using Invoke-Expression with Parameters

$scriptWithParams = "$childScriptPath -DriveLetter $drive"
$isEnoughSpace = Invoke-Expression $scriptWithParams
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available on drive $drive."
} else {
    Write-Host "Warning: Low disk space on drive $drive."
}

7.4 使用带有参数的启动进程:

MainScript.ps1(使用带有参数的启动进程):

# Using Start-Process with Parameters

$outputFile = ".\output.txt"
$argumentList = "-File `"$childScriptPath`" -DriveLetter $drive > `"$outputFile`""
Start-Process PowerShell -ArgumentList $argumentList -Wait -NoNewWindow
$isEnoughSpace = Get-Content $outputFile -Raw
if ($isEnoughSpace) {
    Write-Host "Sufficient disk space available on drive $drive."
} else {
    Write-Host "Warning: Low disk space on drive $drive."
}

Remove-Item $outputFile

8. 将复杂对象作为参数传递

由于脚本和进程处理数据方式的本质,在 PowerShell 脚本之间直接传递复杂对象作为参数本身就具有挑战性。但是,有一些方法和解决方法需要考虑:

8.1.点采购

点溯源是一种可以在同一上下文中运行脚本时使用的方法。它不会启动新进程,而是在当前 PowerShell 会话中运行脚本。

  • 用法:如果我们点源子脚本,它会在调用脚本的上下文中运行。这允许对另一个脚本中定义的复杂对象进行共享访问。
  • 限制:仅当脚本在同一 PowerShell 会话中执行时才有效。不适合需要脚本在隔离环境中运行的场景。

8.2.基于会话的变量

在PowerShell中,我们可以创建基于会话的变量来存储复杂的对象。这些变量可以在同一会话中运行的脚本中访问。

  • 用法:在一个脚本中定义会话变量并在另一个脚本中访问它。
  • 限制:与点源类似,此方法仅在同一 PowerShell 会话中有效。

8.3.序列化和反序列化

对于在不同上下文(例如不同进程)中运行的脚本之间传递数据,序列化是最可行的方法。我们将对象序列化为字符串格式(例如 JSON 或 XML),将字符串传递给其他脚本,然后将其反序列化回对象。

  • 用法:使用 ConvertTo-JsonConvertFrom-JsonExport-ClixmlImport-Clixml 用于将对象与字符串相互转换。
  • 限制:序列化可能无法完美保留某些类型的对象,并且序列化和反序列化对象会产生开销。

8.4.临时文件存储

将对象存储在临时文件中是在脚本之间共享复杂数据的另一种方法。这是一种更加手动的方法,但对于某些类型的对象可能有效。

  • 用法:将对象存储在一个脚本中的临时文件中,并从另一个脚本中读取它。
  • 限制:需要管理临时文件并确保它们被安全地创建并在使用后正确删除。

8.5。进程间通信(IPC)

对于高级场景,可以使用命名管道或内存映射文件等 IPC 机制在脚本之间传递复杂对象。

  • 用法:建立IPC通道,在进程间传输数据。
  • 限制:这种方法很复杂,对于更简单的脚本需求来说可能有点过分了。

9. 结论

有多种方法可以从另一个脚本调用 PowerShell 脚本,每种方法都有不同的用例和性能影响:

  • 使用调用运算符 (&):最适合通用脚本调用。它简单、高效,并且在当前会话中执行脚本。
  • 使用Invoke-Expression:提供灵活性,但应谨慎使用以避免安全风险。
  • 点源 (.):非常适合主脚本需要从子脚本访问资源的场景。
  • 使用Start-Process:适合将子脚本执行与主脚本隔离,尽管会带来一些性能开销。

在选择正确的方法时,请考虑脚本的具体要求,例如是否需要在脚本之间共享变量和函数,或者是否需要脚本执行隔离。 对于大多数情况,调用运算符或点采购就足够了,可以在效率和功能之间实现平衡。

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

取消回复欢迎 发表评论:

关灯