如何将PowerShell脚本嵌入到与Windows批处理脚本相同的文件中?
我知道在其他情况下这种事情是可能的:
sqlcmd并在文件开头巧妙地安排goto和注释#!/usr/local/bin/python.可能没有办法做到这一点 - 在这种情况下,我将不得不从启动脚本调用单独的PowerShell脚本.
我考虑过的一个可能的解决方案是回显PowerShell脚本,然后运行它.不这样做的一个很好的理由是,尝试这个的部分原因是使用PowerShell环境的优点而没有例如转义字符的痛苦
我有一些不寻常的限制,并希望找到一个优雅的解决方案.我怀疑这个问题可能是各种各样的反应:"你为什么不尝试解决这个不同的问题." 我只想说这些是我的约束,对此感到抱歉.
有任何想法吗?是否有一个巧妙的评论和转义字符的合适组合,这将使我能够实现这一目标?
关于如何实现这一点的一些想法:
^一行中的克拉是一个延续 - 就像Visual Basic中的下划线一样&通常用于将命令echo Hello & echo World结果分成两个回声在不同的行上所以这样的事情(如果我可以使它工作)会很好:
# & call powershell -psconsolefile %0
# & goto :EOF
/* From here on in we're running nice juicy powershell code */
Write-Output "Hello World"
Run Code Online (Sandbox Code Playgroud)
除了...
Windows PowerShell console file "insideout.bat" extension is not psc1. Windows PowerShell console file extension must be …我正在使用Process类来运行exe.
exe是我无法控制的第三方控制台应用程序.
我想知道进程是否在命令行上等待输入.
如果它有任何区别,我打算在应用程序等待输入时终止它.
当程序的输出等待读取时,有适当的事件,但是当过程耐心地等待输入时,我看不到任何类似的事件.
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "myapp.exe";
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
process.StartInfo = info;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
如何检测我的进程正在等待输入?
我一直在使用xsd.exe生成一个用于反序列化XML的类.我在源xsd中有十进制值,不需要:
<xs:attribute name="Balance" type="xs:decimal" use="optional" />
Run Code Online (Sandbox Code Playgroud)
xsd生成的类生成以下代码:
private decimal balanceField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Balance {
get {
return this.balanceField;
}
set {
this.balanceField = value;
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到它不是可空的.
我如何将字段生成为可为空的字段,如下所示:
private decimal? balanceField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal? Balance {
get {
return this.balanceField;
}
set {
this.balanceField = value;
}
}
Run Code Online (Sandbox Code Playgroud) 在遇到堆栈溢出的链接后,我发现MiškoHevery的着作非常引人入胜.非常好,以至于我看到了我以前认为我做得很好的新方法.
他主要讨论依赖注入,自动单元测试和良好设计.
他提倡的一些好的做法是可以以编程方式检测到的东西.因此,有一个程序可以检测它们Google Testability Explorer.
我的问题是:
我目前正在开发的项目使用Enterprise Libraries V3.1框架进行日志记录.
我需要获取生成的日志文件并将其存档在特定点.内置的跟踪侦听器似乎使文件在记录事件之间保持打开状态.我已经设置了一个自定义跟踪侦听器,它将附加到文件并关闭它,以便文件始终可以移动.
它看起来像这样(为清晰起见,减去错误处理):
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class AlwaysClosedTextFileTraceListener : CustomTraceListener
{
private string logFilePath;
public AlwaysClosedTextFileTraceListener ()
{
logFilePath = @"hardcodedpath\log.txt";
}
public override void Write(string message)
{
using (StreamWriter logFile = File.AppendText(logFilePath))
{
logFile.Write(message);
logFile.Flush();
logFile.Close();
}
}
public override void WriteLine(string message)
{
using (StreamWriter logFile = File.AppendText(logFilePath))
{
logFile.WriteLine(message);
logFile.Flush();
}
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if (data is LogEntry && this.Formatter != null)
{
WriteLine(this.Formatter.Format(data …Run Code Online (Sandbox Code Playgroud) 我在Windows窗体上有一个数据网格.
当我最初更新该网格以包含数据,然后单击它时,我得到以下异常.
值得注意的是,异常是在窗体处理窗体的内部,因此在我启动窗体时捕获异常.
请注意,我必须勾选调用堆栈中的"显示外部代码"以显示下面的调用堆栈,否则它只显示[外部代码].
在预感/故障排除步骤方面,并认为这可能是造成由被为其选择的项目,然后这是目前选择的项目解雇的消息,我曾尝试.SelectAll();在DataGridView更新后的数据,这有相同的结果.
例外情况:
IndexOutOfRangeException
{"Index -1 does not have a value."}
Run Code Online (Sandbox Code Playgroud)
调用堆栈:
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.this[int].get(int index) + 0xa1 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.Current.get() + 0x16 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(System.Windows.Forms.DataGridViewCellEventArgs e) + 0x101 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnRowEnter(ref System.Windows.Forms.DataGridViewCell dataGridViewCell = null, int columnIndex = 1, int rowIndex = 0, bool canCreateNewRow, bool validationFailureOccurred) + 0x218 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(int columnIndex = 1, int rowIndex = 0, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick = true) + 0x59f bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnCellMouseDown(System.Windows.Forms.DataGridView.HitTestInfo hti, bool isShiftDown, bool isControlDown) + 0x12db …Run Code Online (Sandbox Code Playgroud) 我最近调试了一些有点内存泄漏的代码.这是一个长期运行的程序,作为Windows服务运行.
如果你发现一个戴着IDisposable界面的类,它告诉你它使用的一些资源超出了垃圾收集器为你清理的能力.
它告诉你的原因是你,这个对象的用户现在负责清理这些资源的时间.恭喜!
作为一名尽职尽责的开发人员,.Dispose()当您完成对象以释放这些非托管资源时,您会轻松调用该方法.
完成后,有一个很好的using()模式可以帮助清理这些资源.只是发现哪些确切的物体导致泄漏?
为了帮助追踪这些流氓非托管资源,有什么方法可以查询在任何给定时间点等待处置的对象是什么?
我遇到完全相同:这个问题(和我完全一样的世界疼痛) 中的LINQ ChangeConflictException到SQL更新
因此,我使用LinqToSQL提供SQL的更新,并且ChangeConflictException因为NOCOUNT设置为失败而失败ON.
由于遗留数据库也在我定位的服务器上,我无法重新配置默认连接设置,正如Linq to Sql update中ChangeConflictException中接受的答案中所说的那样.
我的问题是:SET NOCOUNT OFF在执行更新之前,您如何说服LinqToSQL运行SQL ?
在开发Windows表单时,您将获得表单设计器,当您双击该设计器上的按钮时,它会生成方法存根,您可以编辑该方法.
在编辑XSD时,您可以从设计人员或代码中进行编辑.
如果您安装了企业库,则可以右键单击app.config并单击"编辑企业库配置",这会在文档选项卡中显示一个窗口,该窗口在app.config的引擎下编辑XML(尽管UI上有由于某种原因,这比上述两个例子更笨拙.
我希望创建其中一个插件,我该怎么做?
如果它有所作为,我希望获得的UI如下:
会有行,一列有一个组合框.我可以从组合框中选择一个项目并单击该行上的一个按钮,这将创建一个方法存根,其中包含来自组合框的数据的属性.
我打算在反向工作:当你将一个类加载到这个插件中时,它将在行中列出具有相关属性的方法.
c# ×7
.net ×1
.net-2.0 ×1
batch-file ×1
cmd ×1
datagrid ×1
linq ×1
linq-to-sql ×1
logging ×1
plugins ×1
powershell ×1
scripting ×1
sql ×1
unit-testing ×1
windows ×1
winforms ×1
xsd.exe ×1