I have a python script that will run once this powershell script is triggered. The python script connects to Silver Peak Orchestrator and pulls the nodes BGP state and saves it to a text file. Powershell script then will read the text file and update the corresponding node inside solarwinds to update the custom property field. I am decent with python and know that script works as intended but I haven't really wrote any powershell scripts and was wondering if this would work as intended or should I make any alterations. The end goal would be to have a computer or server run this powerhsell script every 10 minutes or so via task scheduler. This way I can use the custom property filed to create a dashboard widget, run reports, alerting etc if BGP state goes down.
Here is the script:
Invoke-Expression "python
\tools\Silver_Peak_REST_APIs\BGPStatusUsingNodeID.py" # SolarWinds credentials
$swUsername = 'username'
$swPassword = 'password'
$swHostname = 'solarwinds.example.com'
# Dictionary to map node names to their NodeID in SolarWinds
$nodeIdMap = {
'/bgp/state/23.NE' = 3985
'/bgp/state/45.NE' = 4138
}
# Iterate over the nodes in the text file
foreach ($node in Get-Content 'BGPState.txt') {
# Get the NodeID for the current node from the dictionary
$nodeId = $nodeIdMap[$node]
# Connect to SolarWinds API
$swUri = "https://$swHostname/SolarWinds/InformationService/v3/Json/Invoke/Orion.Nodes/UpdateCustomProperties"
$swSession = New-PSSession -ComputerName $swHostname -Credential (Get-Credential -UserName $swUsername -Password $swPassword)
# Update the custom property field for the node in SolarWinds
Invoke-Command -Session $swSession -ScriptBlock {
Param($nodeId, $bgpState)
# Update the custom property field for the node
$swApiArgs = {
Entity = "Orion.Nodes"
Arguments = {
Nodes = ($nodeId)
Properties = {
BGP_State = $bgpState
}
}
}
$swApiJson = $swApiArgs | ConvertTo-Json
$swApiResponse = Invoke-RestMethod -Method Post -Uri $args[0] -Body $swApiJson -ContentType 'application/json'
$swApiResponse
} -ArgumentList $nodeId, $bgpState, $swUri
# Disconnect from SolarWinds API
Remove-PSSession $swSession
}