Using PowerShell to add update and delete Class instances in SCOM

29 Nov

Challenge

This short post will provide some PowerShell scripting to let you play with class instances in SCOM. Since there’s not much information on the web on this topic and I had a request from a partner I will provide some code below how to handle this.

In the examples below I will use a class from the OpsLogix VMware management pack, but you can use any (public) class you want. All code just be run on a SCOM MS server. All examples are separate runnable.

Add a class instance

In the example below, I will add a new VMWare ESX host to a datacenter belonging to a vCenter connection. As below:

Before we run the script, we will have to know the Key properties so we can configure the parent child relationship. In this case the we have the following relationship:

vCenter01:7443 -> Datacenter01 -> My_ESX_Host

In this case the Host we want to add is called “My_ESX_Host” and we set the “AssetTag” to “unkown” .

## ======================================================================

## example script how to add a class instance in SCOM

## ======================================================================

## Michel Kamp

## ======================================================================

New-SCOMManagementGroupConnection
-ComputerName
“localhost”

$mg
=
Get-SCOMManagementGroup

$ClassInstanceDisplayName=
“My_ESX_Host”

$Class
=
Get-SCOMClass
-Name
OpsLogix.IMP.VMWare.ESXHost

##================= create/add class instance

$ClassObject
=
New-Object
Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($mg,$class)

# set the KEY props

$ClassObject[$Class.FindHostClass().FindHostClass(),“vCenterHostName”].Value = “vCenter01:7443”

$ClassObject[$Class.FindHostClass(),“DatacenterName”].Value = “Datacenter01”

$ClassObject[$Class,“ESXServerName”].Value =
$ClassInstanceDisplayName

# Set NON key props

$ClassObject[$Class,“AssetTag”].Value = “unkown”

# add and write back to scom

$discovery
=
New-Object
Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData

$discovery.Add($ClassObject)

$discovery.Overwrite($mg)

##================= END ================================================

Change a property of a class instance

Below we change a property called “AssetTag” to “test tag 123” of the ESXHost class instance “My_ESX_Host”. Keep in mind that only NON-Key properties can be changed.

See script below:

## ======================================================================

## example script how to change a class instance in SCOM

## ======================================================================

## Michel Kamp

## ======================================================================

New-SCOMManagementGroupConnection
-ComputerName
“localhost”

$mg
=
Get-SCOMManagementGroup

$ClassInstanceDisplayName=
“My_ESX_Host”

$Class
=
Get-SCOMClass
-Name
OpsLogix.IMP.VMWare.ESXHost

##================= change property of class instance

## get the just created class

$ClassInstance
=
Get-SCOMMonitoringObject
-Class
$Class
|
where { $_.DisplayName -eq $ClassInstanceDisplayName}

# change the properties

# only NON-KEY props can be changed

$ClassInstance[$Class,“AssetTag”].Value = “test tag 123”

# write it to scom

$ClassInstance.Overwrite()

##================= END ================================================

Delete a Class Instance

In this example we delete the ESXHost class instance “My_ESX_Host”.

## ======================================================================

## example script how to delete a class instance in SCOM

## ======================================================================

## Michel Kamp

## ======================================================================

New-SCOMManagementGroupConnection
-ComputerName
“localhost”

$mg
=
Get-SCOMManagementGroup

$ClassInstanceDisplayName=
“My_ESX_Host”

$Class
=
Get-SCOMClass
-Name
OpsLogix.IMP.VMWare.ESXHost

##============== remove a class instance

## get the class instance to remove

$ClassInstance
=
Get-SCOMMonitoringObject
-Class
$Class
|
where { $_.DisplayName -eq $ClassInstanceDisplayName}

# add the class instance to delete

$discovery
=
New-Object
Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData

$discovery.RemoveInternal($ClassInstance,$ClassInstance.GetClasses()[0])

## execute the delete

$discovery.Commit($mg)

##================= END ================================================

Happy SCOM’ing

Michel Kamp

TOUCHING SCOM

https://michelkamp.wordpress.com/

Advertisements

Calling HTTPS on self-hosted C# service using PowerShell gives 404 error

1 Aug

 

A short post on something that was making me bold. So maybe I save you some hears 😉

 

Problem (no challenge this time)

 

You have a Self Hosted C# webservice. This web service is configured to listen to base address https://host.domain.com:8733

  1. You have created a selfsigned certificate as example below:

