private void MakeManualBatchFiles()

in DiagManager/DiagClasses/PackageMgr.cs [133:348]


        private void MakeManualBatchFiles()
        {


            if (m_Instance == "*")
            {
                return;
            }

            StreamWriter ManualStart = File.CreateText(m_tempDirectory + @"\ManualStart.txt");
            StreamWriter ManualStop = File.CreateText(m_tempDirectory + @"\ManualStop.txt");

            ManualStart.WriteLine("REM PURPOSE: This file is for cases where execution of PSSDIAG does not work for some reason. It allows you to manually collect some base information.");
            ManualStart.WriteLine("REM This includes Perfmon, Perfstat scripts and some other configuration information for the instance (sp_configure, sys.databses, etc)");
            ManualStart.WriteLine("REM INSTRUCTIONS:");
            ManualStart.WriteLine("REM 1. Rename the file to ManualStart.cmd (change the extension)");
            ManualStart.WriteLine("REM 2. Rename the ManualStop.txt to ManualStop.cmd (change the extension)");
            ManualStart.WriteLine("REM 3. Execute from a Command Prompt by running ManualStart.cmd");
            ManualStart.WriteLine("REM 4. When ready to stop, execute ManualStop.cmd from another Command Prompt window");
            ManualStart.WriteLine("REM 5. Find the collected data in the \\Output folder");
            ManualStart.WriteLine("");

            ManualStop.WriteLine("REM PURPOSE: This file is for cases where execution of PSSDIAG does not work for some reason. This file stops manual collection of base information.");
            ManualStop.WriteLine("REM INSTRUCTIONS:");
            ManualStop.WriteLine("REM 1. Rename the file to ManualStop.cmd (change the extension)");
            ManualStop.WriteLine("REM 2. When ready to stop collection, execute ManualStop.cmd from a new Command Prompt window");
            ManualStop.WriteLine("REM 3. Find the collected data in the \\Output folder");
            ManualStop.WriteLine("");



            ManualStart.WriteLine("setlocal ENABLEEXTENSIONS");
            ManualStart.WriteLine("set LaunchDir=%~dp0");
            ManualStart.WriteLine("set servername=%computername%");


            ManualStop.WriteLine("setlocal ENABLEEXTENSIONS");
            ManualStop.WriteLine("set LaunchDir=%~dp0");
            ManualStop.WriteLine("set servername=%computername%");

            ManualStart.WriteLine("md \"%LaunchDir%\\output\\internal\"");

            //need to test on a cluster and see if . does anything for VNN
            //TODO: NEED TO EXCLUDE FROM HASH CALCULATION



            ManualStart.WriteLine(string.Format(SqlCmdTemplate, m_Server_Instance, m_output_instance_prefix + "_msdiagprocs.out", "-i\"" + m_input_prefix + "msdiagprocs.sql" + "\""));

            //make manual profile collector
            if (m_userchoice[Res.CollectProfiler] == "true")
            { 
                string strTraceEvents = "";
                foreach (DiagItem diagItem in m_userchoice.ProfilerCategoryList.GetCheckedDiagItemList())
                {
                    TraceEvent evt = diagItem as TraceEvent;
                    if (!string.IsNullOrEmpty(strTraceEvents))
                    {
                        strTraceEvents += ",";
                    }
                    strTraceEvents += evt.Id.ToString();
                }
                string MaxFileSize = m_userchoice[Res.ProfilerMaxFileSize];
                string FileCount = m_userchoice[Res.ProfilerFileCount];
                
                string strTraceStartQuery = string.Format(StartTraceTemplate, strTraceEvents, m_AppName, m_output_instance_prefix + "_sp_trace", MaxFileSize, FileCount);
                string strTraceStopQuery = string.Format(StopTraceTemplate, m_AppName);
                ManualStart.WriteLine("rem starting profiler trace");
                string cmd = string.Format(SqlCmdTemplate, m_Server_Instance, m_internal_output_instance_prefix + "_sp_trace_start.out", "-Q\"" + strTraceStartQuery + "\"");
                ManualStart.WriteLine(cmd);
                ManualStop.WriteLine("rem stoping profile trace");
                cmd = string.Format(SqlCmdTemplate, m_Server_Instance, m_internal_output_instance_prefix + "_sp_trace_stop.out", "-Q\"" + strTraceStopQuery + "\"");
                ManualStop.WriteLine(cmd);
            }

            //make a copy
            List<DiagCategory> checkedCategories = new List<DiagCategory>();
            foreach (DiagCategory cat in m_userchoice.CustomDiagCategoryList.GetCheckedCategoryList())
            {
                checkedCategories.Add(cat);
            }


            // if SQL Xevent needs to be captured
            if (m_userchoice[Res.CollectXEvent] == "true")
            {
                DiagCategory XEventGroup = DiagFactory.GetCustomGroup(null, @"Templates\CustomDiagnostics_Xevent.xml");
                //need to manuall handle taskname "Add File Target" for file size and file count
                DiagItem taskItem = XEventGroup.DiagEventList.Find(x => x.Name == "Add File Target"); // for CustomTask Name is the same as TaskName
                CustomTask task = taskItem as CustomTask;
                task.Cmd = task.Cmd.Replace("1024x", m_userchoice[Res.XEventMaxFileSize]).Replace("5x", m_userchoice[Res.XEventFileCount]);

                XEventGroup.Name = "XEvent";
                //manually check events for special handling
                XEventGroup.CheckEvents(true);
                checkedCategories.Add(XEventGroup);
            }



            foreach (DiagCategory cat in checkedCategories)
            {
                ManualStart.WriteLine("rem group: " + cat.Name);
                foreach (DiagItem item in cat.GetCheckedEventList())
                {
                    CustomTask task = item as CustomTask;
                    //currently do not handle anything other than TSQL script
                    if (null == task )
                    {
                        continue;
                    }
                    if (task.Type.ToUpper() == "TSQL_SCRIPT")
                    {
                        string cmd = "Start ";
                        if (task.GroupName == "XEvent")
                        {
                            cmd = ""; //make sure xevent is last group
                        }
                            
                        cmd = cmd + string.Format(SqlCmdTemplate, m_Server_Instance, m_output_instance_prefix + "_" + task.Cmd + ".out", "-i\"" + m_input_prefix + task.Cmd +"\"" );
                        if (task.Point.ToUpper()=="STARTUP")
                        {
                            ManualStart.WriteLine(cmd);

                        }
                        else
                        {
                            ManualStop.WriteLine(cmd);

                        }
                    
                    }
                  /*
                  //various issues exists for batch files. 
                    else if (task.Type.ToUpper() =="UTILITY")
                    {
                        // working example
                        //start  collecterrorlog.cmd jackli2014\sql16a 1 ^> "c:\temp\pssd\out5.txt" 2>&1 ^&^&exit
                        //
                        string cmd = "start " +  ReplaceToken(task.Name, task.Cmd);
                        cmd = cmd.Replace(">", "^>").Replace("2^>&1", "2>&1");
                        cmd = cmd + " &exit";
                        if (task.Point.ToUpper() == "STARTUP")
                        {
                            ManualStart.WriteLine(cmd);
                        }
                        else
                        {
                            ManualStop.WriteLine(cmd);
                        }

                        
                    }*/
                    else if (task.Type.ToUpper() == "TSQL_COMMAND")
                    {
                        string cmd = "Start ";


                        if (task.GroupName == "XEvent")
                        {
                            cmd = ""; // can do this because it's the last event


                        }
                        
                        cmd =cmd + string.Format(SqlCmdTemplate, m_Server_Instance, m_output_instance_prefix + "_" + task.TaskName + ".out", "-Q\"" + task.Cmd + "\"");
                        cmd = cmd.Replace(@"%output_path%%server%_%instance%", m_output_instance_prefix);

                        if (task.Point.ToUpper() == "STARTUP")
                        {
                            ManualStart.WriteLine(cmd);

                        }
                        else
                        {
                            ManualStop.WriteLine(cmd);

                        }

                    }
                    else
                    {
                        Logger.LogInfo("Manual run hasnot been implemented for task type " + task.Type);
                    }



                    //task.Cmd


                }
            }

            ManualStart.WriteLine("rem starting Perfmon");

            ManualStart.WriteLine(string.Format("logman stop {0}", LogManName));
            ManualStart.WriteLine(string.Format("logman delete {0}", LogManName));
            
            ManualStart.WriteLine(string.Format("logman CREATE COUNTER -n {0} -s {1} -cf {2} -f bin -si 00:00:15 -o {3} -ow ", LogManName, m_userchoice[Res.MachineName], "LogmanConfig.txt", @"output\pssdiag.blg" ));

            ManualStart.WriteLine(string.Format("logman start {0} ", LogManName)); 


            ManualStop.WriteLine(string.Format("logman stop {0}", LogManName));

             ManualStop.WriteLine(string.Format(SqlCmdTemplate, m_Server_Instance, m_output_instance_prefix + "_kill.out", "-Q tempdb.dbo.sp_killpssdiagSessions"));
            
            //logman CREATE COUNTER -n SQLServer -s AlwaysOnN1  -cf C:\dsefiles\dse_counters.txt -f bin -si 00:00:15 -o C:\dsefiles\SQL.blg


            ManualStart.Flush();
            ManualStart.Close();
            ManualStop.Flush();
            ManualStop.Close();

        }