為什麼這個 New-Item 指令可以在 powershell 中運作,但不能在 ps1 腳本中運作?

為什麼這個 New-Item 指令可以在 powershell 中運作,但不能在 ps1 腳本中運作?

完全卡在這裡,我不知道該怎麼做:我有一個多選交互式Powershell 腳本,我正在為操作人員組合在一起,這樣每當他們想要在遠端電腦上執行快速任務時,他們就可以節省幾分鐘的時間。

我已經用 just 替換了所有其他選擇的所有程式碼You have selected #,所以我不會讓任何人對腳本內容的其餘部分感到厭煩,但選擇6讓我發瘋。

這就是我的目標,但還無法超越第一個要點:

  • 建立新.vnc文件
  • 將標準 VNC 協定文字正文新增至文件中
  • 添加$ComputerNameHost=
  • 啟動文件

問題是下面根本不會建立文件;如果直接複製/貼上到 Powershell 中,它可以工作,但不會在腳本中運行!有人知道為什麼會這樣嗎?

$commands = {
  function Show-Menu
  {
    param (
      [string]$Title = 'My Menu'
    )
    Clear-Host
    Write-Host "================ $Title ================"

    Write-Host "1: Press '1' (Description)."
    Write-Host "2: Press '2' (Description)."
    Write-Host "3: Press '3' (Description)."
    Write-Host "4: Press '4' (Description)."
    Write-Host "5: Press '5' (Description)."
    Write-Host "6: Press '6' To start a VNC Connection."
    Write-Host "Q: Press 'Q' to quit."
  }

  Show-Menu –Title 'My Menu'
  $selection = Read-Host "Please make a selection"
  switch ($selection)
  {
    '1' {
      "You have selected 1"
      sleep -seconds 2
    } '2' {
      "You have selected 2"
      sleep -seconds 2
    } '3' {
      "You have selected 3"
      sleep -seconds 2
    } '4' {
      "You have selected 4"
      sleep -seconds 2
    } '5' {
      "You have selected 5"
      sleep -seconds 2
    } '6' {
      $ComputerName = (Read-Host "ComputerName")
      {
        New-Item -Path "C:\Windows\Temp\$ComputerName.VNC"
        Set-Content "C:\Windows\Temp\$ComputerName.VNC" '
        [connection]
        host=$ComputerName
        port=5900
        [options]
        use_encoding_1=1
        copyrect=1
        viewonly=0
        fullscreen=0
        8bit=0
        shared=1
        belldeiconify=0
        disableclipboard=0
        swapmouse=0
        fitwindow=0
        cursorshape=1
        noremotecursor=0
        preferred_encoding=7
        compresslevel=-1
        quality=6
        localcursor=1
        scale_den=1
        scale_num=1
        local_cursor_shape=1'
      }
    } 'q' {
      #Closes the script
      return
    }
  }
  .$commands

}
&$commands

答案1

如果直接複製/貼上到 Powershell 中,它可以工作,但不會在腳本中運行。

這聽起來很像是不合適的執行政策。你可以跑去Get-ExecutionPolicy檢查一下。它可能會回來Restricted

若要永久變更此設置,請執行提升的 PowerShell 並使用 執行Set-ExecutionPolicy RemoteSigned並確認y

要暫時更改它以執行腳本,您可以像這樣運行它:

powershell -ExecutionPolicy Bypass -File .\ScriptFile.ps1

因此,這就是如何從腳本文件運行腳本的方法。除此之外,還有一個問題,已經提過@jfrmilner。在第六個選項中,您透過附加花括號定義另一個腳本區塊:

...
} '6' {
      $ComputerName = (Read-Host "ComputerName")
      {
        # This just the definition of a scriptblock and will not be executed!
        # Instead, it will be sent to stdout.
      }
    } 'q' {
...

定義本身不會運行這些行。您有兩種可能來解決這個問題:

  1. 省略花括號,因此不定義腳本區塊。這些行將會被執行。
  2. 在腳本區塊前面加上 a 前綴.,它將被執行:
...
} '6' {
      $ComputerName = (Read-Host "ComputerName")
      .{
        # This is a scriptblock that will directly be executed!
      }
    } 'q' {
...

答案2

同上 @Reg Edit 和 @Lee-Dailey 所說的。

我們看不到您的螢幕。

其次,你為什麼會期望...

&$commands

....這可以在腳本中工作嗎?

$commands 不是要執行的 .ps1,它是腳本中的腳本區塊。

您說您正在 powershell.exe 控制台或 ISE 或 VSCode 中粘貼,它應該可以工作,因為您加載了程式碼並從現有 PowerShell 實例運行它,並且在該實例中讀取了程式碼。

因此,除非將此程式碼另存為commands.ps1,否則 &$commands 毫無意義,因為程式碼永遠不會載入到正在執行或呼叫的PowerShell 實例中。

此外,該腳本區塊實際上根本不需要。

那麼,試試這個...

# VNCCommands.ps1
function Show-Menu
{
    param 
    (
        [string]$Title = 'My Menu'
    )

    Clear-Host
    $Banner = '='*16
    "$Banner $Title $Banner"

    "1: Press '1' (Description)."
    "2: Press '2' (Description)."
    "3: Press '3' (Description)."
    "4: Press '4' (Description)."
    "5: Press '5' (Description)."
    "6: Press '6' To start a VNC Connection."
    "Q: Press 'Q' to quit."
}

Show-Menu –Title 'My Menu'
$selection = Read-Host "Please make a selection"

switch ($selection)
{
    1 
    {
        "You have selected 1"
        sleep -seconds 2
    } 
    2 
    {
        "You have selected 2"
        sleep -seconds 2
    } 
    3 
    {
        "You have selected 3"
        sleep -seconds 2
    } 
    4 
    {
        "You have selected 4"
        sleep -seconds 2
    } 
    5 
    {
        "You have selected 5"
        sleep -seconds 2
    } 
    6 
    {
        $ComputerName = Read-Host -Prompt 'Enter a computer name: '
        If (-Not (Test-Path -Path "D:\temp\$ComputerName"))
        {New-Item -Path 'D:\Temp' -Name "$ComputerName.VNC" -ItemType File -Verbose}
        Else {Write-Warning -Message "D:\temp\$ComputerName.VNC creation failed"}
    } 
    'q' {return}
}

執行 - -

.\VNCCommands.ps1



# Results
<#
================ My Menu ================
1: Press '1' (Description).
2: Press '2' (Description).
3: Press '3' (Description).
4: Press '4' (Description).
5: Press '5' (Description).
6: Press '6' To start a VNC Connection.
Q: Press 'Q' to quit.
Please make a selection: 6
Enter a computer name: : test
VERBOSE: Performing the operation "Create File" on target "Destination: D:\Temp\test.VNC".


    Directory: D:\Temp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         19-Jul-20     17:14              0 test.VNC
#>

相關內容