<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Powershell Script to use with WebAPI in API / CLI Discussion</title>
    <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Powershell-Script-to-use-with-WebAPI/m-p/2317#M71</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just posting this to show Powershell options to script adding objects to R80 using the new web API.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. create hosts.csv with three columns headed host, ipv4 and color eg&lt;/P&gt;&lt;P&gt;host,ipv4,color&lt;BR /&gt;node1,1.1.1.2,blue&lt;BR /&gt;node2,2.2.2.3,red&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. Open powershell and cd to the curl location&lt;/P&gt;&lt;P&gt;cd "C:\Program Files\cURL\bin"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3. Edit the $credentials section to include a valid user and password&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;4. Edit the $hosts below to point to the location of your csv file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;4. Paste all the below lines between ~#~#~&amp;nbsp; into powershell.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;~#~#~#~#~#~#~&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; $credentials = @"&lt;BR /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"user\":\"USER_HERE\",&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"password\":\"PASSWORD_HERE\"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt; "@&lt;/P&gt;&lt;P&gt;&amp;nbsp; $login = .\curl -k -H "Content-Type: application/json" -X POST -d $credentials &lt;A href="https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/login"&gt;https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/login&lt;/A&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $uid = $login | findstr uid | ForEach-Object {$_.Substring(11,36)}&lt;BR /&gt;&amp;nbsp;&amp;nbsp; $sid = $login | findstr sid | ForEach-Object {$_.Substring(11,43)}&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $hosts = Import-csv c:\path\to\hosts.csv&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt; #This will process the CSV row by row. Each row contains information to create a host object with a name and ipv4-address&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; foreach ($node in $hosts)&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $name = $node.host&lt;BR /&gt;&amp;nbsp;&amp;nbsp; $ipv4 = $node.ipv4&lt;BR /&gt;&amp;nbsp;&amp;nbsp; $color = $node.color&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $hostString = @"&lt;BR /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"name\":\"$name\",&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"ipv4-address\":\"$ipv4\",&lt;BR /&gt; \"color\":\"$color\"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt; "@&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; # curl used to connect to the web api and post the new host names and ip's.&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; .\curl -k -H "Content-Type: application/json" -H "X-chkp-sid:$sid" -X POST -d $hostString &lt;A href="https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/add-host"&gt;https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/add-host&lt;/A&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; }&amp;nbsp; &lt;BR /&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt; # 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp; $uidString = @"&lt;BR /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"uid\":\"$uid\"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt; "@&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; # curl used to connect to the web api and post the uid to publish the session.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; .\curl -k -H "Content-Type: application/json" -H "X-chkp-sid:$sid" -X POST -d $uidString &lt;A href="https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/publish"&gt;https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/publish&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;~#~#~#~#~#~#~&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 09 May 2016 09:59:33 GMT</pubDate>
    <dc:creator>Richard_Mason</dc:creator>
    <dc:date>2016-05-09T09:59:33Z</dc:date>
    <item>
      <title>Powershell Script to use with WebAPI</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Powershell-Script-to-use-with-WebAPI/m-p/2317#M71</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just posting this to show Powershell options to script adding objects to R80 using the new web API.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. create hosts.csv with three columns headed host, ipv4 and color eg&lt;/P&gt;&lt;P&gt;host,ipv4,color&lt;BR /&gt;node1,1.1.1.2,blue&lt;BR /&gt;node2,2.2.2.3,red&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. Open powershell and cd to the curl location&lt;/P&gt;&lt;P&gt;cd "C:\Program Files\cURL\bin"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3. Edit the $credentials section to include a valid user and password&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;4. Edit the $hosts below to point to the location of your csv file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;4. Paste all the below lines between ~#~#~&amp;nbsp; into powershell.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;~#~#~#~#~#~#~&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; $credentials = @"&lt;BR /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"user\":\"USER_HERE\",&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"password\":\"PASSWORD_HERE\"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt; "@&lt;/P&gt;&lt;P&gt;&amp;nbsp; $login = .\curl -k -H "Content-Type: application/json" -X POST -d $credentials &lt;A href="https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/login"&gt;https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/login&lt;/A&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $uid = $login | findstr uid | ForEach-Object {$_.Substring(11,36)}&lt;BR /&gt;&amp;nbsp;&amp;nbsp; $sid = $login | findstr sid | ForEach-Object {$_.Substring(11,43)}&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $hosts = Import-csv c:\path\to\hosts.csv&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt; #This will process the CSV row by row. Each row contains information to create a host object with a name and ipv4-address&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; foreach ($node in $hosts)&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $name = $node.host&lt;BR /&gt;&amp;nbsp;&amp;nbsp; $ipv4 = $node.ipv4&lt;BR /&gt;&amp;nbsp;&amp;nbsp; $color = $node.color&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; $hostString = @"&lt;BR /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"name\":\"$name\",&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"ipv4-address\":\"$ipv4\",&lt;BR /&gt; \"color\":\"$color\"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt; "@&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; # curl used to connect to the web api and post the new host names and ip's.&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; .\curl -k -H "Content-Type: application/json" -H "X-chkp-sid:$sid" -X POST -d $hostString &lt;A href="https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/add-host"&gt;https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/add-host&lt;/A&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; }&amp;nbsp; &lt;BR /&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt; # 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp; $uidString = @"&lt;BR /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; \"uid\":\"$uid\"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt; "@&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; # curl used to connect to the web api and post the uid to publish the session.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; .\curl -k -H "Content-Type: application/json" -H "X-chkp-sid:$sid" -X POST -d $uidString &lt;A href="https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/publish"&gt;https://&amp;lt;MGMT_SERVER_HERE&amp;gt;/web_api/publish&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;~#~#~#~#~#~#~&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 May 2016 09:59:33 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Powershell-Script-to-use-with-WebAPI/m-p/2317#M71</guid>
      <dc:creator>Richard_Mason</dc:creator>
      <dc:date>2016-05-09T09:59:33Z</dc:date>
    </item>
  </channel>
</rss>

