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

Posted

技术标签:

【中文标题】在另一个 Powershell 脚本中运行一个 Powershell 脚本【英文标题】:Running a Powershell script inside another Powershell script 【发布时间】:2022-01-23 07:39:35 【问题描述】:

我和我的同事正在开发一组脚本,以与 Windows Imager 和 Configuration Designer (WICD) 结合使用,以便为新 PC 提供自定义软件并去除一堆讨厌的东西。我们让所有组件独立工作,我们只需要将它们串在一起,这样它们就可以同时运行,几乎不需要用户干预。以下是其工作原理的细分:

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.

或者,至少它应该运行最后一个脚本。这是我唯一没有工作的事情。我可以通过日志文件和输出判断它尝试运行脚本,但它所做的只是启动脚本,然后退出,没有任何输出、回溯或任何表明它失败的东西,除了 PowerShell 提示符。 以下是清理脚本的经过清理和编辑的版本。让这个其他脚本运行我做错了什么吗?还有另一种方法我应该接近这个。非常感谢任何帮助。

如果您愿意,我还可以发布安装脚本和 Windows 清理。第 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

【问题讨论】:

两个旁白:一般来说,没有充分的理由通过pwoershll.exe child 进程运行*.ps1 脚本文件 - 只需直接调用C:\Support\Scripts\WindowsSetup2_0.ps1 。虽然这样做是良性的,但 没有 理由使用转义 \ 字符。在 PowerShell 中作为 \\ 这些部分不是我自己写的。我的同事做了,我只是想在解决其他问题之前让它工作。我尝试直接调用C:\Support\Scripts\WindowsSetup2_0.ps1,但它仍然只是启动(第二个脚本的)脚本并终止到 PowerShell 提示符。 这就是为什么我将我的 cmets 称为 asides:它们不能解决您的问题,而是帮助您改进代码的通用指针。 我会在打扫房间时处理它。顺便说一句,我在我的 WindowsSetup 脚本中发现了一个错误,它导致 cmd 实例打开而不是关闭,从而破坏了脚本。所以我解决了它 【参考方案1】:

我在我的 WindowsSetup 脚本中发现了一个错误,它导致 cmd 实例打开而不是关闭,从而破坏了脚本。

【讨论】:

以上是关于在另一个 Powershell 脚本中运行一个 Powershell 脚本的主要内容,如果未能解决你的问题,请参考以下文章

powershell怎么运行

在python脚本中运行powershell脚本,如何让python在运行时打印powershell输出

每次系统从睡眠中唤醒时都需要运行一个 powershell 脚本

python在powershell中运行脚本给出'非utf'错误[重复]

停止在另一个 Python 脚本中运行的一个 Python 脚本

在 powershell 中运行 nmake