有没有办法从 powershell 会话中启用 "noninteractive"

Is there a way to enable "noninteractive" from within a powershell session

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

但是它启动脚本时没有 powershell.exe 的 -noninteractive 参数。

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

A​​tlassian 不想改变他们的启动机制,所以我想知道我是否可以通过某种方式从 "within" 操纵我的 powershell 会话。

问题是这样的: 有什么方法可以让我的 powershell 会话在启动后更改为 "non interactive mode"?

这样的解决方案对您有用吗?

# 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 参数以及您传递给脚本的任何参数重新启动。

无法在会话中执行此操作。

按照 Bill 的定义,有一个创建新会话的解决方法。