How do I 'run as' 'Network Service'?

前端 未结 5 1237
悲哀的现实
悲哀的现实 2020-12-04 07:47

I am trying to run a process as another account. I have the command:

runas \"/user:WIN-CLR8YU96CL5\\network service\" \"abwsx1.exe\"

but th

5条回答
  •  萌比男神i
    2020-12-04 08:32

    I know this is an old thread but it is the top result for this problem and I wanted to be able to run a command using PowerShell without having to install any additional tools on our Windows Server. I came up with the following PowerShell script that creates a scheduled task, runs it, and then deletes it. It is also written to allow you to run the command under different user accounts.

    function InstallDotNetCoreGlobalTool($PackageId, $Action = "install", $User = "NT AUTHORITY\NETWORK SERVICE", $Password = "")
    {
        $TaskName = "AzureDotNetCoreGlobalToolConfiguration"
        $Command = "dotnet.exe"
        $Arguments = "tool $Action -g " + $PackageId
        $TaskAction = New-ScheduledTaskAction -Execute $Command -Argument $Arguments
    
        Write-Host "Setting up scheduled task to run" $Command $Arguments
    
        Register-ScheduledTask -TaskName $TaskName -User $User -Action $TaskAction
        Start-ScheduledTask -TaskName $TaskName
    
        Write-Host ""
        Write-Host "Waiting on scheduled task to complete."
    
        while ((Get-ScheduledTask -TaskName $TaskName).State  -ne 'Ready') 
        {
          # keep waiting
        }
    
        Write-Host ""
    
        If((Get-ScheduledTask $TaskName | Get-ScheduledTaskInfo).LastTaskResult -eq 0)
        {
            Write-Host $PackageId $Action "completed successfully"
        }
        else
        {
            If ($Action -eq "install")
            {
                Write-Host $PackageId "failed to $Action. Ensure the proper dependencies have been installed or that it isn't already installed."
            }
            Else {
                Write-Host $PackageId "failed to $Action. It may not currently be installed."
            }        
        }
    
        Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
    }
    
    InstallDotNetCoreGlobalTool "Amazon.Lambda.Tools" "uninstall"
    

提交回复
热议问题