Skip to content

Commit

Permalink
Merge pull request #116 from Microsoft/users/abchakra/PassMachineUser…
Browse files Browse the repository at this point in the history
…ToAgent

Pass machine user to agent
  • Loading branch information
abchakra25 committed May 11, 2015
2 parents 2658af7 + 16c4227 commit 6637996
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions Tasks/DeployVisualStudioTestAgent/TestAgentConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,15 @@ function Set-TestAgentConfiguration
[String] $TfsCollection,
[ValidateSet("Service", "Process")]
[String] $AsServiceOrProcess,
[System.Management.Automation.PSCredential] $UserCredential,
[System.Management.Automation.PSCredential] $MachineUserCredential,
[Bool] $DisableScreenSaver = $true,
[Bool] $EnableAutoLogon = $false,
[String] $TestAgentVersion,
[String] $PersonalAccessToken,
[String] $EnvironmentUrl,
[String] $MachineName,
[String] $Capabilities
[String] $Capabilities,
[System.Management.Automation.PSCredential] $AgentUserCredential
)

switch ($AsServiceOrProcess)
Expand All @@ -193,10 +194,16 @@ function Set-TestAgentConfiguration

$configArgs = $configArgs + ("/tfsTeamProjectCollection:{0}" -f $TfsCollection)

if ($PSBoundParameters.ContainsKey('UserCredential') -and $UserCredential)
if ($PSBoundParameters.ContainsKey('AgentUserCredential') -and $AgentUserCredential)
{
$configArgs = $configArgs + ("/userName:{0}" -f $UserCredential.UserName)
$configArgs = $configArgs + ("/password:{0}" -f $UserCredential.GetNetworkCredential().Password)
$configArgs = $configArgs + ("/userName:{0}" -f $AgentUserCredential.UserName)
$configArgs = $configArgs + ("/password:{0}" -f $AgentUserCredential.GetNetworkCredential().Password)
}

if ($PSBoundParameters.ContainsKey('MachineUserCredential') -and $MachineUserCredential)
{
$configArgs = $configArgs + ("/adminUserName:{0}" -f $MachineUserCredential.UserName)
$configArgs = $configArgs + ("/adminPassword:{0}" -f $MachineUserCredential.GetNetworkCredential().Password)
}

if ($PSBoundParameters.ContainsKey('EnableAutoLogon'))
Expand Down Expand Up @@ -237,7 +244,8 @@ function Set-TestAgentConfiguration
{
$configArgs = $configArgs + ("/Capabilities:{0}" -f $Capabilities)
}
$configOut = InvokeTestAgentConfigExe -Arguments $configArgs -Version $TestAgentVersion -UserCredential $UserCredential

$configOut = InvokeTestAgentConfigExe -Arguments $configArgs -Version $TestAgentVersion -UserCredential $MachineUserCredential

# 3010 is exit code to indicate a reboot is required
if ($configOut.ExitCode -eq 3010)
Expand Down Expand Up @@ -307,9 +315,9 @@ function LoadDependentDlls

$vsRoot = Locate-TestVersionAndVsRoot($TestAgentVersion)
$assemblylist =
(Join-Path -Path $vsRoot -ChildPath "PrivateAssemblies\TestAgent\Microsoft.TeamFoundation.Client.dll").ToString(),
(Join-Path -Path $vsRoot -ChildPath "PrivateAssemblies\TestAgent\Microsoft.TeamFoundation.Common.dll").ToString(),
(Join-Path -Path $vsRoot -ChildPath "PrivateAssemblies\TestAgent\Microsoft.VisualStudio.Services.Common.dll").ToString(),
(Join-Path -Path $vsRoot -ChildPath "TestAgent\Microsoft.TeamFoundation.Client.dll").ToString(),
(Join-Path -Path $vsRoot -ChildPath "TestAgent\Microsoft.TeamFoundation.Common.dll").ToString(),
(Join-Path -Path $vsRoot -ChildPath "TestAgent\Microsoft.VisualStudio.Services.Common.dll").ToString(),
(Join-Path -Path $vsRoot -ChildPath "PrivateAssemblies\Microsoft.VisualStudio.TestService.Common.dll").ToString()

foreach ($asm in $assemblylist)
Expand All @@ -328,7 +336,7 @@ function ReadCredentials
)

LoadDependentDlls($TestAgentVersion)
$creds = [Microsoft.VisualStudio.TestService.AgentExecutionHost.CredentialStoreHelper]::GetStoredCredential($TFSCollectionUrl)
$creds = [Microsoft.VisualStudio.TestService.Common.CredentialStoreHelper]::GetStoredCredential($TFSCollectionUrl)

return $creds
}
Expand All @@ -341,18 +349,19 @@ function CanSkipTestAgentConfiguration
[String] $TfsCollection,
[ValidateSet("Service", "Process")]
[String] $AsServiceOrProcess,
[System.Management.Automation.PSCredential] $UserCredential,
[System.Management.Automation.PSCredential] $MachineUserCredential,
[Bool] $DisableScreenSaver,
[Bool] $EnableAutoLogon,
[String] $TestAgentVersion,
[String] $EnvironmentUrl,
[String] $MachineName,
[String] $PersonalAccessToken,
[String] $Capabilities
[String] $Capabilities,
[System.Management.Automation.PSCredential] $AgentUserCredential
)

Write-Verbose -Message "Finding whether TestAgent configuration is required" -Verbose
$existingConfiguration = Get-TestAgentConfiguration -TestAgentVersion $TestAgentVersion -UserCredential $UserCredential
$existingConfiguration = Get-TestAgentConfiguration -TestAgentVersion $TestAgentVersion -UserCredential $MachineUserCredential

