# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.SetManuallyInstalledFalse.Task.ps1"
#=================================================================================
# Gather who the script is running as
$whoami = whoami
#Load the MOMScript API and discovery propertybag
$momapi = New-Object -comObject "Mom.ScriptAPI"
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,1317,0, "Starting script. Running as ($whoami)")
# Begin Main Script
#=================================================================================
Write-Host "Task Starting. Running as $whoami"
IF ($AgentName)
{
Write-Host "Agent Name is ($AgentName)."
}
ELSE
{
Write-Host "FATAL ERROR: Agent Name was not passed to script. Terminating"
$momapi.LogScriptEvent($ScriptName,1317,2, "FATAL ERROR: Agent Name was not passed to script. Terminating")
#EXIT
}
IF ($RowCount -lt 1)
{
Write-Host "FATAL ERROR: We did not retrieve an IsManuallyInstalled value from SQL for ($AgentName). Terminating"
$momapi.LogScriptEvent($ScriptName,1317,2, "FATAL ERROR: We did not retrieve an InsManuallyInstalled value from SQL for ($AgentName). Terminating")
#EXIT
}
IF ($IsManInstValue -ne $true)
{
Write-Host "FATAL ERROR: IsManuallyInstalled value from SQL for ($AgentName) is ($IsManInstValue). Terminating"
$momapi.LogScriptEvent($ScriptName,1317,2, "FATAL ERROR: IsManuallyInstalled value from SQL for ($AgentName) is ($IsManInstValue). Terminating")
#EXIT
}
# Update Table in SQL
Write-Host "IsManuallyInstalled value found from SQL was ($IsManInstValue). Attempting to Update SQL now."
$SqlQuery2 = "UPDATE MT_HealthService
SET IsManuallyInstalled=0 WHERE IsManuallyInstalled=1 AND BaseManagedEntityId IN
(select BaseManagedEntityID from BaseManagedEntity where BaseManagedTypeId = 'AB4C891F-3359-3FB6-0704-075FBFE36710' AND DisplayName = '$AgentName')"
IF ($IsManInstValue -ne $false)
{
Write-Host "FATAL ERROR: Attempt to set IsManuallyInstalled value failed. Current value for ($AgentName) is ($IsManInstValue). Terminating"
$momapi.LogScriptEvent($ScriptName,1317,2, "FATAL ERROR: Attempt to set IsManuallyInstalled value failed. Current value for ($AgentName) is ($IsManInstValue). Terminating")
#EXIT
}
Write-Host "Successfully set IsManuallyInstalled value. New value for ($AgentName) is ($IsManInstValue)."
#=================================================================================
# End Main Script </Script></ScriptBody>
<Parameters>
<Parameter>
<Name>AgentName</Name>
<Value>$Target/Property[Type="System!System.Entity"]/DisplayName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>120</TimeoutSeconds>
</WriteAction>
</Task>