run script block as a specific user with Powershell

后端 未结 3 517
南旧
南旧 2021-01-16 11:12

I am not getting anywhere when using Start-Process / Start-Job cmdlets with -Credential $cred

Problem

I have a service account use in deployment (unattended

相关标签:
3条回答
  • 2021-01-16 11:45

    I ended up enabling WinRM using WinRM quickconfig

    I was then able to use Invoke-Command

        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
    
    Invoke-Command {
        param(
            [string]$WebAppName 
        )
         #elevated command here
    
    } -comp $computerName -cred $cred  -ArgumentList @("$myWebAppId")
    
    0 讨论(0)
  • 2021-01-16 11:49

    Hi this might be an example that might work for you let me know if it does.

    $global:credentials = new-object -typename System.Management.Automation.PSCredential 
    
    
    $job = Start-Job -ScriptBlock {Get-Service} -Credential $credentials
    
    Wait-Job $job
    
    Receive-Job $job
    
    0 讨论(0)
  • 2021-01-16 11:49

    While there's no quick and easy way to do this in PowerShell 2.0, version 3.0 (currently in RC, mostly likely RTW very soon given that Windows 8 RTW will appear on MSDN/Technet tomorrow) supports the notion of configuring remoting endpoints with a custom identity. This would be done with the Register-PSSessionConfiguration cmdlet on the computer where you want the command to run, which may be the local computer. Then, when using Invoke-Command, provide a session with the -Session parameter. The session is created using the New-PSSession cmdlet, which lets you specify the computer and the configuration name (which is tied to the custom identity.)

    Clear as mud?

    0 讨论(0)
提交回复
热议问题