Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Richard_Mason
Participant

Powershell Script to use with WebAPI

Just posting this to show Powershell options to script adding objects to R80 using the new web API.

1. create hosts.csv with three columns headed host, ipv4 and color eg

host,ipv4,color
node1,1.1.1.2,blue
node2,2.2.2.3,red

2. Open powershell and cd to the curl location

cd "C:\Program Files\cURL\bin"

3. Edit the $credentials section to include a valid user and password

4. Edit the $hosts below to point to the location of your csv file.

4. Paste all the below lines between ~#~#~  into powershell.

~#~#~#~#~#~#~

  $credentials = @"
{
    \"user\":\"USER_HERE\",
    \"password\":\"PASSWORD_HERE\"
    }
"@

  $login = .\curl -k -H "Content-Type: application/json" -X POST -d $credentials https://<MGMT_SERVER_HERE>/web_api/login
  
   $uid = $login | findstr uid | ForEach-Object {$_.Substring(11,36)}
   $sid = $login | findstr sid | ForEach-Object {$_.Substring(11,43)}
  
   $hosts = Import-csv c:\path\to\hosts.csv
  
#This will process the CSV row by row. Each row contains information to create a host object with a name and ipv4-address
 
foreach ($node in $hosts)
 
   {
   
   $name = $node.host
   $ipv4 = $node.ipv4
   $color = $node.color
   
   $hostString = @"
{
    \"name\":\"$name\",
    \"ipv4-address\":\"$ipv4\",
\"color\":\"$color\"
    }
"@
 
  # curl used to connect to the web api and post the new host names and ip's.
 
   .\curl -k -H "Content-Type: application/json" -H "X-chkp-sid:$sid" -X POST -d $hostString https://<MGMT_SERVER_HERE>/web_api/add-host
 
  } 
 

# unlike mgmt_cli the changes are not published automatically so we need to publish the session we just created using the uid string returned during login.

 


  $uidString = @"
{
    \"uid\":\"$uid\"
    }
"@

  # curl used to connect to the web api and post the uid to publish the session.

  .\curl -k -H "Content-Type: application/json" -H "X-chkp-sid:$sid" -X POST -d $uidString https://<MGMT_SERVER_HERE>/web_api/publish

~#~#~#~#~#~#~

0 Kudos
0 Replies

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events