简体   繁体   中英

Running a Powershell script inside another Powershell script

My coworker and I are developing a set of scripts to use in conjunction with Windows Imager and Configuration Designer (WICD) to provision new PCs with custom software and to strip out a bunch of nasty stuff. We have all the components working independently, we just need to string them together so that they all run at once with little to no user intervention. Here's a breakdown of how it works:

Get to OOBE experience where you select region
Plug in USB drive with provisioning package at the root of the USB drive
Let things run

First script (install) creates working directories, downloads scripts from github repo, skips OOBE turning off everything, creates admin user and password, enables auto logon, uses a run once registry key to run second script.

Second script (cleanup) Waits 30 seconds to let the registry populate
Resets privacy settings, disables auto logon, deletes stored credentials, installs a piece of software, and lastly runs another script which installs more software and strips a bunch of bloatware out of Windows.

Or, at least it's supposed to run that last script. That's the only thing I don't have working. I can tell by the log files and output that it attempts to run the script but all it does is start the transcript and then exits without any output, traceback, or anything to indicate that it's failing except for a PowerShell prompt. Below is a sanitized and redacted version of the cleanup script. Is there something I'm doing wrong to get this other script to run? Is there another way I should be approaching this. Any help is greatly appreciated.

I can also post the install script and the Windows cleanup if you'd like. Line 24 is where I'm hitting a roadblock.

Start-Transcript -Append C:\Support\Logs\PostDeploymentCleanupLog.txt

#Wait for registry to populate
Start-Sleep -s 30

# Reset Privacy settings to default
reg delete HKLM\SOFTWARE\Policies\Microsoft\Windows\OOBE /v DisablePrivacyExperience /f

# Disable autoLogon
REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f

# Remove stored credentials
REG DELETE "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /f


# Start SomeSoftware installer in quiet mode
Function Install-SomeSoftware {...} #End Function Install-SomeSoftware


Install-SomeSoftware -Server '<redacted>' -LocationID <redacted> -Token '<redacted>' -Silent -Force -Transcript

# Run WindowsSetup2_0-WIP
# Forked from SomeGuy's GitHub repo
PowerShell.exe -ExecutionPolicy Bypass -File C:\Support\Scripts\WindowsSetup2_0.ps1

#Remove unneeded directories
Remove-Item -Path C:\\Support\\Scripts -Recurse -Verbose
Remove-Item -Path C:\\Support\\Installers -Recurse -Verbose

Stop-Transcript

I found an error in my WindowsSetup script which was causing cmd instances to open and not close, hence breaking the script.

& "PowerShell.exe -ExecutionPolicy Bypass -File C:\Support\Scripts\WindowsSetup2_0.ps1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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