Joining System to Domain with Workflow

Okay, I need an adult…

I’m attempting to write a multi-purpose script that will, among other things, rename a computer and join it to the domain. I’ve got multiple problems with the structure of the script, but here’s my most immediate issue:

The first thing I’m attempting to do is join the system to the domain. I’m trying to use workflow to perform this task so that I can (eventually) resume the script to do other things. Here’s the code:

Process {

        workflow Join-Domain {
            param (
            
            )
            
            # Functionality #1: Rename system & domain join
            InlineScript {
                Invoke-Command -ComputerName localhost -ScriptBlock {
                    Add-Computer -Credential (Get-Credential) -DomainName $args[0] -PSComputerName $args[1] -NewName $args[2] -ArgumentList $domainToJoin, $TempComputerName, $ComputerName
                    }
                }
            
            Restart-Computer -Wait -Timeout 30;
            
        }

        # Call workflow to join domain.
        Join-Domain;

When I run the script, I’m not prompted to enter credentials, and I get the following message:

Cannot process command because of one or more missing mandatory parameters: Credential.

I’ve tried creating a variable $CredsToUse and passing that in the -Credential switch, but I get the same error. I also attempted to pass it in as one of the $args, but I get a different, albeit similar, error:

Cannot process argument transformation on parameter ‘Credential’. A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials.

I’m running this with Powershell 5.1 on a Server 2016 VM.

I’m sure I’m missing some really basic concept, and I’ve Googled this voraciously but to no avail. Any help would be greatly appreciated.

Cheers,
Will

Well, based on your post, and what you say you are seeing. I’d edit your code this way.

    Process {

            workflow Join-Domain {
                param 
                (
                    [string]$AdminCreds,
                    [string]$DomainToJoin,
                    [string]$TempComputerName,
                    [string]$ComputerName
                )
            
                # Functionality #1: Rename system & domain join
                InlineScript {
                    Invoke-Command -ComputerName localhost -ScriptBlock {
                        Add-Computer -Credential (Get-Credential -Credential $AdminCreds) -DomainName $DomainToJoin -PSComputerName $TempComputerName -NewName $ComputerName
                        }
                    }
            
                Restart-Computer -Wait -Timeout 30;
            
            }

            # Call workflow to join domain.
            Join-Domain
    }

Workflows does not support iteraction with the user. You need to create the creds object beforehand (or pass it like an Input to the script block) and use it inside the inline script via the $using: statement.
For example:

Add-Computer -Credential $using:creds

Same goes for your other variables - you Need the $using: Statement to Access them.
More on the Topic : here.

Thanks for the responses. I’m currently reading over the Technet article regarding the $using: statement within workflows. I appreciate the guidance.

~Will~