set psscript=DiscoverOsBased.ps1
set vbscript=DiscoverOsBased.vbs
set cscript=%windir%\system32\cscript.exe
set ps=%windir%\system32\WindowsPowerShell\v1.0\powershell.exe
set params=
if Exist "%cscript%" goto vb
if Exist "%ps%" goto pshell
goto end
:vb
if not exist "%~dp0%vbscript%" goto end
if exist "%~dp0vbparams.txt" (
set /p params=<"%~dp0vbparams.txt"
)
"%cscript%" /nologo "%~dp0%vbscript%" %params%
goto end
:pshell
if not exist "%~dp0%psscript%" goto end
if exist "%~dp0psparams.txt" (
set /p params=<"%~dp0psparams.txt"
)
"%ps%" -nologo -EP bypass -command "& '%~dp0%psscript%'" %params%
goto end
Function Init-ScomHelper
{
$DiscHelper = @'
namespace SCOM.Helper
{
using System;
using System.Runtime.InteropServices;
public class ConvertData
{
public static string GetDataItemFromOutput(
Object oData)
{
NativeMethods.ISerialize discoverySerializer = null;
discoverySerializer = oData as NativeMethods.ISerialize;
string xmlString = null;
if (null != discoverySerializer)
{
int hr = discoverySerializer.SaveToString(out xmlString);
Marshal.ThrowExceptionForHR(hr);
}
return xmlString;
}
public static class NativeMethods
{
[Guid("A4E79E8A-9494-47A4-A280-8C7D35C88A2F"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISerialize
{
int SaveToString([MarshalAs(UnmanagedType.BStr)] out string output);
int LoadFromString([MarshalAs(UnmanagedType.BStr)] string input);
}
}
}
}
Windows Cluster Os Based Discovery
SourceId: {0} ManagedEntityId : {1} ClassId : {2} PropertyId : {3} PropertyValue : {4}
OsVersion : {5} PropertyId2 : {6} PropertyValue2 : {7}
"@
$StartDiscoveryMsg = "Started cluster Os Based discovery at machine local time:"
$FinishDiscoveryMsg = "Finished dicovery at machine local time: "
$CreateDiscoveryDataErrorMsg = "Failed to get Discovery Data Object. MOM v3 must be installed to execute this script."
$InitApiErrorMsg = "Failed to get Discovery API. MOM v3 must be installed to execute this script. Error:{0}"
$InitScomSerApiErrorMsg = "Failed to Initialize SCOM Discovery Serialization API. MOM v3 must be installed to execute this script."
$ErrorStartParametersMsg = "Minimal list of parameters is empty. Exiting discovery. SourceId: {0} ManagedEntityId : {1} ClassId : {2} PropertyId : {3} "
function Get-EmptyDiscoveryData($SourceId, $ManagedEntityId,$IsSnapshot)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$Error.Clear()
if ($null -eq $oAPI)
{
$oAPI = New-Object -ComObject "MOM.ScriptAPI"
if ($null -eq $oAPI)
{
return $null
}
}
Function Log-DiscoveryMessage([string]$message)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$timex=get-date -displayhint time
$message = $message +" " + $timex
LogMessage $DBG_TRACE $message
}
#======================================================================
# Method: LogMessage
# Description: Log a debug message to ScriptContext
# Parameters:
# nLevel - Debug level for the message that
# we're logging.
# strMessage - The message to write to the trace.
#======================================================================
Function LogMessage($nLevel, $strMessage,$LogId = $EventId)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
#==========================================================================
# Initialize the arguments in Script
#==========================================================================
Function Initialize-DiscoveryScript
(
[ref]$oAPI
)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
Dim oAPI, oDiscoveryData
Dim SourceId,ManagedEntityId,ClassId,PropertyId,PropertyValue
Dim OSVersion,PropertyId2,PropertyValue2
Dim result
Dim message
Dim IsArgs
result = False
Set oAPI = CreateObject("MOM.ScriptAPI")
If(0 <> Err.number or (False = HasValue(oAPI)) ) Then
WScript.Quit(-1)
End If
result = GetArguments(SourceId,ManagedEntityId,ClassId,PropertyId,PropertyValue,OSVersion,PropertyId2,PropertyValue2,IsArgs)
message = "SourceId: " & SourceId & " ManagedEntityId: " & ManagedEntityId & " ClassId: " & ClassId & " " & vbCrLf
message = message & "OSVersion : " & OSVersion & " PropertyId: " & PropertyId & " PropertyValue: " & PropertyValue & " " & vbCrLf
message = message & "PropertyId2: " & PropertyId2 & " PropertyValue2: " & PropertyValue2 & " " & vbCrLf
If(False = result) Then
Call oAPI.LogScriptEvent(ScriptName,ScriptEventId, EventTypeWarning, "Wrong Number of arguments." & vbCrLf & message)
Set oAPI = Nothing
WScript.Quit(-2)
End If
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
If(0 <> Err.number or (False = HasValue(oDiscoveryData)) ) Then
message = "Cannot create Discovery Data. Error: " & Err.Description & vbCrLf & message
Call oAPI.LogScriptEvent(ScriptName,ScriptEventId, EventTypeError, message)
Set oAPI = Nothing
WScript.Quit(-3)
End If
If (False = IsArgs) Then
oDiscoveryData.IsSnapShot = False
Call oAPI.Return(oDiscoveryData)
Set oAPI = Nothing
Exit Sub
End If
result = DiscoverInstances(SourceId,ManagedEntityId,ClassId,PropertyId,PropertyValue,OSVersion,PropertyId2,PropertyValue2,oDiscoveryData)
If(False = result) Then
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
If(0 <> Err.number or (False = HasValue(oDiscoveryData)) ) Then
message = "Cannot create empty Discovery Data. Error: " & Err.Description & vbCrLf & message
Call oAPI.LogScriptEvent(ScriptName,ScriptEventId, EventTypeError, message)
WScript.Quit(-4)
End If
oDiscoveryData.IsSnapShot = False
End If
Call oAPI.Return(oDiscoveryData)
Set oDiscoveryData = Nothing
Set oAPI = Nothing
End Sub
Function HasValue(Value)
Dim bNothing
bNothing = false
IF ( IsObject(Value) ) THEN
IF (Nothing is Value) THEN
bNothing = true
END IF
END IF
HasValue = Not ( IsEmpty(Value) or bNothing or IsNull(Value) )
End Function
Function GetArguments(ByRef SourceId,ByRef ManagedEntityId,ByRef ClassId,ByRef PropertyId,ByRef PropertyValue,ByRef OSVersion,ByRef PropertyId2,ByRef PropertyValue2,ByRef IsArgs)
On Error Resume Next
GetArguments = False
IsArgs = False
If (WScript.Arguments.Count < 2) Then
Exit Function
End If
If (WScript.Arguments.Count > 5) Then
OSVersion = Replace(objArguments(5), Chr(34), "")
End If
If (WScript.Arguments.Count > 6) Then
PropertyId2 = Replace(objArguments(6), Chr(34), "")
End If
If (WScript.Arguments.Count > 7) Then
PropertyValue2 = Replace(objArguments(7), Chr(34), "")
End If
GetArguments = True
On Error Goto 0
End Function
Function DiscoverInstances(SourceId,ManagedEntityId,ClassId,PropertyId,PropertyValue,OSVersion,PropertyId2,PropertyValue2,oDiscoveryData)
On Error Resume Next
Dim result, oClassInstance
DiscoverInstances = True
result = GetSnapshotDiscovery(OsVersion)
If (Exit_IncrementalDiscovery = result) Then
oDiscoveryData.IsSnapshot = false
ElseIf(Start_SnapShotDiscovery = result) Then
Set oClassInstance = oDiscoveryData.CreateClassInstance(ClassId)
If (0 <> Err.number) Then
DiscoverInstances = False
Exit Function
End If
If ( IsObject(OsVersion)) Then
Exit Function
End If
sWmiTarget = "."
If ( Not IsObject(sComputerName)) Then
If (HasValue(sComputerName) and Len(sComputerName) > 0 ) Then
sWmiTarget = "" & sComputerName
End If
End If
sWmiTarget = "winmgmts:\\" & sWmiTarget & "\root\cimv2"
Filter = GetOsFilter(OsVersion,",")
StrQuery = "select Version from Win32_OperatingSystem" & Filter
Set oWmiSet = WMIExecQueryNoAbort(sWmiTarget, strQuery)
If Err.Number <> 0 Then
IsError = True
Exit Function
End If
Count = oWmiSet.Count
If Err.Number <> 0 Then
IsError = True
Exit Function
End If
If (bIsOsValid) Then
iResult = Start_SnapShotDiscovery
Else
If (bIsError) Then
iResult = Exit_IncrementalDiscovery
Else
iResult = Exit_SnapShotDiscovery
End IF