gpt4 book ai didi

powershell - 在 PowerShell 脚本中调用其他 PowerShell 脚本

转载 作者:行者123 更新时间:2023-12-02 23:00:41 30 4
gpt4 key购买 nike

我正在尝试让一个主 PowerShell 脚本运行所有其他脚本,同时等待 30-60 秒以确保完成任务。我尝试过的所有其他操作都不会停止/等待第一个脚本及其进程完成,然后才能同时完成所有其他脚本,并且会导致自动重新启动。

主脚本,以管理员身份运行:

$LogStart = 'Log '

$LogDate = Get-Date -Format "dd-MM-yyy-hh-mm-ss"

$FileName = $LogStart + $LogDate + '.txt.'

$scriptList = @(
'C:\Scripts\1-OneDriveUninstall.ps1'
'C:\Scripts\2-ComputerRename.ps1'
);

Start-Transcript -Path "C:\Scripts\$FileName"

foreach ($script in $scriptList) {
Start-Process -FilePath "$PSHOME\powershell.exe" -ArgumentList "-Command '& $script'"
Write-Output "The $script is running."
Start-Sleep -Seconds 30
}

Write-Output "Scripts have completed. Computer will restart in 10 seconds."
Start-Sleep -Seconds 10

Stop-Transcript

C:\Scripts\3-Restart.ps1
1-OneDriveUninstall.ps1 :
Set-ItemProperty -Path REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin -Value 0

taskkill /f /im OneDrive.exe

C:\Windows\SysWOW64\OneDriveSetup.exe /uninstall
2-ComputerRename.ps1 :
$computername = Get-Content env:computername
$servicetag = Get-WmiObject Win32_Bios |
Select-Object -ExpandProperty SerialNumber
if ($computername -ne $servicetag) {
Write-Host "Renaming computer to $servicetag..."
Rename-Computer -NewName $servicetag
} else {
Write-Host "Computer name is already set to service tag."
}

日志文件显示:

Transcript started, output file is C:\Scripts\Log 13-09-2019-04-28-47.txt.
The C:\Scripts\1-OneDriveUninstall.ps1 is running.
The C:\Scripts\2-ComputerRename.ps1 is running.
Scripts have completed. Computer will restart in 10 seconds.
Windows PowerShell transcript end
End time: 20190913162957



但是,它们根本没有正确运行。它们单独运行良好,但在放入一个主脚本中时则不然。

最佳答案

PowerShell 可以直接从其他 PowerShell 脚本运行 PowerShell 脚本。您唯一需要的时候 Start-Process因为那是当您想以提升的权限运行被调用的脚本时(这里没有必要,因为您的父脚本已经提升了运行)。

这应该足够了:

foreach ($script in $scriptList) {
& $script
}

上面的代码将按顺序运行脚本(即只有在前一个脚本终止后才启动下一个脚本)。如果要并行运行脚本,规范的方法是使用 background jobs :
$jobs = foreach ($script in $scriptList) {
Start-Job -ScriptBlock { & $using:script }
}
$jobs | Wait-Job | Receive-Job

关于powershell - 在 PowerShell 脚本中调用其他 PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932053/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com