Archive for June 2010

Service Manager Announcements   Leave a comment

I’ve written in previous blogs on how to get data out of Service Manager, and generally, that data is usually simple text, numbers or sometimes an enumeration (which is pretty easy to convert to text). However, Service Manager also allows you to store text with formatting (rich text data) which can be pretty difficult to view. First, let’s create an announcement:

We can use one of the scripts created in an earlier posting to retrieve an instance of the announcement:

PS> get-smclass announcement.item|get-scsmobject|fl
Id             : 2
Title          : Announcement 001
Body           : {\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0
                 Times New Roman;}{\f2\fcharset0 Sego
                 e UI;}{\f3\fcharset0 Calibri;}{\f4\fcharset0 Copperplate Gothic
                 Bold;}}{\colortbl\red0\green0\blue0;\r
                 ed255\green255\blue255;\red255\green0\blue0;}
                 {\*\listtable
                 {\list\listtemplateid1\listhybrid
. . .
                 b0\jclisttab\tx720\fi-360\ql\par}
                 }
                 }
ExpirationDate : 6/28/2010 7:00:00 AM
Priority       : System.Announcement.PriorityEnum.Medium
DisplayName    : Announcement 001
Type           : System.Announcement.Item
Name           : 2
Path           :
FullName       : System.Announcement.Item:2

Since the body property of the announcement is rich text, it really isn’t readable in this format, but we can fix that with a fun little script. This is one of those scripts that allow us to mix the command line and the graphical environment. We’ll call this script Display-RichText.ps1 since that’s what it does!

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
param (
$string
)
begin
{
[void][reflection.assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void][reflection.assembly]::LoadWithPartialName(“System.Drawing”)
## the form
$form = new-object System.Windows.Forms.Form
$form.size = new-object System.Drawing.Size 400,400 

## the Rich text box
$text = new-object System.Windows.Forms.RichTextBox
$text.multiline = $true
$text.dock = “Fill”
$text.scrollbars = “Both”
$text.width = 80

## Quit button
$QuitButton = new Windows.Forms.Button
$QuitButton.Name = “QuitButton”
$QuitButton.TabIndex = 0
$QuitButton.Text = “Quit”
$QuitButton.UseVisualStyleBackColor = $true
$QuitButton.Add_Click({$form.dispose()})
$QuitButton.Dock = “Bottom”

$form.controls.add($text)
$form.controls.add($QuitButton)
function loadtext
{
param ( $string )
try
{
$bytes = [byte[]]($string.ToCharArray())
$stream = new-object io.memorystream $bytes,$true
$text.loadfile($stream, “richtext”)
$text.DeselectAll()
[void]$form.showdialog()
}
finally
{
$stream.close()
$stream.dispose()
}
}

}
end
{
loadtext $string
}

Now let’s see what we can do!

PS> $announcement = get-smclass announcement.item|get-scsmobject
PS> display-richtext $announcement.body

Now we can see the contents of the announcement!

Advertisements

Posted June 13, 2010 by jtruher3 in ServiceManager

Saving Service Manager Instances in a CSV file   Leave a comment

In a post I did a few months ago, I wrote how it you can retrieve data from Service Manager via a PowerShell script, but I didn’t show much more than just plain output. Earlier this week I was asked if it would be possible to export configuration items in a CSV file. By using the script I wrote months ago, we have all the tools we need. Let’s review the previous script:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
param ( $classname )
$emg      = new-object microsoft.enterprisemanagement.enterprisemanagementgroup localhost
$class    = $emg.EntityTypes.GetClasses()|?{$_.name -eq $classname}
if ( ! $class )
{
    Write-Error "`nERROR: Class ‘$classname’ not found, exiting."
    exit
}
$DEFAULT  = [Microsoft.EnterpriseManagement.Common.ObjectQueryOptions]::Default
$EMOT     = [Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject]
# Retrieve the interface for EntityObjects, which we’ll use when we create our generic method
$IMGMT    = $emg.EntityObjects.GetType()
# the types of the parameters, this is so we can find the right method
[type[]]$TYPES = [Microsoft.EnterpriseManagement.Configuration.ManagementPackClass],
                 [Microsoft.EnterpriseManagement.Common.ObjectQueryOptions]
# Retrieve the method
$ObjectReader = $IMGMT.GetMethod("GetObjectReader",$TYPES)
# Create a generic method
$GenericMethod = $ObjectReader.MakeGenericMethod($EMOT)
# Invoke the method with our arguments
[array]$arguments = [Microsoft.EnterpriseManagement.Configuration.ManagementPackClass]$class,$DEFAULT
$GenericMethod.invoke($emg.EntityObjects,$arguments) | %{
    # Create a custom object based on the original object
    $o = new-object psobject $_
    # elevate the properties in the Values collection to the top level
    $o.values|%{ $o | add-member -force NoteProperty $_.Type $_.Value }
    # assign a synthetic typename to the object, so we can use our formatting
    # more easily
    $name = $_.GetLeastDerivedNonAbstractClass().name
    $o.psobject.typenames.Insert(0, "EnterpriseManagementObject#$name")
    # now, emit the object!
    $o
    }

We’ll call this script get-object (and it assumes that you’ve already loaded the Microsoft.EnterpriseManagement.Core.dll), and just to remind you, here’s how it works:

PS> get-object microsoft.windows.computer|format-table DisplayName,Netbios*,PrincipalName -au

DisplayName                   NetbiosComputerName NetbiosDomainName PrincipalName
-----------                   ------------------- ----------------- -------------
JWT-SCDW$                     JWT-SCDW            WOODGROVE         JWT-SCDW.woodgrove.com
WIN-752HJBSX24M.woodgrove.com WIN-752HJBSX24M     WOODGROVE         WIN-752HJBSX24M.woodgrove.com

You provide a class name to it and it retrieves all the instances of that class. Now, to save these instances in a CSV file, I just need to use the Export-CSV cmdlet that is part of the PowerShell distribution and viola!

PS> get-object microsoft.windows.computer|Export-CSV Microsoft.Windows.Computer.csv
PS> get-content .\Microsoft.Windows.Computer.csv
#TYPE EnterpriseManagementObject#Microsoft.Windows.Computer
"PrincipalName","DNSName","NetbiosComputerName","NetbiosDomainName","IPAddres...
"JWT-SCDW.woodgrove.com","JWT-SCDW.woodgrove.com","JWT-SCDW","WOODGROVE",,,"S...
"WIN-752HJBSX24M.woodgrove.com","WIN-752HJBSX24M.woodgrove.com","WIN-752HJBSX...

You can use this to create a copy of your data, or use it as a way to exchange your data with other applications. Because of the way that other applications use CSV files, you may need to remove the first line of the CSV file which describes what the object was from the PowerShell perspective, but otherwise, you should be able to use the CSV file easily!

Posted June 4, 2010 by jtruher3 in ServiceManager