Debugging Runtime Scripts in Operations Manager and Essentials 2007 The third installment in the System Center Forum Scripting Series

Similar documents
Part 2: Custom Performance Objects in Runtime Scripts

Part 1:Updating MOM 2005 Scripts for Operations Manager 2007

Intro to MS Visual C++ Debugging

Debugging Code in Access 2002

Installing Visual Studio for Report Design

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

The Perl Debugger. Avoiding Bugs with Warnings and Strict. Daniel Allen. Abstract

Windows Script Host Fundamentals

SCUtils ConvertTask Guide Solution for Microsoft System Center 2012 Service Manager

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Editor: Maarten Goet MVP- OpsMgr Editor: Pete Zerger, MVP- OpsMgr System Center Forum

CS 1110, LAB 1: PYTHON EXPRESSIONS.

A Tutorial for ECE 175

11Debugging and Handling. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

SQL Server Whitepaper DEMYSTIFYING DEBUGGING TECHNIQUES WITH SQL SERVER BY PINAL DAVE

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Window Server 2012 Hyper-V Virtual Machine Snapshots

CS354 gdb Tutorial Written by Chris Feilbach

Starting to Program in C++ (Basics & I/O)

Introduction to IntelliJ

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002

Arduino IDE Friday, 26 October 2018

Copyright 2018 MakeUseOf. All Rights Reserved.

Programming Logic - Beginning

Shibboleth Best Practice Guide Guidance for Installing and Running Shibboleth on Windows

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

7 Tips for Raising The Quality Bar With Visual Studio 2012

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

1.7 Limit of a Function

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

Subject: Top-Paying IT Certificates for 2015 (And Our New Courses)

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

BLUEBIRD QUESTIONS. Basics Patient Data 4 5. Appointments 5. Service Codes 5 6. Invoice Creation 6 7. Invoice Advance 7 8. Payment Basics 8 9

File System Management Pack

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Supplement H.1: JBuilder X Tutorial. For Introduction to Java Programming, 5E By Y. Daniel Liang

FROM 4D WRITE TO 4D WRITE PRO INTRODUCTION. Presented by: Achim W. Peschke

What s New in Enterprise Jeff Simpson Sr. Systems Engineer

1. I NEED TO HAVE MULTIPLE VERSIONS OF VISUAL STUDIO INSTALLED IF I M MAINTAINING APPLICATIONS THAT RUN ON MORE THAN ONE VERSION OF THE.

7 The Integrated Debugger

10 cool tools in Windows 7

Debugging and Handling Exceptions

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Writing to and reading from files

QUIZ. What is wrong with this code that uses default arguments?

Unit E Step-by-Step: Programming with Python

How to update Windows and Office offline

Getting started with the Spyder IDE

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

CSE 361 Fall 2017 Lab Assignment L2: Defusing a Binary Bomb Assigned: Wednesday Sept. 20 Due: Wednesday Oct. 04 at 11:59 pm

Creating a MOM 2005 Peformance Graph Report From a Template

Chapter 4: Programming with MATLAB

MEAP Edition Manning Early Access Program Get Programming with Java Version 1

SUPER SAVE TOOLS Super Save Your Scenes, Super Save Your Time!

There are two main workflows for working with Cordova projects, Web focused and Platform focused.

4 Working with WSH objects

Debugging INTRODUCTION DEBUGGER WHAT IS VBA'S DEBUGGING ENVIRONMENT?

EEN118 LAB FOUR. h = v t ½ g t 2

Supplement: Visual C++ Debugging

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

1 Preface About this Manual Intended Audience Revision History Document Conventions Version...

COPYRIGHTED MATERIAL. Part I: Getting Started. Chapter 1: IDE. Chapter 2: Controls in General. Chapter 3: Program and Module Structure

The Very Basics of the R Interpreter

Code::Blocks Student Manual

Supplement II.B(1): JBuilder X Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Visual Studio 2010 Silverlight No Symbols Have Been Loaded For This Document

Testing and Debugging

