I need to use Invoke-Command to execute Powershell script blocks on remote machines that are on/off the domain.
I have something like the following below:
Invoke-Command -ComputerName $targetServer -ArgumentList @($selectedProjects, $targetURI, $buildRetention) -ScriptBlock $cleanupSB -Credential $PSCred
The property "-Credential $PSCred" is only needed when I want to execute on a machine off the domain, and so I'd like to dynamically construct the properties that are used with "Invoke-Command"
I tried to create an array such as:
$prop = @("-Credential", "`$PSCred")
Invoke-Command -ComputerName $targetServer -ArgumentList @($selectedProjects, $targetURI, $buildRetention) -ScriptBlock $cleanupSB $($prop)
However, this is not interpreted as I intended. Is there a I can dynamically create these arguments and use them with Invoke-Command? Thanks!
If(Machine is on domain){Invoke-Command}Else{Invoke-Command with creds}
? – TheMadTechnician