在 Powershell Scriptblock 中调用 Restmethod URI 不起作用
Invoking Restmethod URI in Powershell Scriptblock not working
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 应该开始处理远程脚本并显示旋转等待符号,但失败了。
实际上问题出在 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
我认为您不需要 invoke-expression (),您可以删除它并重试吗?
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 应该开始处理远程脚本并显示旋转等待符号,但失败了。
实际上问题出在 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
我认为您不需要 invoke-expression (),您可以删除它并重试吗?