Skip to content

"Run selection" does nothing #3078

Closed
Closed
@pilgi3

Description

@pilgi3

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions