powershell - 有没有办法在 powershell session 中启用 "noninteractive"

标签 powershell powershell-5.0

我有一个编排机制(bamboo 服务器),可以启动我的 ps1 脚本。

但是,它启动脚本时没有给 powershell.exe 添加 -noninteractive 参数。

这反过来又使我的所有脚本停止并等待用户交互。

Atlassian 不想改变他们的启动机制,所以我需要弄清楚是否可以以某种方式从“内部”操纵我的 powershell session 。

问题是这样的: 有什么方法可以让我的 powershell session 在启动后更改为“非交互模式”?

最佳答案

这样的解决方案适合您吗?

# Returns $true if current PS instance started with -NonInteractive parameter
function Test-NonInteractive {
  foreach ( $arg in [Environment]::GetCommandLineArgs() ) {
    if ( $arg -like "-noni*" ) {
      return $true
    }
  }
  return $false
}

if ( -not (Test-NonInteractive) ) {
  $scriptArgs = ""
  $args | ForEach-Object {
    if ( $scriptArgs -eq "" ) {
      $scriptArgs = '"{0}"' -f $_
    }
    else {
      $scriptArgs = ' "{0}"' -f $_
    }
  }
  $params = @{
    "FilePath" = "powershell.exe"
    "ArgumentList"  = @(
      "-ExecutionPolicy Bypass"
      "-NonInteractive"
      "-File ""{0}""" -f $MyInvocation.MyCommand.Path
      $scriptArgs
    )
  }
  Start-Process @params
  return
}

# ...rest of script logic here...

此脚本使用 -NonInteractive 参数以及您传递给脚本的任何参数重新启动。

关于powershell - 有没有办法在 powershell session 中启用 "noninteractive",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47533908/

相关文章:

excel - 运行 powershell 命令时出错

powershell - 在Powershell中重新加载修改后的类

powershell - DSC:模块文件 *ModuleName* 不包含具有所需版本 1.0 的模块

powershell,如何获取 firefox 窗口标题并关闭其中一个

powershell - 如何过滤powershell中从控制台程序获取的行?

PowerShell:以编程方式访问脚本文档

debugging - 清除 ISE Powershell 环境/调试方法

powershell - 在 Windows Server 2008 R2 上使用 Com 对象通过 PowerShell 在特定 NIC 上禁用 IPV6?

powershell - 如何使用 PowerShell 拆分文本文件?

windows - 检索powershell中日期范围内修改的文件的完整路径和文件权限