7

I'm trying to get one master PowerShell script to run all of the others while waiting 30-60 seconds to ensure that the tasks are completed. Everything else I tried wouldn't stop/wait for the first script and its processes to complete before going through all the others at the same time and would cause a restart automatically.

Main script, run as admin:

$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."
}

The log file shows:

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

They aren't running correctly at all though. They run fine individually but not when put into one master script.

5
  • 1
    you may want to look at Get-Help Start-Process -Parameter Wait for one way to do what you seem to want. [grin] Commented Sep 14, 2019 at 3:13
  • Hi! Thanks for the comment. I tried wait instead of the sleep timer, but it ran all of the scripts at once and straight into a reset. Commented Sep 14, 2019 at 4:08
  • 1
    you likely need to post the actual line of code you used. for instance, i think the ArgumentList parameter needs to be the last one. also, this >>> '& $script'" <<< looks wrong. i think you are supposed to put -File when you use a script - AND you almost certainly aint supposed to use the call operator & at that point. Commented Sep 14, 2019 at 4:26
  • Okay Lee. I’ll go back and take a look at what you’re saying when I’m in the office tomorrow. Thank you. Commented Sep 14, 2019 at 4:48
  • you are quite welcome! good luck ... [grin] Commented Sep 14, 2019 at 5:33

1 Answer 1

12

PowerShell can run PowerShell scripts from other PowerShell scripts directly. The only time you need Start-Process for that is when you want to run the called script with elevated privileges (which isn't necessary here, since your parent script is already running elevated).

This should suffice:

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

The above code will run the scripts sequentially (i.e. start the next script only after the previous one terminated). If you want to run the scripts in parallel, the canonical way is to use background jobs:

$jobs = foreach ($script in $scriptList) {
    Start-Job -ScriptBlock { & $using:script }
}
$jobs | Wait-Job | Receive-Job
Sign up to request clarification or add additional context in comments.

1 Comment

What's the use of the ampersand before the script's name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.