New-SelfSignedCertificate -DnsName host.domain.com -NotAfter “2020/01/01” -FriendlyName “Test Cert” -CertStoreLocation Cert:\LocalMachine\My

  1. You added the certificate also to the Trusted Root Certification Authorities on the server. So the cert chain will be valid !

     

  2. You have configured the binding and listener for example as below: (certhash should be replaced by the hash from the cert above)

netsh http add urlacl url=https://*:8733/Config_Service user=EVERYONE delegate=yes

netsh http add sslcert ipport=0.0.0.0:8733 certhash=‎aaaaaaaaaaaaaaaaaaaaaa appid={3a1d638b-1b51-482a-dddd-218a589c2e69}

  1. Now on the host it selfs you call the webservice from out your browser as :

     

    https://host.domain.com:8733/Config_Service/ConfigService/api/list/configuration

     

    You will get a 404

     

  2. Now you go to external workstation and open a web browser and go to:

    https://host.domain.com:8733/Config_Service/ConfigService/api/list/configuration

     

    And succeed! You get the expected results.

     

  3. Now you open a PowerShell on the server and execute:

     

    Invoke-RestMethod
    -Urihttps://host.domain.com:8733/Config_Service/ConfigService/api/list/configuration”
    -Method
    Get

     

    And you will get again the 404 as

    Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

     

    If you get this error below you didn’t setup the certificate correctly, see step 1 and 2

    Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

     

  4. You try the same PowerShell on the external workstation. And it works …..

 

 

So local it looks not working …… Hmmmm

 

Solution

 

It’s very simple, but took me some time to find … Just disable the DisableLoopbackCheck on the server 😉

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\DisableLoopbackCheck = 1 (as dword)

No restart needed.

 

Hope this helps you,

Michel Kamp

 

 

ARM: A parameter cannot be found that matches parameter name ‘_artifactsLocationSasToken’

5 Dec

 

Short note to myself.

Problem

If you are trying to deploy an ARM template using Visual studio and you get the error below…..

AzureRmResourceGroupDeployment : A parameter cannot be found that matches parameter name ‘_artifactsLocationSasToken’

… do not spend 1 hour on trying to figure out why the parameter isn’t found. Just continue reading the solution below.

Solution

 

Scan your ARM template(s) if it has the correct formatting!!! Even when the deployment doesn’t mention any validation errors (because the deploy hasn’t reached this step yet)

So in my case I had one } too much , so the next part was seen as a parameter for the main resource. The compiler and syntax highlight didn’t complain but the deployment gave the ‘_artifactsLocationSasToken’ error.

So just be warned 😉

 

Happy Azurering

Michel Kamp

Michelkamp.wordpress.com

AZURE ARM template deployment

30 Nov

 

Issue:

When you are developing ARM templates in Visual studio and want to deploy you will get an error:

[ERROR] The running command stopped because the preference variable “ErrorActionPreference” or common parameter is set to Stop: Container name ‘dev_mka_extentiontest-stageartifacts’ is invalid. Valid names start and end with a lower case letter or a number and has in between a lower case letter, number or dash with no consecutive dashes and is 3 through 63 characters long.

[ERROR] + CategoryInfo : InvalidArgument: (:) [Set-AzureStorageBlobContent], ArgumentException

[ERROR] + FullyQualifiedErrorId : ArgumentException,Microsoft.WindowsAzure.Commands.Storage.Blob.SetAzureBlobContentCommand

 

Investigation

Reading the error message it should be something with the name of the artifacts container the deployment script wants to create.

I use the deployment configured as below:

So I just took the Azure storage explorer and tried to create exactly the same container. And guess … same error.

So I changed the _ in the name to – and guess … it worked.

Solution:

So be sure you don’t create a Resource group with_ in the name. Yes I know Azure will accept it but the deployment script used in Visual studio has an issue with this.

 

 

Happy Azurering

Michel kamp

https://michelkamp.wordpress.com

Azure Marketplace Solution UI Test

29 Nov

Hi,

A quick note for all DevOps under us:

Currently I am doing an investigation on how to publish a solution to the Azure marketplace. You have 2 ways to do this. A Virtual Machine offer or a Solution template offer.