Semester 2, 2018: Lab 1

Introduction to Computation and Problem Solving

COMP 105 Homework: Type Systems

How To Upload Your Newsletter

Web Design Competition Tutorial. Designed for use by the Michigan Council of Women in Technology Copyright 2018 Patricia Howard All Rights Reserved

Open two copies of Visual Studio one for VSGraph, one for VisualStudioTips

Azure Developer Immersions Application Insights

First, let s just try to run the program. When we click the button we get the error message shown below:

Chapter 9: Dealing with Errors

CS1 Lecture 5 Jan. 25, 2019

Visual Studio 2008 Load Symbols Manually

Experiment N o 1. Introduction to Assembly Language Programming

Warewolf User Guide 1: Introduction and Basic Concepts

Visual Studio.NET. Although it is possible to program.net using only the command OVERVIEW OF VISUAL STUDIO.NET

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

COPYRIGHTED MATERIAL. Using Adobe Bridge. Lesson 1

Templates Templates are functions or classes that are parameterized. We have already seen a few in STL:

Integrated Software Environment. Part 2

EW The Source Browser might fail to start data collection properly in large projects until the Source Browser window is opened manually.

Programming Style. Quick Look. Features of an Effective Style. Naming Conventions

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Computer Science II Lab 3 Testing and Debugging

NetBeans Tutorial. For Introduction to Java Programming By Y. Daniel Liang. This tutorial applies to NetBeans 6, 7, or a higher version.

Lab 3-2: Exploring the Heap

A Tutorial on using Code::Blocks with Catalina 3.0.3

Installing the PC-Kits SQL Database

Web Design Competition File Upload Tutorial

JCreator. Starting JCreator

Clickteam Fusion 2.5 Creating a Debug System - Guide

Reliable programming

Prerequisites for Eclipse

Transcription:

Debugging Runtime Scripts in Operations Manager and Essentials 2007 The third installment in the System Center Forum Scripting Series Author: Neale Brown, MCSE (Messaging) Contributor, System Center Forum Version: 1.0 March 2008 Some Rights Reserved: You are free to use and reference this document and it s, so long as when republishing you properly credit the author and provide a link back to the original published source.

Contents Previous Installments... 3 Introduction... 3 Choosing a Debugger... 4 Office Debugger (Web Script Editor)... 4 Instantiating the debugger... 4 Visual Studio (2005 or 2008) Debugger... 6 Debugging Ops Mgr 2007 Script Issues... 8 Test Script... 10 Conclusion... 10 System Center Forum http://www.systemcenterforum.org 2

Previous Installments Part 1: Updating MOM 2005 Runtime Scripts for OpsMgr and SCE Part 2: Custom Performance Objects in Runtime Scripts Introduction I can think of few things more frustrating than an Operations Manager script that s not working as you want it to. Fortunately, if your script is stuck in a loop or just returning the wrong data, there are a couple of ways you can troubleshoot this issue. Pete covered the basics of testing your scripts from a command line using the Cscript engine (see Part 1 of the Scripting Series), but I will go a step further cover two simple debugging methods for of accessible to most non-developers (IT Pros). These come in handing when looking up errors on the search engines doesn t yield results. The first way is to simply add extra WSCript.Echo commands to a script to display values assigned to variables at key points in your script or log that data into a file. For example, during an IF statement you can use a WScript.Echo to test your logic and display the result of the expression you re testing. For example: IF File.size > 50 Wscript.echo True: & File.size ELSE Wscript.echo False: & File.size END IF You can use the Windows Scripting Host (WSH) interpreter, which includes the CScript and WScript engines, to debug the script by using either the /D or /X switch. o /D This switch will enable Active Debugging. This means that debugging will start if an unhandled exception happens or if a stop statement is used. o /X This switch will start debugging before the script is interpreted. This means that you can step through the script by setting breakpoints and watchpoints to make sure the script is executed properly. This switch is extremely useful for troubleshooting erroneous results and faulty logic. Using the /X switch using Cscript is the best way to debug a script and is an excellent tool for debugging Ops Mgr scripts before deploying in a MP. System Center Forum http://www.systemcenterforum.org 3

