vscode-powershell: "Run selection" does nothing

Issue Type: Bug

I have an open Workspace in Visual Code and select a specific PowerShell Script. Then I select the function incl. “get-teamviewerdevices” to run the function. Right click and select “Run selection” or hitting F8 to test the function does nothing.

My next step was to close the Workspace and open the same PowerShell Script on the same place with a right click “Open in Code” and try again. Now it works as expected?! Any ideas or suggestions?

[string]$accessToken = "<token>"
[string]$tvApiVersion = '<apiversion>'
[string]$tvApiBaseUrl = '<URL>'


function Get-TeamViewerDevices {

    [CmdletBinding(DefaultParameterSetName = "Policy", SupportsShouldProcess = $true)]
    param(
    
        [Parameter(ParameterSetName = 'Policy', Mandatory = $false)]
        [ValidateSet('None', 'Inherit')]
        [string] $Policy = 'Inherit',

        [Parameter(ParameterSetName = 'SpecificPolicy', Mandatory = $true)]
        [string] $PolicyId

    )
    function ConvertTo-TeamViewerRestError {
        param([parameter(ValueFromPipeline)]$err)
        try { return ($err | Out-String | ConvertFrom-Json) }
        catch { return $err }
    }

    function Invoke-TeamViewerRestMethod {
        # Using `Invoke-WebRequest` instead of `Invoke-RestMethod`:
        # There is a known issue for PUT and DELETE operations to hang on Windows Server 2012.
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
        try { return ((Invoke-WebRequest -UseBasicParsing @args).Content | ConvertFrom-Json) }
        catch [System.Net.WebException] {
            $stream = $_.Exception.Response.GetResponseStream()
            $reader = New-Object System.IO.StreamReader($stream)
            $reader.BaseStream.Position = 0
            Throw ($reader.ReadToEnd() | ConvertTo-TeamViewerRestError)
        }
    }
    function Get-TeamViewerDevice($accessToken, $groupId) {
        if ($groupId) { $body = @{ groupid = $groupId } }
        return Invoke-TeamViewerRestMethod -Uri "$tvApiBaseUrl/api/$tvApiVersion/devices" `
            -Method Get -Headers @{authorization = "Bearer $accessToken"} -Body $body
    }
    
    try {			
        
        $script:devices = (Get-TeamViewerDevice -accessToken $accessToken)
        $script:currentdevice = $devices.devices -match $env:COMPUTERNAME
        
    } catch {
                    
        Write-Host "No connection with >URL< possible!"
         
    } 
                            
}

get-teamviewerdevices

#$assignedFalse = $devices.devices | Select-Object -Property Alias, last_seen, assigned_to | where-object {$_.assigned_to -like "*false*"}
#$lastSeen = $devices.devices | Select-Object -Property Alias, last_seen, assigned_to
$online = $devices.devices | Select-Object -Property Alias, groupid, Online_state, assigned_to | Where-Object {$_.online_state -like "*online*" -and $_.assigned_to -like "*false*"}

foreach ($client in $online) {

    $GroupID = $client.groupid
    if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
        
        if (Test-Path -Path ${env:ProgramFiles(x86)}\TeamViewer\Teamviewer.exe) {
            
            Invoke-Command -ComputerName $client.alias -ScriptBlock {Start-Process -FilePath ${env:ProgramFiles(x86)}\TeamViewer\TeamViewer.exe -ArgumentList "assign --api-token >token< --group-id $GroupID";Write-Host "Der Client"$client.alias"in der Gruppe"$GroupID" wurde neu zugewiesen.[64]"}
        }
        
    } else {
        
        if (Test-Path -Path $env:ProgramFiles\Teamviewer\TeamViewer.exe) {
    
            Invoke-Command -ComputerName $client.alias -ScriptBlock {Start-Process -FilePath $env:ProgramFiles\TeamViewer\TeamViewer.exe -ArgumentList "assign --api-token >token< --group-id $GroupID";Write-Host "Der Client"$client.alias"in der Gruppe"$GroupID" wurde neu zugewiesen.[32]"}
        }
    
        
    }
}

Extension version: 2020.6.0 VS Code version: Code 1.51.1 (e5a624b788d92b8d34d1392e4c4d9789406efe8f, 2020-11-10T23:34:32.027Z) OS version: Windows_NT x64 10.0.18363

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 26 (2 by maintainers)

Most upvoted comments

@pilgi3 the preview has a complete rewrite of the core pipelining engine to help make things faster and more stable. The core of it is stable but there may be many new edge cases here and there we are working to smooth over as a result of such a huge change.

Can you provide some more specifics? Run Selection (F8) works just fine for me on selecting a line in a script and having it run in the PSIC at the bottom. (On my computer I have it bound to alt-enter but it’s the same process)

https://user-images.githubusercontent.com/15258962/152807129-9572ebf4-1472-4405-89c9-f9861092c5ae.mp4

Yes the log files are from an affected device. I will do again and provide the payload logs as well!

I have reproduce the issue right now. with_workspace_logs.zip without_workspace_logs.zip The zip file “without_workspace” contains the log, when I open the ps1 file alone without an open workspace (F8 works) The other zip file contains the logfiles with open workspace and open the ps1 file right in the workspace (F8 does nothing).

I have done this on my own device. Usually I am working with Visual Studio Code on a Server 2016 but could not find the logs, because there is no .vscode in the user profile 😕 . But on my own windows device there I have the same issue no worry!