简体   繁体   English

在另一个 Powershell 脚本中运行 Powershell 脚本

[英]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.我和我的同事正在开发一组脚本,与 Windows 成像器和配置设计器 (WICD) 一起使用,为新 PC 提供定制软件并去除一堆讨厌的东西。 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.我可以通过日志文件和 output 判断它尝试运行脚本,但它所做的只是启动脚本,然后在没有任何 output、回溯或任何表明它失败的情况下退出,除了 Z3D265B4E1EEB8DCCZ3 提示。 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.如果您愿意,我还可以发布安装脚本和 Windows 清理。 Line 24 is where I'm hitting a roadblock.第 24 行是我遇到障碍的地方。

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.我在我的 WindowsSetup 脚本中发现了一个错误,导致 cmd 实例打开而不关闭,从而破坏了脚本。

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

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

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