Welcome Forums General PowerShell Q&A Run scriptblock as different user on local machine

Viewing 2 reply threads
  • Author
    Posts
    • #169396
      Participant
      Topics: 1
      Replies: 0
      Points: -6
      Rank: Member

      I want to run a scriptblock ($commands) as a different user ($credential) on the same machine where the script runs.

      I tried “Invoke-Command -Credential $Credential -ScriptBlock {$commands}”, but it appears that you have to specify  -ComputerName when using -Credential.

      The system is firewalled and I am not allowed to enable PS-remoting.

      Is there a way to to accomplish this?

    • #169435
      Senior Moderator
      Topics: 8
      Replies: 1158
      Points: 4,045
      Helping Hand
      Rank: Community Hero

      You can use Start-Process and call PowerShell.exe with -credentials and arguments ‘-c &{your commands}’

      Or

      Use Start-Job with Credentials and -ScriptBlock parameter.

    • #169867
      Participant
      Topics: 1
      Replies: 63
      Points: 355
      Helping Hand
      Rank: Contributor

      you could use -cn $env:computername or  -cn localhost in the invoke-command statement.

      Just make sure that any variables you intend to pass are done as $using:<variable>

       

      $name=”bob” would be pased as $using:name and not just $name

       

       

Viewing 2 reply threads
  • The topic ‘Run scriptblock as different user on local machine’ is closed to new replies.