我有一个编排机制(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/