Stefan Stranger's Lifestream - tagged with scripts http://www.stranger.nl/feed en-us http://blogs.law.harvard.edu/tech/rss Sweetcron stefan@stranger.nl Using PSExec to troubleshoot OpsMgr issues http://www.stranger.nl/items/view/6169

As you probably know OpsMgr quite some people use the local system account as Action Account. And if things are not working as expected you sometimes want to run a script or other actions under the local system account. I used to use the Task Scheduler to have scripts running under the Local System Account, but now I learned you can easily use the PSExec tool of SysInternals to do the same:-) How does this work? You can download the tool and install it on the systems you want to do your troubleshooting or just use the live share on http://live.sysinternals.com/ I created a quick and dirty PowerShell script that writes the owner of the PowerShell process to the PowerShell eventlog.

######################################################################################## # Write Owner of PowerShell Process to PowerShell Eventlog # Authors: Stefan Stranger # ScriptName: UserAccountDebugging.ps1 # v1.000 - 24/03/2010 - stefstr - initial sstranger's release (quick & dirty version)
######################################################################################## #Function Write-EventLog($Description) # #Writes Owner of PowerShell process to PowerShell Eventlog. ############################################################################################## function Write-EventLog($Description) {     $source = "PowerShell(PowerShell)"     [string]$type = "Information"     [int]$eventid = 999         if(![System.Diagnostics.EventLog]::SourceExists($source))         {             [System.Diagnostics.EventLog]::CreateEventSource($source,'Windows PowerShell')         }         else          {                 $log = New-Object System.Diagnostics.EventLog                  $log.set_log("Windows PowerShell")                  $log.set_source($source)                 $log.WriteEntry($Description,$type,$eventid)         }

} $processes = Get-WmiObject Win32_Process -Filter "name='powershell.exe'" $appendedprocesses = foreach ($process in $processes) {Add-Member -MemberType NoteProperty -Name Owner  -Value ($process.GetOwner().User) -InputObject $process -PassThru} $owners = ($appendedprocesses | select owner) foreach ($owner in $owners) {     $evtdescription = "PowerShell process is being run under the next account: "  + $owner.Owner     Write-EventLog $evtdescription }

Do whatever you wanted to do in the PowerShell script for your OpsMgr environent

Write-Host "Hello World" Save above script as UserAccountDebugging.ps1. If we run the above script with our logged on user account we get the next result:

Result in Eventviewer

    Now let’s start PSExec and run the PowerShell script with the local system account. Open Command prompt (as Administrator) and type: psexec –i –d –s powershell.exe

Now a new PowerShell Window will be opened as Local System Account.

Let’s now run the PowerShell script again and check the owner of the PowerShell process. Close all PowerShell sessions first ;-)

      Have fun using PSExec to debug OpsMgr Permissions issues with the local system account.

]]>
Wed, 24 Mar 2010 16:29:00 +0100 http://www.stranger.nl/items/view/6169
MP Authoring: Extending a class attribute from a file http://www.stranger.nl/items/view/6138

Sources: Steve Rachui’s Manageability blog – ConfigMgr/OpsMgr and                Savision Live Maps Blog Is this a coincidence or what? I’ve been asked to take a look at some new blog articles from Savision about adding a ‘location’ attribute to the Windows Server class. And you can now read their first blog article How-To: Import Computer Location Information Into OpsMgr (Part 1-3) in a series of three. Savision explains how this can be achieved in three steps: Create a management pack that adds a ‘location’ attribute to the Windows Server class Write a connector that reads asset information from a CSV file and updates OpsMgr Configure Live Maps to create dynamic location aware maps Steve Rachui also extends in his blog article the class attributes from a flat file. But he uses a discovery vbscript to read the flat file instead of the connector being used/explained (in later blog posts) by Savision. It’s up to you what you choose to use. Have fun extending class attributes!

]]>
Tue, 16 Mar 2010 21:19:00 +0100 http://www.stranger.nl/items/view/6138
Anybody wants to test my OpsMgr MP Authoring PowerGUI PowerPack? http://www.stranger.nl/items/view/6134

