collect the statistics for the execution of powershell scripts

Posted on 2013-05-13
Medium Priority
Last Modified: 2013-05-14

How to collect the statistics for the execution of powershell scripts in a table in SQL SERVER (date, name of the script execution time) data?
Do you have any sample scripts or command?

thank you

Question by:bibi92
1 Comment
LVL 72

Accepted Solution

Qlemo earned 2000 total points
ID: 39163230
You will need to have a "wrapper" script, which takes the script name as a parameter, then execute it with measure-command, and construct an insert statement with the result.
function measure-script ([String] $scriptname)
  $connstr = "server=MSSQLserver; database=Stats; Integrated Security=SSPI"
  $dt = get-date
  $mo = measure-command { & "$scriptname" }
      "insert into psscriptstats (scriptname, starttime, exectime) values ('$scriptname', '$dt', $($mo.milliseconds))",
      new-object System.Data.SqlClient.SqlConnection($connstr)

Open in new window

is a minimalistic approach (untested).

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
After a recent Outlook migration from a 2007 to 2010 environment, some issues with Distribution List owners were realized. In this article, I explain how that was rectified.
Loops Section Overview
Screencast - Getting to Know the Pipeline

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question