A Virtual Machine offer will be a sysprep’d VM that will contain all your pre-installed software and just will be deployed as a new virtual machine. So you will have to prepare a VM and sysprep/generalize it and upload it to Azure.

A Solution template offer is somehow more advanced. Here you don’t have to configure a sysprep’d VM image but you just use an ARM template to rollout a brand new VM and then use a script extension resource to deploy your artifacts (aka your software to install). The cool part is that you can also change the UI interface on the Azure portal when configuring the deployment. This is done with the createUiDefinition.json file that has to be a part of the solution zip file you upload to azure.

An example of this can be found here: https://github.com/Azure/azure-quickstart-templates/tree/master/marketplace-samples

So when you have created all your ARM templates and put it into a Solution zip file (aka just zip all the ARM templates in one file) you will upload it using the https://publish.windowsazure.com portal and make a staged publish. (aka test rollout/publish)

Now you have to wait for a couple of hours before it will be ready to test it out. And this is the part that is annoying. There is no way to test the custom UI interface without having to do a staged publish and wait for a couple of hours a again……

Or is there …. ??

Yes there is! Thanks to this link I was able to test my custom deployment UI.

All you will have to do is:

  1. Create using the Azure storage explorer a new public container. (so set the public access level). For example named : “test”
  2. Copy the file “createUiDefinition.json” to this container.
  3. Check if you can open the file using the “copy Url” in a new browser.
  4. Now you will have to encode this URL , you can use http://meyerweb.com/eric/tools/dencoder/ for this.
  5. The URL looks like this now: https%3A%2F%2Fegnlmkotkizce.blob.core.windows.net%2Ftest%2FcreateUiDefinition.json
  6. Now replace the URL in the text below: (note: if you see ” replace it by the normal double quotes , this is a WordPress issue)

    https://portal.azure.com/#blade/Microsoft_Azure_Compute/CreateMultiVmWizardBlade/internal_bladeCallId/anything/internal_bladeCallerParams/{“initialData”:{},”providerConfig”:{“createUiDefinition”:”URL from step 5“}}

     

  7. The end result could look like this:

    https://portal.azure.com/#blade/Microsoft_Azure_Compute/CreateMultiVmWizardBlade/internal_bladeCallId/anything/internal_bladeCallerParams/{“initialData”:{},”providerConfig”:{“createUiDefinition”:”https%3A%2F%2Fegnlmkotkizce.blob.core.windows.net%2Ftest%2FcreateUiDefinition.json“}}

     

  8. Open a new browser and paste in the URL from step 7. And the result will be ….

 

Super handy and cool!

 

Happy scomming azuring

Michel Kamp

https://michelkamp.wordpress.com

 

 

 

OMS: Querying OMS the Message Analyzer way

22 Sep

 

Hi,

Short post to give you something cool I tried out today. I think about 1 year ago Microsoft dropped the Network Analyzer tool and replaced it with the Microsoft Message Analyzer tool.

With this tool you can now trace not only network traffic, like you only could do with the network analyzer tool, but also many other trace datasources. One of them is also OMS. Yes you hear it right. You can now analyse your OMS query’s using the Message Analyzer tool !

Here’s a short howto:

Download the Message Analyzer tool from:

http://www.microsoft.com/en-us/download/details.aspx?id=44226

 

Install and start it.

 

Now press the “New Session” button.

Now select the OMS datasource

Logon into you AZURE account.

 

YOU will need to have an active AZURE subscription !!!

Select the correct Azure subscription and Workspace.

 

Now in de query box you can specify the search query like you would do in the OMS Log search.

 

For this demo I use “*” to get all records.

 

Press Apply

After a couple of seconds the OMS records will be displayed. Now you can select 1 record and see all the properties filed and values.

 

At this time the results are limited by 10. Maybe later on it will be changed.

 

Happy OMS’ing!

Michel Kamp

 

 

 

[OMS][TIP] Graph Grouping

14 Sep

 

Something I noticed.

In OMS when you are making search query’s you can use the BY command to group. When you specify multiply group columns and use the INTERVAL to generate a graph you will also get a nice feature exposed.

In the legend you can now select the lines you want to see by grouping. This could be very handy.

See picture below:

 

Drawback

 

One drawback when using multiply groups. If you use this query also in a custom view you will lose the legend. But this legend is useless anyway since the view space is too little to make it readable.

 

 

Happy SCOM’ing

Michel Kamp