Start-Process passing credentials variable (Solved)

Hi,

I’m trying to use the cmdlet Start-Process using the ArgumentList to make a variable of the current session available on the new process.

I have tried using like this:

$ADCred = Get-Credential
$Path = ".\ManageRemoteComputers.ps1"
Start-Process PowerShell -Credential $ADcred -ArgumentList "-File $Path", $ADCred

And inside the ManageRemoteComputers.ps1 file, using this:

$CurrName = Read-Host "Type the name of the computer that you want to rename"
$NewName = Read-Host "Type the new name of the computer"
Invoke-Command -ComputerName $CurrentName -Credential $ADCred -ArgumentList $ADCred -ScriptBlock {
param ($ADCred)
Rename-Computer -ComputerName $CurrentName -NewName $NewName -Force -DomainCredential $ADCred -verbose
}

But then a new credential window pop-up…

Any ideas?

You need a param() block in the script itself to properly accept the credential variable, otherwise it will be stored in $args[0] in the script. Param() in the invoke-command block is separate and also required if you want to pass the parameter by name. Additionally, I wouldn’t use Start-Process for that; it’s more effective to just invoke the script directly and pass in the credential

Joel Sallow,

Thanks for the fast reply. As soon as I sent the message I was wondering why I’m using start-process… I’ll try again tomorrow. Really appreciate your help! :blush:

Joel Sallow,

I removed the “Start-Process” from the equation and it’s working as expected. Thanks for clarifying it to me!