# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.InstallAgent.WA.ps1"
#=================================================================================
# Gather script start time
$StartTime = Get-Date
# 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,1017,0, "Starting script. AgentName is ($AgentName). Running as ($whoami)")
#Connect to local SCOM Management Group Section
#=================================================================================
# Clear any previous errors
if($Error)
{
$Error.Clear()
}
# Import the OperationsManager PowerShell module and connect to the management group
Try
{
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
Import-module $SCOMModulePath
}
Catch
{
$momapi.LogScriptEvent($ScriptName,1017,2, "Unable to load the OperationsManager module, Error is: $error")
}
Try
{
New-DefaultManagementGroupConnection
}
Catch
{
$momapi.LogScriptEvent($ScriptName,1017,2, "Unable to connect to the management server. Error when calling New-DefaultManagementGroupConnection. Error is: $error")
}
#=================================================================================
# Begin Main Script
#=================================================================================
# Get local SCOM management server name
$LocalHost = [System.Net.Dns]::GetHostEntry([string]$env:computername).HostName
Write-Host "Running on management server ($LocalHost)"
Write-Host "Getting Management Server object"
# Get SCOM Management Server object
$PrimaryMgmtServer = Get-SCOMManagementServer -Name $LocalHost
Write-Host "Attempting to Install Agent"
# Clear any previous errors
if($Error)
{
$Error.Clear()
}
# Install SCOM Agent
Install-SCOMAgent -DNSHostName $AgentName -PrimaryManagementServer $PrimaryMgmtServer
if($Error)
{
Write-Host "Error ocurred: ($Error)"
$momapi.LogScriptEvent($ScriptName,1017,2, "Error installing agent. Error is: $Error")
}
Write-Host "Push install attempted. Check Alerts view for any failures, Pending Actions, or the Agent Logs on the management servers."
#=================================================================================
# End Main Script
# Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,1017,0,"`n Script has completed. Runtime is ($ScriptTime).") </Script></ScriptBody>
<Parameters>
<Parameter>
<Name>AgentName</Name>
<Value>$Config/AgentName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="PSWA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>