1

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
rumplesmyboy
  • 189
  • 1
  • 10
  • 1
    It also fails if I comment it out and try running it separately like so: wget -O C:/temp/script1.ps1 https://script-source.com:8443/script1.ps1 | powershell C:\temp\script1.ps1 | powershell C:\temp\script2.ps1 – rumplesmyboy Jun 16 '22 at 23:28

1 Answers1

1

This appears to be solved by adding an ampersand in front of the script name instead of calling it with powershell like I was doing:

powershell_stuff
..
..
& C:/temp/script2.ps1
..
..
morePowershell_stuff

Idea from this post: Powershell script to run another powershell script and wait till over

rumplesmyboy
  • 189
  • 1
  • 10