私はInvoke-Commandを使用して、リモートサーバー上でpowershell式を実行しています。コマンド実行時に正規表現エラーが発生します。 powershellはRegExを内部的に使用して引数を解析するように見えますが、何らかの理由でこのシナリオでは失敗しています。Powershell Invoke-Command RegExエラーの結果
特に混乱しているのは、解析エラーの原因と思われる変数がInvoke-Command呼び出しでまったく使用されていないことです。私の推測では、( "使用"スコープを介して)遠隔実行スクリプトで変数を参照できるように、何らかの変数キャプチャが行われているので、これは$ ArtifactDirectoryパラメータでは失敗しています。例外の "正規表現"の値が$ ArtifactDirectoryパラメータの値なので、問題を引き起こすのは$ ArtifactDirectoryパラメータです。
もう1つ重要なことは、TFSがエラーなしで実行しているのとまったく同じマシン上で手動で(同じパラメータ値で)スクリプトを実行できることです。これは、TFSビルド/デプロイメントエージェントが設定している問題を引き起こすのは、何らかのセッションオプションであることを示しています。ここで
は、PowerShellスクリプトの関連部分である:
param(
[string] $ArtifactDirectory,
[string] $ComputerName
)
$someFancyCommand = "this does not seem to matter"
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Invoke-Expression "$using:someFancyCommand"
}
そして、ここでは、私は(TFSリリース管理を経て、このケースでは)それを実行しています方法です:
MyScript.ps1 -ArtifactDirectory "C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App" -ComputerName "SomeComputer"
ここれます完全なスタックトレース:
2016-04-25T13:53:28.6024146Z [Exception:System.Management.Automation.RuntimeException: The regular
2016-04-25T13:53:28.6024146Z expression pattern {{C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App}} is not valid. --->
2016-04-25T13:53:28.6024146Z System.ArgumentException: parsing "{{C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App}}" -
2016-04-25T13:53:28.6024146Z Unrecognized escape sequence \M.
2016-04-25T13:53:28.6024146Z at System.Text.RegularExpressions.RegexParser.ScanCharEscape()
2016-04-25T13:53:28.6024146Z at System.Text.RegularExpressions.RegexParser.ScanBasicBackslash()
2016-04-25T13:53:28.6024146Z at System.Text.RegularExpressions.RegexParser.ScanRegex()
2016-04-25T13:53:28.6024146Z at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
2016-04-25T13:53:28.6024146Z at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, TimeSpan matchTimeout, Boolean
2016-04-25T13:53:28.6024146Z useCache)
2016-04-25T13:53:28.6024146Z at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options)
2016-04-25T13:53:28.6024146Z at System.Management.Automation.ParserOps.NewRegex(String patternString, RegexOptions options)
2016-04-25T13:53:28.6024146Z at System.Management.Automation.ParserOps.ReplaceOperator(ExecutionContext context, IScriptExtent errorPosition,
2016-04-25T13:53:28.6226512Z Object lval, Object rval, Boolean ignoreCase)
2016-04-25T13:53:28.6226512Z --- End of inner exception stack trace ---
2016-04-25T13:53:28.6226512Z at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception
2016-04-25T13:53:28.6226512Z exception)
2016-04-25T13:53:28.6226512Z at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
2016-04-25T13:53:28.6226512Z at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2016-04-25T13:53:28.6226512Z at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2016-04-25T13:53:28.6226512Z at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
2016-04-25T13:53:28.6226512Z at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
2016-04-25T13:53:28.6336645Z at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean
2016-04-25T13:53:28.6336645Z createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior
2016-04-25T13:53:28.6336645Z errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo
2016-04-25T13:53:28.6336645Z invocationInfo, Object[] args)
2016-04-25T13:53:28.6336645Z at System.Management.Automation.ScriptBlock.<>c__DisplayClassa.<InvokeWithPipe>b__8()
2016-04-25T13:53:28.6336645Z at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
2016-04-25T13:53:28.6336645Z at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior
2016-04-25T13:53:28.6336645Z errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo
2016-04-25T13:53:28.6336645Z invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[]
2016-04-25T13:53:28.6336645Z args)
2016-04-25T13:53:28.6336645Z at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope,
2016-04-25T13:53:28.6336645Z ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
2016-04-25T13:53:28.6336645Z at Microsoft.PowerShell.Commands.ForEachObjectCommand.ProcessRecord()
2016-04-25T13:53:28.6336645Z at System.Management.Automation.CommandProcessor.ProcessRecord()]
私は '$ ArtFactCommand'を' $ ArtifactDirectory'でどのように作成するかの例を見ると思いますか?上記のコマンドを実際に実行した場合、エラーが異なると思います。なぜ 'invoke-expression'なのでしょうか?なぜ、「コマンド」だけではないのですか? – Matt
$ ArtFactCommandのためにArtifactDirectoryは使用されません。その$ ArtifactDirectoryは、Invoke-Command呼び出しの後のMyScript.ps1の他の場所で使用されます。実行されている式は、Windowsサービスを開始/停止/インストールするための基本的なTopShelfコマンドラインです。式自体は "& 'c:\ myapp \ host.exe' start"のようになります。私の記事で述べたように、コマンド自体は重要ではないようです。変数をまったく使用しない単純な書き込みホストで置き換えることはできますが、それでも失敗します。 「なぜ、単にコマンドではないのですか」と言うとき、あなたは何を指していますか? – RMD
エラーが '$ ArtifactDirectory'に関連している場合、それを使用中に見ないのはなぜですか(エラーがPowerShellから来た場合)?私は彼らが関連していると仮定していたので、あなたはexeを実行していたので、IEXの使用について質問しました。その場合も 'Invoke-Command -Command'はうまくいきました。もう一度、変数とあなたのコードとの関係を調べようとしていました。私はTFSについては何もしていないので、私の無知に過ぎないかもしれません。エラーはTFSリリース管理から来ていますか? – Matt