Question:
I have written a program with C#, that creates a logfile and fills this by using log4net. This program starts powershell-scripts. The scripts use log4net, too.
It works:
1 2 3 4 5 6 7 8 9 10 |
> C#: > ps.Runspace.SessionStateProxy.SetVariable("myMethod",(Action > ps.AddCommand(System.IO.Path.Combine(pathScripts, testSkripte[i].ToString())); > ps.Invoke(); > Powershell: > $ScriptLog.Invoke([log4net.Core.Level]::Debug, "TestFile_Debug") > $ScriptLog.Invoke([log4net.Core.Level]::Warn, "TestFile_Warn") $ScriptLog > $ScriptLog.Invoke([log4net.Core.Level]::Error, "TestFile_Error") |
Now I want add to use the standard Write-Error, Write-Debug, etc. CMDlets in my Script.
(looks like here – answer of Hinek).
1 2 3 4 5 |
Powershell: Write-Warning "Write-Warning" AND Write-Error "Write-Error" |
works, but the following doesn´t work:
1 2 3 |
Write-Debug "Write-Debug" (I don´t get an item in my logfile) OR Write-Debug "Write-Debug" -debug (for this I get an item in my logfile, but ...) |
… I get an error in my logfile, too. The error looks like this:
1 2 3 4 |
[2010-10-22 13:10:58,097] DEBUG : Write-Debug [2010-10-22 13:10:58,113] ERROR : Cannot invoke this function because the current host does not implement it |
(I think to have all namespaces.)
What the error-message means and what can I do again this?
thanks
Answer:
Now I had found the answer myself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
C#-Code: using (ps = PowerShell.Create()) { int a = testSkripte.Count; for (int i = 0; i < a; i++) { ps.Runspace.SessionStateProxy.SetVariable("portalpfad", pathExecuteScript); ps.Runspace.SessionStateProxy.SetVariable("ScriptLog", (Action //add following row: ps.Runspace.SessionStateProxy.SetVariable("DebugPreference", "Continue"); ps.AddCommand(System.IO.Path.Combine(pathScripts, testSkripte[i].ToString())); ps.Streams.Debug.DataAdded += new EventHandler ps.Streams.Warning.DataAdded += new EventHandler ps.Streams.Error.DataAdded += new EventHandler ps.Invoke(); } |
and this for write-debug:
1 2 3 4 5 6 7 8 9 10 11 12 |
Powershell-Code: usings ... #set variable $DebugPreference #Now Write (-Debug) without Error Write-Debug "Write-Debug" Write-Warning "Write-Warning" Write-Error "Ende --> Write-Error" |