Choosing a Debugger By default, a debugger is not included with the WSH interpreter so if you want do debug your script, you will need to install one. There are a couple of common Microsoft products that include the ability to debug a script. The most common products are Microsoft Office and Microsoft Visual Studio. Office Debugger (Web Script Editor) There is a component included with Microsoft Office that is a script editor and will register itself as a debugger. This feature is a part of the Office Tools section and is called Microsoft Script Editor (HTML Source Editing). This feature also includes Web Scripting and Web debugging. Instantiating the debugger Once this feature is installed, you can use the following command to instantiate the debugger: C:\>cscript.exe /x c:\test.vbs The next window should be a list of available debuggers on your machine. You should select New Instance of Microsoft Script Editor. System Center Forum http://www.systemcenterforum.org 4

As you can see, there are multiple debuggers listed in this prompt. We will also go over using Visual Studio 2005 as a debugger later in this article. When you click Yes, it should bring up a window with your script and a line of code highlighted. The highlighted area is the starting point for the debugging. To get you started, here are some key commands that can be used in the debugger: Quick Watch (Ctrl+Alt+Q) This can be used to watch a variable throughout the script to verify that the correct information is being stored. Step Into (F11) This is used to step through each line of the script. This is nice if your script is small and has very FEW loops (stressing the word few). IF you have too many loops, you will want to make use of the next command. Step Out (Shift+F11) If you are in a function and want to break out to the next line of code outside the function, then use. This could be handy if you have verified the function works correctly or is not associated with the problem. Step Over (F10) If the next call is a function with other nested functions or loops, then this will execute the function and halt at the first line of code after the function Note: Some of the commands are available (Step Execution) from the Context menu by right clicking on a line or variable. Stay way from using F5 (Continue) if you want to step through your script. This will go through the script and will quit when it encounters an error, a stop statement, or the end of the script. System Center Forum http://www.systemcenterforum.org 5

Visual Studio (2005 or 2008) Debugger Visual Studio is a very nice application development environment that also includes debugging features that Windows Scripting Host (WSH) interpreter can use. Installing Visual Studio just for script debugging is overkill but there is one reason to do it if you do not have office installed. You can obtain Visual Studio 2008 Express Edition for free! If you don t want to pay for an Office license, then you can just obtain the Visual Studio Express Edition to do script debugging. You will find the interface to be nicer than the MS Script Editor s (MSE) vanilla interface. The commands and shortcuts listed for the MSE will also work for Visual Studio. The example below is using Visual Studio 2005 Professional Edition but you can download any Visual Studio Express edition. System Center Forum http://www.systemcenterforum.org 6

You can use the following command to instantiate the debugger: C:\>cscript.exe /x c:\test.vbs The next window should be a list of available debuggers on your machine. You should select New Instance of Visual Studio 2005 When you click Yes, the next screen should be Visual Studio 2005 debugging the script with the first block of code highlighted. The highlighted line is the starting point for the debugger. The Visual Studio debugger will have the same set of commands and shortcuts as the MSE has but the VS debugger has a window called Local that has the local variables listed in it. If you need to review content in the variables, then this debugger will fit your needs by default. To get you started, here are some key commands that can be used in the debugger. Quick Watch (Ctrl+Alt+Q) This can be used to watch a variable throughout the script to verify that the correct information is being stored. Since VS has the Locals pane, you do not really need to use this feature since it is already activated. Step Into (F11) This is used to step through each line of the script. This is nice if your script is small and has very FEW loops (stressing the word few). If you have several loops in your script, you will want to make use of the next command. Step Out (Shift+F11) If you are in a function and want to break out to the next line of code outside the function, then use. This could be handy if you have verified the function works correctly or is not associated with the problem. System Center Forum http://www.systemcenterforum.org 7

