Skip to content

Commit

Permalink
remove usage of net exe (#6863)
Browse files Browse the repository at this point in the history
* remove usage of net exe

* log robocopy and new-psdrive error messages

* verify username before creating a credential object out of it

* change order

* minor changes

* always try to create directory

* run robocopy script job with erroractionpreference set to Stop

* minor change

* removing error redirection
  • Loading branch information
arjgupta authored Apr 10, 2018
1 parent 85e55f3 commit a213915
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 74 deletions.
97 changes: 30 additions & 67 deletions Tasks/WindowsMachineFileCopy/RoboCopyJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ param (
[string]$additionalArguments,
[string]$scriptRoot
)

Import-Module "$scriptRoot\ps_modules\VstsTaskSdk"
Import-VstsLocStrings -LiteralPath $scriptRoot/Task.json

Expand All @@ -21,8 +20,7 @@ param (
Write-Verbose "additionalArguments = $additionalArguments"

$sourcePath = $sourcePath.Trim().TrimEnd('\', '/')
$targetPath = $targetPath.Trim().TrimEnd('\', '/')

$targetPath = $targetPath.Trim().TrimEnd('\', '/')
$isFileCopy = Test-Path -Path $sourcePath -PathType Leaf
$doCleanUp = $cleanTargetBeforeCopy -eq "true"

Expand Down Expand Up @@ -158,25 +156,11 @@ param (

return ""
}

function Get-NetExeCommand
{
$netExePath = Join-Path -path (get-item env:\windir).value -ChildPath system32\net.exe
if(Test-Path $netExePath)
{
Write-Verbose "Found the net exe path $netExePath. Net command will be $netExePath"
return $netExePath
}

Write-Verbose "Unable to get the path for net.exe. Net command will be 'net'"
return 'net'
}

function Create-DestinationDirectory(
[string]$path
)
{
$psCredentialObject = New-Object PSCredential($credential.UserName, (ConvertTo-SecureString $credential.Password -AsPlainText -Force))
$destPath = $path
$foundParentPath = $false
$isRoot = $false
Expand All @@ -186,11 +170,11 @@ param (
{
try
{
New-PSDrive -Name WFCPSDrive -PSProvider FileSystem -Root $destPath -Credential $psCredentialObject
New-PSDrive -Name WFCPSDrive -PSProvider FileSystem -Root $destPath -Credential $psCredentialObject -ErrorAction 'Stop'
$foundParentPath = $true
Write-Verbose "Found parent path"
$relativePath = $path.Substring($destPath.Length)
New-Item -ItemType Directory WFCPSDrive:$relativePath -Force
New-Item -ItemType Directory WFCPSDrive:$relativePath -ErrorAction 'Stop' -Force
Write-Verbose "Created directory"
}
catch
Expand Down Expand Up @@ -218,69 +202,48 @@ param (
}
}
}


Validate-Credential $credential
$userName = Get-DownLevelLogonName -fqdn $fqdn -userName $($credential.UserName)
$password = $($credential.Password)
$psCredentialObject = New-Object pscredential -ArgumentList $userName, (ConvertTo-SecureString -String $password -AsPlainText -Force)

$machineShare = Get-MachineShare -fqdn $fqdn -targetPath $targetPath
$destinationNetworkPath = Get-DestinationNetworkPath -targetPath $targetPath -machineShare $machineShare

Write-Verbose "machine share= $machineShare"
Write-Verbose "destination network path= $destinationNetworkPath"

Validate-Credential $credential
$userName = Get-DownLevelLogonName -fqdn $fqdn -userName $($credential.UserName)
$password = $($credential.Password)

if([bool]([uri]$targetPath).IsUnc)
{
Create-DestinationDirectory -path $destinationNetworkPath
}

$netExeCommand = Get-NetExeCommand
Create-DestinationDirectory -path $destinationNetworkPath

if($machineShare)
{
$command = "$netExeCommand use `"$machineShare`""
if($userName)
{
$command += " /user:`'$userName`' `'$($password -replace "['`]", '$&$&')`'"
}
$command += " 2>&1"

$dtl_mapOut = iex $command
if ($LASTEXITCODE -ne 0)
{
$errorMessage = (Get-VstsLocString -Key "WFC_FailedToConnectToPathWithUser" -ArgumentList $machineShare, $($credential.UserName)) + $dtl_mapOut
ThrowError -errorMessage $errorMessage -fqdn $fqdn
try {
New-PSDrive -Name "WFCPSDrive" -PSProvider FileSystem -Root $destinationNetworkPath -Credential $psCredentialObject -ErrorAction 'Stop'
} catch {
Write-VstsTaskError -Message (Get-VstsLocString -Key "WFC_FailedToCreatePSDrive" -ArgumentList $destinationNetworkPath, $($_.Exception.Message)) -ErrCode "WFC_FailedToCreatePSDrive"
throw
}
}

try
if($doCleanUp)
{
if($doCleanUp)
{
Clean-Target
}
Clean-Target
}

$robocopyParameters = Get-RoboCopyParameters -additionalArguments $additionalArguments -fileCopy:$isFileCopy
$robocopyParameters = Get-RoboCopyParameters -additionalArguments $additionalArguments -fileCopy:$isFileCopy

$command = "robocopy `"$sourceDirectory`" `"$destinationNetworkPath`" `"$filesToCopy`" $robocopyParameters"
Invoke-Expression $command

if ($LASTEXITCODE -ge 8)
{
$errorMessage = Get-VstsLocString -Key "WFC_CopyingFailedConsultRobocopyLogsForMoreDetails"
ThrowError -errorMessage $errorMessage -fqdn $fqdn
}
else
{
$message = (Get-VstsLocString -Key "WFC_CopyingRecurivelyFrom0to1MachineSucceed" -ArgumentList $sourcePath, $targetPath, $fqdn)
Write-Output $message
}
}
finally
$command = "robocopy `"$sourceDirectory`" `"$destinationNetworkPath`" `"$filesToCopy`" $robocopyParameters"
Invoke-Expression $command

if ($LASTEXITCODE -ge 8)
{
if($machineShare)
{
$dtl_deleteMap = iex "$netExeCommand use `"$machineShare`" /D /Y";
}
$errorMessage = Get-VstsLocString -Key "WFC_CopyingFailedConsultRobocopyLogsForMoreDetails"
ThrowError -errorMessage $errorMessage -fqdn $fqdn
}
else
{
$message = (Get-VstsLocString -Key "WFC_CopyingRecurivelyFrom0to1MachineSucceed" -ArgumentList $sourcePath, $targetPath, $fqdn)
Write-Output $message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"loc.messages.WFC_CopyStartedFor0": "Copy started for - {0}",
"loc.messages.WFC_NoMachineExistsUnderEnvironment": "No machine exists under environment: '{0}' for deployment",
"loc.messages.WFC_NoMachineExistsUnderEnvironment0ForDeployment": "No machine exists under environment: '{0}' for deployment",
"loc.messages.WFC_CopyingFailedForResource": "Copying failed for resource : {0}"
"loc.messages.WFC_CopyingFailedForResource": "Copying failed for resource : {0}",
"loc.messages.WFC_FailedToCreatePSDrive": "Failed to Create PSDrive with Destination: '{0}', ErrorMessage: '{1}'",
"loc.messages.WFC_RobocopyError": "Robocopy Error. Computer: '{0}'. ErrorMessage: '{1}'"
}
8 changes: 5 additions & 3 deletions Tasks/WindowsMachineFileCopy/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 0,
"Patch": 4
"Minor": 1,
"Patch": 0
},
"releaseNotes": "What's new in Version 2.0: <br/>&nbsp;&nbsp;Proxy support is being added. <br/>&nbsp;&nbsp; Removed support of legacy DTL machines.",
"minimumAgentVersion": "1.104.0",
Expand Down Expand Up @@ -109,6 +109,8 @@
"WFC_CopyStartedFor0": "Copy started for - {0}",
"WFC_NoMachineExistsUnderEnvironment": "No machine exists under environment: '{0}' for deployment",
"WFC_NoMachineExistsUnderEnvironment0ForDeployment": "No machine exists under environment: '{0}' for deployment",
"WFC_CopyingFailedForResource": "Copying failed for resource : {0}"
"WFC_CopyingFailedForResource": "Copying failed for resource : {0}",
"WFC_FailedToCreatePSDrive": "Failed to Create PSDrive with Destination: '{0}', ErrorMessage: '{1}'",
"WFC_RobocopyError": "Robocopy Error. Computer: '{0}'. ErrorMessage: '{1}'"
}
}
8 changes: 5 additions & 3 deletions Tasks/WindowsMachineFileCopy/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 0,
"Patch": 4
"Minor": 1,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "1.104.0",
Expand Down Expand Up @@ -109,6 +109,8 @@
"WFC_CopyStartedFor0": "ms-resource:loc.messages.WFC_CopyStartedFor0",
"WFC_NoMachineExistsUnderEnvironment": "ms-resource:loc.messages.WFC_NoMachineExistsUnderEnvironment",
"WFC_NoMachineExistsUnderEnvironment0ForDeployment": "ms-resource:loc.messages.WFC_NoMachineExistsUnderEnvironment0ForDeployment",
"WFC_CopyingFailedForResource": "ms-resource:loc.messages.WFC_CopyingFailedForResource"
"WFC_CopyingFailedForResource": "ms-resource:loc.messages.WFC_CopyingFailedForResource",
"WFC_FailedToCreatePSDrive": "ms-resource:loc.messages.WFC_FailedToCreatePSDrive",
"WFC_RobocopyError": "ms-resource:loc.messages.WFC_RobocopyError"
}
}

0 comments on commit a213915

Please sign in to comment.