title: Automate Splunk oneshot command with powershell
body: <p>I am trying to write a powershell script that automates Splunk's <code>oneshot</code> command which will explicitly reindex a log file contained in a monitored directory.  Given a range of dates, the script will traverse the directory in which the log files are kept, and execute <code>oneshot</code> once for each file that targets that date (there are typically between 50 and 110 or so per day). I am definitely not what you would call a "powershell guru" by any means, which is what brings me here. </p>
  
  <p>When taking a closer look, <code>Invoke-Expression</code> <a href="http://screencast.com/t/L8rpy63fDeeN" rel="nofollow">seems to be</a> starting a separate cmd.exe instance each time it's called.  This results in either the splunkd daemon to crash, or for cmd.exe to crash.</p>
  
  <p>That being said, are there better alternatives to using <code>Invoke-Expression</code>?  It appears that my script isn't waiting for each invoked expression to complete before it fires off the next one, and that is causing adverse effects on the server.  I've read this happens when using <code>Invoke-Expression</code> to call into external non-console applications. In this case, I'm calling into splunk.exe to execute the command, so that makes sense.</p>
  
  <p>I am considering utilizing the scripting host instead of using <code>Invoke-Expression</code>, but first wanted to ask the pros here at StackOverflow for some potential alternatives :)</p>
  
  <p>That being said, has anyone run into a similar problem in the past, and if so what alternative did you go with? Alternatively, are there other powershell tools I could use in place of <code>Invoke-Expression</code> that would not only do the job, but would provide more stable execution? Any help/suggestions are much appreciated!</p>