Step Over (F10) If the next call is a function with other nested functions or loops, then this will execute the function and halt at the first line of code after the function Note: Some of the commands are available (Step Execution) from the Context menu by right clicking on a line or variable. As you can see, the Visual Studio debugging interface and MSE debugger for WSH is very similar. It is really up to you as to which debugger you want to use. If you can spare the Office License or you debug on your desktop, then I would suggest using MSE. If you don t want to use an Office license, then download one of the Visual Studio Express editions. Debugging Ops Mgr 2007 Script Issues Sometimes scripts deployed in management packs may work on some servers but not others. The event logged by Opsmgr to the Operations Manager Event Log will include the path to the script and the command line parameters but really doesn t tell you why. Sure it gives you a reason, but WHY are you getting that reason. What is exactly going on behind the scenes? Well, the debugging tools listed above System Center Forum http://www.systemcenterforum.org 8

can help in a big way with the information given by the Ops Mgr logged event. In this section we will go over some simple steps to help you debug your Ops Mgr scripts. Here is a sample Ops Mgr event log error from a failing script. Even though this maybe an obvious error, we will go through a couple of different steps to troubleshoot the problem. Event Type: Warning Event Source: Health Service Modules Event Category: None Event ID: 21413 Date: 3/2/2008 Time: 8:40:38 PM User: N/A Computer: NOCMS03 Description: The Event Policy for the process started at 8:40:38 PM has detected errors in the output. The expression: \a+ matched the following output: Command executed: "C:\WINDOWS\system32\cscript.exe" /nologo "TestScript.vbs" Working Directory: C:\Program Files\System Center Operations Manager 2007\Health Service State\Monitoring Host Temporary Files 4\2164\ One or more workflows were affected by this. Workflow name: MomUIGeneratedRule2988e7c333c74034a97e999d7a7de78f Instance name: NOCMS03.noc.momresources.org Instance ID: {096CF035-C7B7-957F-71F2-DEFE8425E19C} Management group: LAB As you can see, there was an error with the script but we are not sure what the error. Fortunate for us we have plenty of information in this script to debug the problem. Let s start out with Steps 1. 1. Running the Script Since the event gives us the directory and name of the script, we can run it from that directory using CScript.exe and CMD.exe. We should get the interpreter error which will help solve the problem. a. Open CMD.exe from the Run command on the Start Menu. b. Change directories to the Working Directory listed in the Event c. Run the script using the Command Executed line in the Event and monitor the output. 2. Debug the Script If step one doesn t reveal a script problem, but the script is still not working then it is time to use the /X switch. a. Open CMD.exe from the Run command on the Start Menu. b. Change directories to the Working Directory listed in the Event. System Center Forum http://www.systemcenterforum.org 9

c. Run the script using the Command Executed line in the Event but in this case we add the /x switch. Here is what it would look like from the event above: C:\WINDOWS\system32\cscript.exe" /nologo /x "TestScript.vbs d. Select the debugger and run through the script carefully to find the problem. Once you have found the problem, you can run the script again to verify you have fixed it. In this case, by performing steps 1 you have found that CScript.exe is complaining about an undefined variable (this is because the Option Explicit is declared in the script). Once that is resolved the script will operate properly. Test Script The text box below contains the script from the last debugging example above. Option Explicit 'declare constants 'map event types & numbers to friendly names Const EVENT_TYPE_ERROR = 1 Const EVENT_TYPE_WARNING = 2 Const EVENT_TYPE_SUCCESS = 4 Dim inttestvar Set oapi = CreateObject("MOM.ScriptAPI") If inttestvar = 1 Then Call oapi.logscriptevent("testscript.vbs", 10101, EVENT_TYPE_SUCCESS, _ "TestScript.vbs: Success") Else Call oapi.logscriptevent("testscript.vbs", 10102, EVENT_TYPE_ERROR, _ "TestScript.vbs: Failed") End If Conclusion We hope you find this information helpful. Your feedback is always welcome through our Contact Page at http://www.systemcenterforum.org/contact. System Center Forum http://www.systemcenterforum.org 10