Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users/rok/rok m123 ps wfc fix #6941

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion Tasks/PowerShellOnTargetMachines/PowerShellJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,55 @@ param (
[String]$psOnRemoteScriptBlockString = "Invoke-PsOnRemote -MachineDnsName $fqdn -ScriptPath `$scriptPath -WinRMPort $port -Credential `$credential -ScriptArguments `$scriptArguments -InitializationScriptPath `$initializationScriptPath -SessionVariables `$parsedSessionVariables $skipCACheckOption $httpProtocolOption $enableDetailedLoggingOption"
[scriptblock]$psOnRemoteScriptBlock = [scriptblock]::Create($psOnRemoteScriptBlockString)
$deploymentResponse = Invoke-Command -ScriptBlock $psOnRemoteScriptBlock


# Telemetry data logic through ps session
try{
if($skipCACheckOption)
{
$sessionOption = New-PSSessionOption -SkipCACheck
}
else
{
$sessionOption = New-PSSessionOption
}
$secpasswd = ConvertTo-SecureString $credential.Password -AsPlainText -Force
$psCredential = New-Object System.Management.Automation.PSCredential($credential.UserName, $secpasswd)
if($httpProtocolOption -eq '-UseHttp')
{
$session = New-PSSession -Computer $fqdn -Port $port -Credential $psCredential -SessionOption ($sessionOption )
}
else
{
$session = New-PSSession -Computer $fqdn -Port $port -Credential $psCredential -SessionOption ($sessionOption ) -UseSSL
}
$VmUuidHash = Invoke-Command -Session $session -ScriptBlock {
$sha = New-Object System.Security.Cryptography.SHA512CryptoServiceProvider
$computerDetails = Get-WmiObject -class Win32_ComputerSystemProduct -namespace root\CIMV2
$encoding = [system.Text.Encoding]::ASCII
$uuidHash = [System.BitConverter]::ToString( $sha.ComputeHash($encoding.GetBytes($computerDetails.UUID)))
$uuidHash = $uuidHash -replace "-" , ""
return $uuidHash
}
$isAzureVm = Invoke-Command -Session $session -ScriptBlock {
(Get-Process -Name 'WindowsAzureGuestAgent' -ErrorAction Ignore) | Select-Object -First 1 | ForEach-Object {
if($_.Path)
{
return $true
}
else
{
return $false
}
}
}
$deploymentResponse | Add-Member "IsAzureVm" $IsAzureVm -Force
$deploymentResponse | Add-Member "VmUuidHash" $VmUuidHash -Force
}
catch
{
Write-Verbose "Error during fetching telemetry = $_"
$deploymentResponse | Add-Member "TelemetryError" $_ -Force
}

Write-Output $deploymentResponse
}
39 changes: 34 additions & 5 deletions Tasks/PowerShellOnTargetMachines/PowerShellOnTargetMachines.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ $ErrorActionPreference = 'Stop'
$deploymentOperation = 'Deployment'

$envOperationStatus = "Passed"
$jobId = $env:SYSTEM_JOBID;

# enabling detailed logging only when system.debug is true
$enableDetailedLoggingString = $env:system_debug
Expand All @@ -65,6 +66,36 @@ if ($enableDetailedLoggingString -ne "true")
# Telemetry
Import-Module $PSScriptRoot\ps_modules\TelemetryHelper

function Publish-AzureTelemetry
{
param([object] $deploymentResponse,
[string] $jobId )
if($deploymentResponse){
$jsonString = -join("{")
if([bool]($deploymentResponse.PSobject.Properties.name -match "IsAzureVm")){
$jsonString = -join( $jsonString,
"`"IsAzureVm`" : `"$($deploymentResponse.IsAzureVm)`"" ,
",")
}
if([bool]($deploymentResponse.PSobject.Properties.name -match "VmUuidHash")){
$jsonString = -join( $jsonString,
"`"VmUuidHash`" : `"$($deploymentResponse.VmUuidHash)`"",
",")
}
if([bool]($deploymentResponse.PSobject.Properties.name -match "TelemetryError")){
$jsonString = -join( $jsonString,
"`"TelemetryError`" : `"$($deploymentResponse.TelemetryError)`"",
",")
}

$jsonString = -join( $jsonString,
"`"JobId`" : `"$jobId`"" , "}")
}

$telemetryString ="##vso[telemetry.publish area=TaskHub;feature=PowerShellOnTargetMachines]$jsonString"
Write-Host $telemetryString
}

try
{
$connection = Get-VssConnection -TaskContext $distributedTaskContext
Expand Down Expand Up @@ -105,10 +136,10 @@ if($runPowershellInParallel -eq "false" -or ( $resources.Count -eq 1 ) )
$status = $deploymentResponse.Status

Write-Output (Get-LocalizedString -Key "Deployment status for machine '{0}' : '{1}'" -ArgumentList $displayName, $status)
Publish-AzureTelemetry -deploymentResponse $deploymentResponse -jobId $jobId

if ($status -ne "Passed")
{
Write-Telemetry "DTLSDK_Error" $deploymentResponse.DeploymentSummary
Write-Verbose $deploymentResponse.Error.ToString()
$errorMessage = $deploymentResponse.Error.Message
throw $errorMessage
Expand Down Expand Up @@ -145,6 +176,8 @@ else

Write-ResponseLogs -operationName $deploymentOperation -fqdn $displayName -deploymentResponse $output
Write-Output (Get-LocalizedString -Key "Deployment status for machine '{0}' : '{1}'" -ArgumentList $displayName, $status)
Publish-AzureTelemetry -deploymentResponse $output -jobId $jobId

if($status -ne "Passed")
{
$envOperationStatus = "Failed"
Expand All @@ -161,12 +194,8 @@ else
}
}
}

if($envOperationStatus -ne "Passed")
{
foreach ($error in $dtlsdkErrors) {
Write-Telemetry "DTLSDK_Error" $error
}

$errorMessage = (Get-LocalizedString -Key 'Deployment on one or more machines failed.')
throw $errorMessage
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PowerShellOnTargetMachines/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 47
"Patch": 51
},
"minimumAgentVersion": "1.104.0",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PowerShellOnTargetMachines/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 47
"Patch": 51
},
"minimumAgentVersion": "1.104.0",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/WindowsMachineFileCopy/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 41
"Patch": 43
},
"minimumAgentVersion": "1.104.0",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/WindowsMachineFileCopy/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 41
"Patch": 43
},
"minimumAgentVersion": "1.104.0",
"groups": [
Expand Down