简体   繁体   中英

How to avoid running Visual studio or Test Agent as administrator

I have some code to start my application services, which I call before the start of my automated test cases. But I have to run either visual studio or test agent as administrator. Is there any other way to run it?

//My method    
 //Code to start stopped Pfx services


                ServiceController PfxService = new ServiceController();
                string[] PfxServiceList = { "******.SocketService", "******.WcfServices", "MSSQL*****" };

                foreach (string ServiceName in PfxServiceList)
                {
                    PfxService.ServiceName = ServiceName;
                    string ServiceStatus = PfxService.Status.ToString();
                    if (ServiceStatus == "Stopped")
                    {
                        PfxService.Start();
                        PfxService.WaitForStatus(ServiceControllerStatus.Running);
                        Playback.Wait(2000);
                    }
                }

I have created this powershell script to start services, i trigger this piece of code through task scheduler. Which i run at intervals. But at times it doesn't meet the requirement.

$Password = "Enter_Your_Password"
$UserAccount = "Enter_Your_AccountInfor"
$MachineName = "Enter_Your_Machine_Name"
$ServiceList = @("*****.SocketService","*****.WcfServices","*****DesktopService","*****Service")
$PasswordSecure = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $UserAccount, $PasswordSecure 

$LogStartTime = Get-Date -Format "MM-dd-yyyy hh:mm:ss tt"
$FileDateTimeStamp = Get-Date -Format "MM-dd-yyyy_hh"
$LogFileName = "C:\Users\krakhil\Desktop\Powershell\Logs\StartService_$FileDateTimeStamp.txt" 


#code to start the service

"`n####################################################################" > $LogFileName
"####################################################################" >> $LogFileName
"######################  STARTING SERVICE  ##########################" >> $LogFileName

for($i=0;$i -le 3; $i++)
{
"`n`n" >> $LogFileName
$ServiceName = $ServiceList[$i]
"$LogStartTime => Service Name: $ServiceName" >> $LogFileName

Write-Output "`n####################################"
Write-Output "Starting Service - " $ServiceList[$i]

"$LogStartTime => Starting Service: $ServiceName" >> $LogFileName
Start-Service $ServiceList[$i]

$ServiceState = Get-Service | Where-Object {$_.Name -eq $ServiceList[$i]}

if($ServiceState.Status -eq "Running")
{
"$LogStartTime => Started Service Successfully: $ServiceName" >> $LogFileName
Write-Host "`n Service " $ServiceList[$i] " Started Successfully"
}
else
{
"$LogStartTime => Unable to Stop Service: $ServiceName" >> $LogFileName
Write-Output "`n Service didn't Start. Current State is - "
Write-Host $ServiceState.Status
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM