2016-04-25 14 views
1

私は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()] 
+0

私は '$ ArtFactCommand'を' $ ArtifactDirectory'でどのように作成するかの例を見ると思いますか?上記のコマンドを実際に実行した場合、エラーが異なると思います。なぜ 'invoke-expression'なのでしょうか?なぜ、「コマンド」だけではないのですか? – Matt

+0

$ ArtFactCommandのためにArtifactDirectoryは使用されません。その$ ArtifactDirectoryは、Invoke-Command呼び出しの後のMyScript.ps1の他の場所で使用されます。実行されている式は、Windowsサービスを開始/停止/インストールするための基本的なTopShelfコマンドラインです。式自体は "& 'c:\ myapp \ host.exe' start"のようになります。私の記事で述べたように、コマンド自体は重要ではないようです。変数をまったく使用しない単純な書き込みホストで置き換えることはできますが、それでも失敗します。 「なぜ、単にコマンドではないのですか」と言うとき、あなたは何を指していますか? – RMD

+0

エラーが '$ ArtifactDirectory'に関連している場合、それを使用中に見ないのはなぜですか(エラーがPowerShellから来た場合)?私は彼らが関連していると仮定していたので、あなたはexeを実行していたので、IEXの使用について質問しました。その場合も 'Invoke-Command -Command'はうまくいきました。もう一度、変数とあなたのコードとの関係を調べようとしていました。私はTFSについては何もしていないので、私の無知に過ぎないかもしれません。エラーはTFSリリース管理から来ていますか? – Matt

答えて

0

通常のPowerShellで試してみました。あなたがあなたの質問で指摘したように、これは間違いなくTFSリリース管理の問題と思われ、入力引数の扱いです。

解像度/動作周囲:パス"C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App"のすべての特殊文字をエスケープしてください。 PowerShellのエスケープシーケンスは、文字\です。ここにあなたの便宜のために

はパスがエスケープシーケンスと同じようにどのように見えるべきかです:

"C:\\MSAgent\\_work\\66f1e4ebb\\Cc - \(WIP\)\\CC - My Cool App" 
1

私は再書き込みスクリプト全体を終了し、魔法、エラーが去っていきました。他の人が指摘しているように、上のスクリプトの簡単なバージョンを試してみるとうまくいきます。実際には、手動で実行され、TFSリリースで実行されてもうまく動作します。

最終的には、エラーがスクリプトの後のファイルコピー操作に関連していたように見えますが、のInvoke-Command呼び出しが削除された場合、ファイルコピー操作はうまく機能しました

だから私は何が起こっているのかわからないんだけど、リモートコマンドの実行と、次のスクリプトのその後の実行の間に何らかの相互作用があった:任意のレートで

Get-ChildItem -Path $sourceDirectory -Recurse -Exclude $exclude | 
    Copy-Item -Destination { 
     if ($_.PSIsContainer) { 
      Join-Path $ArtifactDirectory $_.Parent.FullName.Substring($sourceDirectory.length) 
     } else { 
      Join-Path $ArtifactDirectory $_.FullName.Substring($sourceDirectory.length) 
     } 
    } -Force -Exclude $exclude -Verbose 

が、私は上記のリファクタリングスクリプトをモジュール/関数に組み込むと、問題は完全に消え去った。

関連する問題