I'm calling a script inside another script:
...
...
powershell C:/temp/script2.ps1"
...
...
That joins a VM to a domain after downloading some things. However, when the script runs, the prompt for "Add-Computer" runs before read-host or it's skipped entirely:
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 $_
}
This obviously fails because the domain join requires -OUPath which isn't prompting.
If I run script2.ps1 by right-clicking, it works. When I run it from inside the other script like so, it fails:
wget -O C:/temp/script1.ps1 https://script-source.com:8443/script1.ps1 | powershell C:\temp\script1.ps1