Jumping around to certain spots in script

后端 未结 3 1906
别那么骄傲
别那么骄傲 2021-02-07 16:45

Is there a way to make a script jump to a specific spot like :GOTO in command prompt? I wanted to make the script jump to the beginning when it is ended.

$tag1 =         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 17:19

    Here's and example using your script:

    $GetInfo = {
      $tag1 = Read-Host 'Enter tag # or Q to quit'
      if ($tag1 -eq 'Q'){Return}
      cls
      sc.exe \\$tag1 start RemoteRegistry
      cls
      Start-Sleep -s 2
      cls
      systeminfo /S $tag1 | findstr /B /C:"OS Name" /C:"System Boot Time" /C:"System Up Time"
      Get-EventLog system -computername $tag1 -InstanceId 2147489657 -Newest 10 |
        ft EventID,TimeWritten,MachineName -AutoSize
      .$GetInfo
     }
    
    &$GetInfo
    

    Use . instead of & inside the script block to prevent it from walking up the call stack.

    Putting code into script blocks to be called later from arbitrary points in the script (emulating a GoTo) is functionally the same as using a function, and script block used in this manner are sometimes referred to as "anonymous functions".

提交回复
热议问题