我正在另一个脚本中调用一个脚本:
...
...
powershell C:/temp/script2.ps1"
...
...
下载完某些内容后,该脚本会将虚拟机加入域。但是,当脚本运行时,“Add-Computer”提示会在 read-host 之前运行,或者完全跳过:
param (
[String]$OU,
[PSCredential]$Credential
)
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\script2.txt -append
if ([Environment]::UserInteractive) {
if (!$OU) { $OU = Read-Host "Enter Tenant Resource Pool Name (exactly as appears in vCenter inventory)" }
if (!$Credential) { $Credential = Get-Credential -Message "Enter dev domain credentials" }
}
# Add Computer to Dev domain
try {
Add-Computer -DomainName dev.domain.com -OUPath "OU=$OU,dc=dev,dc=domain,dc=com" -ErrorAction stop -Credential $Credential
}
catch {
Write-Warning "Failed to join to domain."
Read-Host -Prompt "Press Enter to exit"
Throw $_
}
这显然会失败,因为域加入需要 -OUPath,但并没有提示。
如果我通过右键单击运行 script2.ps1,它会起作用。当我从另一个脚本内部运行它时,它会失败:
wget -O C:/temp/script1.ps1 https://script-source.com:8443/script1.ps1 | powershell C:\temp\script1.ps1
答案1
这似乎可以通过在脚本名称前面添加一个 & 符号来解决,而不是像我一样用 powershell 调用它:
powershell_stuff
..
..
& C:/temp/script2.ps1
..
..
morePowershell_stuff
来自这篇文章的想法:Powershell 脚本运行另一个 powershell 脚本并等到结束