I just created my first PowerGUI PowerPack which you can use together with the MP Authoring Console. Just like I blogged previously in my MP Authoring Helper PowerShell script here. Here a teaser screenshot of the PowerGUI PowerPack.   It’s just a first version I created and I would like to have it tested a little more before posting it on the PowerGUI PowerPack library. So if anybody is creating MP’s using the MP Authoring Console and wants to try to use the MP Authoring PowerPack within PowerGUI to help creating MP’s let me know via Twitter via a DM or use the contact form on my weblog and I’ll email my PowerPack for testing.

]]>
Mon, 15 Mar 2010 21:24:00 +0100 http://www.stranger.nl/items/view/6134
Basic Troubleshooting Discovery Script (follow up on Kevin’s Post) http://www.stranger.nl/items/view/6101

This week I’m having a MP Authoring Workshop from Brian Wren in the UK and one of the modules of this workshop is Discovery. And to my surprise Kevin Holman published a blog post on Basic troubleshooting of discovery script yesterday. It’s a good start but I’ve some more help on troubleshooting a Discovery script. Here are mine (with some help of Brian): Verify that MP has been committed on agent Check agent for eventlog errors Check management server for errors Verify that discovery is running Enable debug events in script Debug script on agent   Step 1. Verify that MP has been committed on agent. How? There are several ways. One of them is looking in the OpsMgr eventlog for EventId 1201. Step 2. Check for agent errors. How? Check for OpsMgr EventId’s like 31876 or 21405 or other Health Service Module source errors. Step 3. Check for management server errors How? Check for OpsMgr EventId’s 10801 Step 4. Verify that discovery is running How? You can look for your discovery script in the C:\Program Files\System Center Operations Manager 2007\Health Service State\ subdirectories with the next command: dir /B /S mycooldiscovery.vbs Did you found it? I hope so;-) More info here. Now you can use Process Monitor from SystInternals to monitor your cscript vb discovery script. For more info on how to use Process Monitor go to Jeevan Bish’s blog. Step 5. Enable debug events in script. How? You can add a debug function to you discovery script that writes info the eventlog using oAPI.LogScriptEvent in your discovery script. For more info on using oAPI.LogScriptEvent take a look at MSDN. Step 6. Debug script on Agent. How? See Kevin’s post on how to do this. You can alslo take a look at Brian Wren’s Demo Store App Management on OpsManJam for an LogDebugEvent Function. ' '================================================================================== ' Sub:        LogDebugEvent ' Purpose:    Logs an informational event to the Operations Manager event log '            only if Debug argument is true '================================================================================== Sub LogDebugEvent(EventNo,Message)     Message = VbCrLf & Message     If bDebug = True Then         Call oAPI.LogScriptEvent(SCRIPT_NAME,EventNo,EVENT_LEVEL_INFO,Message)     End If End Sub More blogposts on MP Authoring after this week of MP Authoring training.

]]>
Wed, 10 Mar 2010 18:49:00 +0100 http://www.stranger.nl/items/view/6101
Download OpsMgr Guides in one-click http://www.stranger.nl/items/view/5928

Have you ever tried to download the files on the System Center Operations Manager 2007 R2 Documentation webpage? On this page you find all the technical documentation for Operations Manager 2007 R2. Pretty handy to have them downloaded on you workstation for offline reading. But as you see you need download each file separately :-( And if you are as lazy as me you don’t like that. That’s why I created a PowerShell script using BitsTransfer to do this in one-click. Cool? I think so. ############################################################################### # Download all OpsMgr Guides from # http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=19bd0eb5-7ca0-41be-8c0f-2d95fe7ec636 # in one-go using PowerShell and Bits. # Remark: Use PowerShell 2.0 because it makes use of the BitsTransfer Module # Author: Stefan Stranger # v1.001 - 19/02/2010 - stefstr - initial release ############################################################################### $global:path = "c:\Temp\" Import-Module BitsTransfer #Loads the BitsTransfer Module Write-Host "BitsTransfer Module is loaded" $OpsMgrGuides = @("http://download.microsoft.com/download/7/4/d/74deff5e-449f-4a6b-91dd-ffbc117869a2/Linked.Reporting.MP.xml", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007_AuthGuideXplat.exe", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007_ReportAuthoringGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_CrossPlatformMPAuthoringGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_CrossPlatformMPAuthoringGuide_Samples.zip", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_CrossPlatformMPAuthoringGuide_Samples.zip", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_DesignGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_DeploymentGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_MPAuthoringGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_MPModuleReference.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_OperationsAdministratorsGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_OperationsUsersGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_SecurityGuide.docx", "http://download.microsoft.com/download/B/F/D/BFDD0F66-1637-4EA3-8E6E-8D03001E5E66/OM2007R2_UpgradeGuide.docx") Foreach ($OpsMgrGuide in $OpsMgrGuides) { Start-BitsTransfer $OpsMgrGuide $path} Write-Host "OpsMgr Guides are downloaded to $path" Just copy above script and save it to DownLoadOpsMgrGuides.ps1 and run it from PowerShell 2.0. Screenshots:   Have fun with the OpsMgr Guides and using PowerShell!

]]>
Fri, 19 Feb 2010 10:43:00 +0100 http://www.stranger.nl/items/view/5928
PowerShell: State Changes for a specified Monitor http://www.stranger.nl/items/view/5121

