Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让PowerShell显示正在调用的命令_Powershell - Fatal编程技术网

如何让PowerShell显示正在调用的命令

如何让PowerShell显示正在调用的命令,powershell,Powershell,我有一个简单的PowerShell脚本,我正在Jenkins那里调用它: function PerformRepoActions($localRepo) { $startDir = $pwd.path cd $localRepo hg recover hg revert --all hg pull -r $branch --rebase hg update -r $branch cd $startDir } $branch = $env:NAMEDBRANCH Pe

我有一个简单的PowerShell脚本,我正在Jenkins那里调用它:

function PerformRepoActions($localRepo)
{
  $startDir = $pwd.path
  cd $localRepo
  hg recover
  hg revert --all
  hg pull -r $branch --rebase
  hg update -r $branch
  cd $startDir
}

$branch = $env:NAMEDBRANCH
PerformRepoActions $pwd.path 
当我运行此命令时,它不会显示我正在执行的任何mercurial命令。以下是Jenkins输出中显示的内容:

no interrupted transaction available
pulling from [repo_location]
no changes found
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
它给出了hg命令的输出,但没有显示命令本身

因此,我正在寻找一些可以打印这些命令的东西,相当于cmd.exe的“echo on”

搜索告诉我,PowerShell中的等效项是“Set PSDebug-Trace 1”。因此,我将其添加到脚本的开头,并将输出更改为:

DEBUG:   15+  >>>> $branch = $env:NAMEDBRANCH
DEBUG:   16+  >>>> PerformRepoActions $pwd.path
DEBUG:    5+  >>>> {
DEBUG:    6+    >>>> $startDir = $pwd.path
DEBUG:    7+    >>>> cd $localRepo
no interrupted transaction available
pulling from ssh://hg@mercurial.wilcoxassoc.com/PcDmis/QA
no changes found
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
正如您所看到的,虽然它确实给了我更多的输出,但它实际上并没有显示我正在调用的命令

“setpsdebug-trace2”提供了更多的输出,但仍然没有显示调用了什么mercurial命令

我提出的解决方法是创建一个函数,该函数回响并执行一个字符串。这很有效,但感觉很糟糕:

function execute($cmd)
{
  echo $cmd
  iex $cmd
}

function PerformRepoActions($localRepo)
{
  $startDir = $pwd.path
  execute "cd $localRepo"
  execute "hg recover"
  execute "hg revert --all"
  execute "hg pull -r $branch --rebase"
  execute "hg update -r $branch"
  execute "cd $startDir"
}
$branch = $env:NAMEDBRANCH
PerformRepoActions $pwd.path
有没有更好的办法

看来一定有,但我以前一直很惊讶


编辑:这不是的副本。将输出重新定向到一个文件,这是该问题的答案,是不可行的。我需要它显示在Jenkins输出中,而不是一个文件。

有一种方法可以捕获当前脚本的内容,只需做一点工作,就可以将其与相关输出结合起来。(我不确定这是否需要)

从Powershell v3开始,您可以访问(这是我能找到的唯一一个关于它的链接!)

基本上Ast可以访问当前脚本(或您发送给它的任何脚本)的所有属性,包括其完整的源代码

运行此脚本(我包括记事本以显示它运行外部exe)

将产生此输出

before


Attributes           : {}
UsingStatements      : {}
ParamBlock           : 
BeginBlock           : 
ProcessBlock         : 
EndBlock             : Write-Host "before"
                       $MyInvocation.MyCommand.ScriptBlock.Ast 
                       Write-Host "after"

                       notepad
DynamicParamBlock    : 
ScriptRequirements   : 
ImplementingAssembly : 
Extent               : 
                       Write-Host "before"
                       $MyInvocation.MyCommand.ScriptBlock.Ast 
                       Write-Host "after"

                       notepad 
Parent               : 

after
我想如果你和我一起玩

$MyInvocation.MyCommand.ScriptBlock.Ast 
你应该能够让它生产出你想要的东西

编辑以添加


通过使用Ast选择每个Powershell cmdlet并为其附加一个断点,您可以进一步提高这一点,然后您应该可以访问cmdlet名称(我认为这不适用于可执行文件,目前这只是一个理论!)您需要通过管理日志记录并从断点继续的外部脚本来运行整个过程。但我认为这是可行的

Mercurial不可能复制Oops。我只是想把它贴上“powershell”的标签。对此很抱歉。正如另一个答案所说,PowerShell(以及我所知道的大多数shell)上没有“echo on”这样的东西。你不能将Jenkins配置为显示该文件吗?(可能与?)您也可以执行“echo on”,然后在完成后对文件进行cat。
$MyInvocation.MyCommand.ScriptBlock.Ast