Start-Process passing credentials variable (Solved)

Welcome Forums General PowerShell Q&A Start-Process passing credentials variable (Solved)

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #112786

    Participant
    Points: 0
    Rank: Member

    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?

  • #112789

    Participant
    Points: 0
    Rank: Member

    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

    • #112792

      Participant
      Points: 0
      Rank: Member

      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! 😊

    • #112825

      Participant
      Points: 0
      Rank: Member

      Joel Sallow,

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

You must be logged in to reply to this topic.