This week a got a question from a customer about ‘flapping’ taking place from a Monitor. I found out that they meant the State Changes that took place for a newly created Monitor. They found out that for a certain machine the State changed quite often and this was caused by a Recovery Task that was part of this monitor. So they wanted to know if there was a way to see quickly if a Monitor caused a lot of State Changes. There are quite some SQL queries you can use to have a look at the State Changes taken place in your OpsMgr environment. Just take a look at some of the SQL queries on the weblog of my colleague Jonathan Almquist. But what if you only want to know the State Changes that took place for a specific Monitor? Again you can use one of Jonathan’s SQL queries and change them to fit your needs. And this is exactly what I’ve done but I also wanted to run the query from the OpsMgr Command Shell. The reason for this was that I easily wanted to retrieve the Monitor Name using the OpsMgr Get-Monitor Cmdlet.   So here is the PowerShell script I created. You can change the SQL query if you want, to fit your own needs ;-) Result screenshot: Have fun with OpsMgr and PowerShell!

]]>
Wed, 09 Dec 2009 14:51:00 +0100 http://www.stranger.nl/items/view/5121
Have these servers an OpsMgr agent installed? http://www.stranger.nl/items/view/5057

Today I got a question from a customer if I would check if a number of servers had an OpsMgr agent installed? Because this was a large list and the number of servers being monitored by OpsMgr was also a very large list I didn’t wanted to to copy and paste all the server names for the text file and check them in the OpsMgr Console. So I created a PowerShell script to check if an OpsMgr Agent was installed for each of the servers in the file list. The first script I created was not very fast because it did a get-agent for each of the servers in the list and that takes quite some time in a large environment ;-) So I created a new one that’s much faster. Let’s start with the slow script: First you need to put all the servers you want to check in a file, like this:

servername1 servername2 servername3 servername4 servername5 servername6 servername7 servername8 servername9 servername10 servername11 dc8

Save this list in a text file like d:\temp\servers.txt Now run the next script in the OpsMgr Command shell:

$servers = get-content d:\temp\servers.txt foreach ($server in $servers) {get-agent | where {$_.ComputerName -eq $server} | select Name} As you can see it takes almost 12 seconds to do this the slow way.

And now the fast way.

$Servers = get-content c:\temp\servers.txt $Agents = get-agent | select ComputerName Foreach ($agent in $Agents) {if ($servers –contains $agent.ComputerName) {$agent | select ComputerName}}

So the next time someone is asking you if an OpsMgr agent is installed on their servers you now it in seconds. Have fun using PowerShell!

]]>
Thu, 03 Dec 2009 22:31:00 +0100 http://www.stranger.nl/items/view/5057
My Operations Manager 2007 SQL Queries http://www.stranger.nl/items/view/2825

Jonathan Almquist started a blog post about Operations Manager 2007 SQL Queries. “I’m starting a new post here, similar to my Command Shell reference.  This will include some useful SQL queries that I happen to need and direct my customers to on a daily basis.  If you don’t see something here, check Kevin Holman’s blog.  He’s already got a library of useful queries posted, in which I will not duplicate here. I will continue to update this table periodically.  If you subscribe to my blog, you will receive each new example as I post it.” I also have some SQL queries I used at customers I want to share. Would be nice if we had some kind of Wiki where we could post all our OpsMgr SQL queries though ;-)

OperationsManager OperationsManagerDW

  Logical Free Megabytes on Windows Server 2000 and higher

 

% Logical Disk Free Space on Windows Server 2003

List all Windows Computers  

Total Number of Servers per OS  

]]>
Sat, 25 Apr 2009 17:18:00 +0200 http://www.stranger.nl/items/view/2825
Response: What’s the diff? Or – how to see exactly what is new with an updated MP http://www.stranger.nl/items/view/2293

