Вызов Restmethod URI в скриптовом блоке Powershell не работает - PullRequest
1 голос
/ 28 февраля 2020

Кажется, есть проблема со Scriptblock, которая заставляет его не обрабатываться, и я думаю, что это может быть простой синтаксис:

        function start-wait4 {
        $scroll = "/-\|/-\|"
        $idx = 0
        $job = Invoke-Command -ComputerName $env:ComputerName -ScriptBlock { Invoke-Expression (Invoke-RestMethod -Uri "https://webaddresswherecodeishosted") } -AsJob

        $origpos = $host.UI.RawUI.CursorPosition
        $origpos.Y += 1

        while (($job.State -eq "Running") -and ($job.State -ne "NotStarted")) {
            $host.UI.RawUI.CursorPosition = $origpos
            Write-Host $scroll[$idx] -NoNewline
            $idx++
            if ($idx -ge $scroll.Length) {
                $idx = 0
            }
            Start-Sleep -Milliseconds 100
        }
        # It's over - clear the activity indicator.
        $host.UI.RawUI.CursorPosition = $origpos
        Write-Host ' '
    }
    start-wait4

Start-wait4 должен начать обработку удаленного сценария и отобразить символ ожидания вращения, но он не работает.

Ответы [ 2 ]

0 голосов
/ 28 февраля 2020

Не думаю, что вам нужно invoke-expression (), вы можете удалить это и попробовать еще раз?

0 голосов
/ 28 февраля 2020

На самом деле проблема была в части InvokeCommand, которую нужно было изменить на Start-Job:

      function start-wait4 {
        $scroll = "/-\|/-\|"
        $idx = 0
        $job = Start-Job -ScriptBlock { Invoke-Expression (Invoke-RestMethod -Uri "https://webaddresswithremotecode") } 

        $origpos = $host.UI.RawUI.CursorPosition
        $origpos.Y += 1

        while (($job.State -eq "Running") -and ($job.State -ne "NotStarted")) {
            $host.UI.RawUI.CursorPosition = $origpos
            Write-Host $scroll[$idx] -NoNewline
            $idx++
            if ($idx -ge $scroll.Length) {
                $idx = 0
            }
            Start-Sleep -Milliseconds 100
        }
        # It's over - clear the activity indicator.
        $host.UI.RawUI.CursorPosition = $origpos
        Write-Host ' '
    }
    start-wait4
...