if (-not (IsDtaExecutionHostRunning))
{
Expand Down Expand Up @@ -421,9 +430,9 @@ function CanSkipTestAgentConfiguration

if ($PSBoundParameters.ContainsKey('UserCredential'))
{
if ($UserCredential.UserName -ne $existingConfiguration.UserName)
if ($AgentUserCredential.UserName -ne $existingConfiguration.UserName)
{
Write-Verbose -Message ("UserName mismatch. Expected : {0}, Current {1}. Reconfiguration required." -f $UserCredential.UserName, $existingConfiguration.UserName) -Verbose
Write-Verbose -Message ("UserName mismatch. Expected : {0}, Current {1}. Reconfiguration required." -f $AgentUserCredential.UserName, $existingConfiguration.UserName) -Verbose
return $false
}
}
Expand Down Expand Up @@ -600,26 +609,27 @@ function ConfigureTestAgent
[String] $TfsCollection,
[ValidateSet("Service", "Process")]
[String] $AsServiceOrProcess,
[System.Management.Automation.PSCredential] $UserCredential,
[System.Management.Automation.PSCredential] $MachineUserCredential,
[Bool] $DisableScreenSaver = $false,
[Bool] $EnableAutoLogon = $false,
[String] $TestAgentVersion = "14.0",
[String] $EnvironmentUrl,
[String] $PersonalAccessToken,
[String] $MachineName,
[String] $Capabilities
[String] $Capabilities,
[System.Management.Automation.PSCredential] $AgentUserCredential
)

EnableTracing -TestAgentVersion $TestAgentVersion

$ret = -1
if ($AsServiceOrProcess -eq "Service")
{
$ret = Set-TestAgentConfiguration -TfsCollection $TfsCollection -AsServiceOrProcess $AsServiceOrProcess -UserCredential $UserCredential -TestAgentVersion $TestAgentVersion -EnvironmentUrl $EnvironmentUrl -PersonalAccessToken $PersonalAccessToken -MachineName $MachineName -Capabilities $Capabilities
$ret = Set-TestAgentConfiguration -TfsCollection $TfsCollection -AsServiceOrProcess $AsServiceOrProcess -UserCredential $UserCredential -TestAgentVersion $TestAgentVersion -EnvironmentUrl $EnvironmentUrl -PersonalAccessToken $PersonalAccessToken -MachineName $MachineName -Capabilities $Capabilities -AgentUserCredential $AgentUserCredential
}
else
{
$ret = Set-TestAgentConfiguration -TfsCollection $TfsCollection -AsServiceOrProcess $AsServiceOrProcess -UserCredential $UserCredential -DisableScreenSaver $DisableScreenSaver -EnableAutoLogon $EnableAutoLogon -TestAgentVersion $TestAgentVersion -EnvironmentUrl $EnvironmentUrl -PersonalAccessToken $PersonalAccessToken -MachineName $MachineName -Capabilities $Capabilities
$ret = Set-TestAgentConfiguration -TfsCollection $TfsCollection -AsServiceOrProcess $AsServiceOrProcess -UserCredential $UserCredential -DisableScreenSaver $DisableScreenSaver -EnableAutoLogon $EnableAutoLogon -TestAgentVersion $TestAgentVersion -EnvironmentUrl $EnvironmentUrl -PersonalAccessToken $PersonalAccessToken -MachineName $MachineName -Capabilities $Capabilities -AgentUserCredential $AgentUserCredential
}

if ($ret -eq 0)
Expand All @@ -646,10 +656,11 @@ function SetRebootKey
$disableScreenSaver = [Boolean] $disableScreenSaver
$enableAutoLogon = [Boolean] $enableAutoLogon

$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, (ConvertTo-SecureString -String $password -AsPlainText -Force)
$machineCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, (ConvertTo-SecureString -String $password -AsPlainText -Force)
$agentCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $testAgentConfigUserName, (ConvertTo-SecureString -String $testAgentConfigPassword -AsPlainText -Force)

$ret = CanSkipTestAgentConfiguration -TfsCollection $tfsCollectionUrl -AsServiceOrProcess $asServiceOrProcess -EnvironmentUrl $environmentUrl -MachineName $machineName -UserCredential $Credential -DisableScreenSaver $disableScreenSaver -EnableAutoLogon $enableAutoLogon -PersonalAccessToken $PersonalAccessToken -Capabilities $capabilities
$ret = CanSkipTestAgentConfiguration -TfsCollection $tfsCollectionUrl -AsServiceOrProcess $asServiceOrProcess -EnvironmentUrl $environmentUrl -MachineName $machineName -UserCredential $machineCredential -DisableScreenSaver $disableScreenSaver -EnableAutoLogon $enableAutoLogon -PersonalAccessToken $PersonalAccessToken -Capabilities $capabilities -AgentUserCredential $agentCredential
if ($ret -eq $false)
{
ConfigureTestAgent -TfsCollection $tfsCollectionUrl -AsServiceOrProcess $asServiceOrProcess -EnvironmentUrl $environmentUrl -MachineName $machineName -UserCredential $Credential -DisableScreenSaver $disableScreenSaver -EnableAutoLogon $enableAutoLogon -PersonalAccessToken $PersonalAccessToken -Capabilities $capabilities
ConfigureTestAgent -TfsCollection $tfsCollectionUrl -AsServiceOrProcess $asServiceOrProcess -EnvironmentUrl $environmentUrl -MachineName $machineName -MachineUserCredential $machineCredential -DisableScreenSaver $disableScreenSaver -EnableAutoLogon $enableAutoLogon -PersonalAccessToken $PersonalAccessToken -Capabilities $capabilities -AgentUserCredential $agentCredential
}

0 comments on commit 6637996

Please sign in to comment.