Source: Kevin Holman’s OpsMgr Blog Kevin blogged about ways to find the differences between different versions of a MP. After converting the MP’s to XML files he showed you could use XML Notepad or Beyond Compare to show the differences between the two MP’s. He ends with the question if you have some better/cooler XML diff tools to let him know. I found another ways of finding the differences but I don’t think these are any better, because they still doe not provide the summary in the context of OpsMgr. Ok, how could you find the differences between a MP? First you could PowerShell. PowerShell has a Cmdlet called Compare-Object. This Cmdlet takes two resultsets and automatically analyzes them. It then outputs only those items present in either one of the result sets. Steps: Export the MP’s to XML files Get the contents of the to XML converted MP files Run Compare-Object Cmdlet Complete PowerShell script $version1 = get-content C:\temp\Compare\version1\System.Mom.BackwardCompatibility.Library.xml $version2 = get-content C:\Temp\Compare\Version2\System.Mom.BackwardCompatibility.Library.XML Compare-Object $version1 $version2 But as said before I don’t think this would really be an improvement above the ways Kevin mentions. The second method is using Compare Sheets in Excel. First export the MP’s to Excel with the MPViewer tool from Boris and doing the comparison within Excel. I’ve found some great Excel tools from JMT Excel Consulting which have a Compare Sheets macro you could use to do the comparison. Only problem with this Macro is you have to select each Worksheet manually before you can do the comparison. So the question still remains do you better ways of finding the differences between MP’s with some OpsMgr context?

]]>
Sun, 15 Mar 2009 15:46:00 +0100 http://www.stranger.nl/items/view/2293
Using Script Debugger for debugging OpsMgr Scripts http://www.stranger.nl/items/view/1689

When I was watching the recording from the January 2009 SCVUG Meeting, Pete Zerger showed that you could use a script debugger when debugging scripts. Till now I had not used a script debugger so I thought why not enable the just-in-time script debugger from Visual C# 2008 Express Edition I’ve running on my laptop? But it turned out that Just-In-Time Debugging is not available in the Express Edition. Don’t worry you can download and install the Script Debugger for Windows NT 4.0 and Later and use that when you want to debug your OpsMgr scripts. How do you debug your scripts? Just open the command prompt and type: c:\cscript.exe /x loveopsmgr.vbs

]]>
Sun, 25 Jan 2009 15:43:00 +0100 http://www.stranger.nl/items/view/1689
Audio / Video from the January 2009 SCVUG Meeting http://www.stranger.nl/items/view/1690

Source: System Center Forum Today I was watching the latest recording of the January 2009 SCUG Meeting and to my surprise I was seeing some info about Debugging Scripts which I blogged about some time ago.    So if you didn’t had time to join the livemeeting go download the recording. Below are the starting times in the video for each speaker so you can fast forward to whatever you like. 1:05 - Savision Live Maps v3 Demo (Dennis Rietvink) 51:46 - Custom Scripting and Script Debugging in OpsMgr 2007 (Pete Zerger) 1:19:45 - Top MS Support Issues for Operations Manager 2007 (Steve Rachui) 

]]>
Sun, 25 Jan 2009 15:15:00 +0100 http://www.stranger.nl/items/view/1690
Debugging OpsMgr Scripts http://www.stranger.nl/items/view/1057

One of the great advantages of OpsMgr 2007 against MOM 2005 is that you can easily test OpsMgr 2007 scripts from the command prompt. Just go to the C:\Program Files\System Center Operations Manager 2007\Health Service State and open a command prompt. Type DIR /B /B *.vbs and you find all the scripts that are used on that specific agent. And if you have found your script to debug you can easily run it with cscript and see what happens. But what if a scripts needs some parameters? Most of the time these are GUIDS and how do you find the right parameters? A colleague of mine Dirk van Coeverden found an easy way to find these parameters. Let’s Look at the Operational Database Space Free (%) Monitor. This Monitor uses a vbscript GetOpsMgrDBPercentageFreeSpace.vbs which needs two parameters to work So if we want to debug this script we need two parameters; DatabaseServerName and DatabaseName. How do we find those parameters? First we need to go to the Authoring Pane in the Opsmgr Console and find the monitor which runs this script. Then we change the default timeout of the script to something smaller then the default with an override. Say 1 second. Now we have to wait for this monitor to run again and hopefully see that the script timeouts because of the lower timeout settings. And if the script runs longer than the timeout period an event 21402 – Script ran longer than the timeout period will be created. Now you can debug the script from the commandprompt with the correct parameters. Don’t forget to remove the overrides.

]]>
Sun, 07 Dec 2008 16:18:00 +0100 http://www.stranger.nl/items/view/1057