简体   繁体   English

使用 PowerShell 为补丁自动化运行同步功能

[英]Run Simultaneous Functions for Patch Automation with PowerShell

I've seen different post regarding paralleling or run functions simultaneously in parallel but the code in the answers have not quite worked for me.我看过不同的关于并行或同时并行运行函数的帖子,但答案中的代码对我来说并不完全有效。 I'm doing patching automation and the functions I have all work and do their thing separately but since we work with more than 200+ computers, waiting for each function to finish with its batch of computers kind of defeats the purpose.我正在做补丁自动化,我所有的功能都可以单独工作,但由于我们与 200 多台计算机一起工作,等待每个功能在它的一批计算机上完成,这有点违背了目的。 I have the code in one script and in summary its structured like this:我有一个脚本中的代码,总而言之,它的结构如下:

define global variables定义全局变量

$global:varN $global:varN

define sub-functions定义子功能

function sub-function1()函数子函数1()

function sub-functionN()函数子函数N()

define main functions定义主要功能

function InstallGoogleFunction($global:varN) {函数 InstallGoogleFunction($global:varN) {

$var1 $var1

$result1 = sub-function1 $result1 = 子函数 1

$resultN = sub-functionN $resultN = 子函数N

} }

function InstallVLCFunction($global:varN) {函数安装VLCFunction($global:varN) {

"similar code as above" “类似上面的代码”

} }

function InstallAppFunction($global:varN) {函数 InstallAppFunction($global:varN) {

"similar code as above" “类似上面的代码”

} }

The functions will all install a different app/software and will write output to a file.这些函数都将安装不同的应用程序/软件,并将输出写入文件。 The only thing is I cannot seem to run all the functions for installation without waiting for the first one to finish.唯一的问题是我似乎无法在不等待第一个完成的情况下运行所有​​安装功能。 II tried start-job code but it executed and displayed a table like output but when verifying the computers neither had anything running on Task Manager.我尝试了启动作业代码,但它执行并显示了一个类似输出的表格,但在验证计算机时,任务管理器上都没有运行任何东西。 Is there a way powershell can run this installation functions at the same time?有没有办法powershell可以同时运行这个安装功能? If I have to resort to a one-by-one I will call the functions by the least amount of time taken or the least computers the functions read they need to install I will but I just wanted someone to better explain if this can be done.如果我不得不一一求助,我会用最少的时间调用函数,或者用最少的计算机调用它们需要安装的函数,但我只是想让有人更好地解释这是否可以做到.

You can use multithreading to achieve this.您可以使用多线程来实现这一点。 Below example shows how you can trigger tasks on multiple using multi threading in PowerShell.下面的示例显示了如何在 PowerShell 中使用多线程触发多个任务。

Replace list of machines in $Computers with your machines and give it a try.用你的机器替换$Computers中的机器列表并尝试一下。 Below example get the disk details on given machines下面的示例获取给定机器上的磁盘详细信息

# This is your script you want to execute
$wmidiskblock = 
{ 
  Param($ComputerName = "LocalHost") 
  Get-WmiObject -ComputerName $ComputerName -Class win32_logicaldisk |  Where-Object {$_.drivetype -eq 3} 
} 

# List of servers
$Computers = @("Machine1", "Machine2", "Machine3")  

#Start all jobs in parallel 
ForEach($Computer in $Computers) 
{ 
   Write-Host $Computer 
   Start-Job -scriptblock $wmidiskblock  -ArgumentList $Computer 
} 

Get-Job | Wait-Job 

$out = Get-Job | Receive-Job 
$out |export-csv 'c:\temp\wmi.csv'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM