AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1569850
Accepted
Mastaxx
Mastaxx
Asked: 2020-07-20 03:35:07 +0800 CST2020-07-20 03:35:07 +0800 CST 2020-07-20 03:35:07 +0800 CST

为什么这个 New-Item 命令在 powershell 中有效,但在 ps1 脚本中无效?

  • 772

完全被困在这里,不知道该怎么做:我有一个多选交互式 Powershell 脚本,我正在为操作组合在一起,所以只要他们想在远程计算机上执行快速任务,他们就会节省几分钟。

我已经用 just 替换了所有其他选项的所有代码You have selected #,所以我不会让任何人对其余的脚本内容感到厌烦,但是选择6让我发疯。

这就是我的目标,但还不能超越第一个要点:

  • 创建新.vnc文件
  • 将文本的标准 VNC 协议正文添加到文件中
  • $ComputerName之后添加Host=
  • 启动文件

问题是下面根本不会创建文件;如果直接复制/粘贴到 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
windows-10 powershell
  • 2 2 个回答
  • 526 Views

2 个回答

  • Voted
  1. Best Answer
    stackprotector
    2020-07-20T21:51:41+08:002020-07-20T21:51:41+08:00

    如果直接复制/粘贴到 Powershell 中,它可以工作,但不会在脚本中运行。

    这听起来很像一个不合适的执行策略。你可以跑Get-ExecutionPolicy过去看看。它可能会返回Restricted。

    要永久更改它,请运行提升的 PowerShell 并执行Set-ExecutionPolicy RemoteSigned并使用y.

    要临时更改它以执行脚本,您可以像这样运行它:

    powershell -ExecutionPolicy Bypass -File .\ScriptFile.ps1
    

    因此,这就是您可以从脚本文件运行脚本的方式。除此之外,@jfrmilner已经提到了另一个问题。在您的第 6 个选项中,您正在通过附加的花括号定义另一个脚本块:

    ...
    } '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
  2. postanote
    2020-07-20T15:23:08+08:002020-07-20T15:23:08+08:00

    @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
    #>
    
    • 0

相关问题

  • VMware Workstation USB 仲裁服务无法自动启动

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 资源管理器侧面板中的桌面外壳快捷方式

  • 为什么我不能将文件从 Android 发送到 Windows 10?

  • 在多个文件上打开方式?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve