VMware Networking Community
rajeevsrikant
Expert
Expert

Power NSX - Output log

I am using Power NSX tool to automate adding IP address to IPSets periodically along with Power Shell.

I am using rest invoke method.

Invoke-RestMethod -method "put"-uri "/api/2.0/services/ipset/

The script executes fine & no issue with the script. Would like to generate log output which indicates that the script has executed successfully.

Let me know how to get the required log out put when the script is executed successfully indicating it is fine.

Reply
0 Kudos
10 Replies
amolnjadhav
Enthusiast
Enthusiast

Hi Rajeev,

There should be multiple ways to handle exception handing.. Here i can suggest try & catch blocks in powercli script.

$URI = "/api/2.0/services/ipset/"

  try {

        $response = invoke-nsxrestmethod -method "POST" -uri $URI -connection $connection -body $body

        write-host "IP-Set created sucessfully"

    }

    catch {

        Throw "Unable to create ipset's $_"

    }

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Regards Amol Jadhav VCP NSXT | VCP NSXV | VCIX6-NV | VCAP-DCA | CCNA | CCNP - BSCI
Reply
0 Kudos
rajeevsrikant
Expert
Expert

Thanks Amol

Few clarifications.

If the API is executed properly my understanding is that, the output should generate some value like 200 indicating the script is executed rightly.

If not i will generate output like 4XX, ect

Can this be taken as output & generate log file

Reply
0 Kudos
rajeevsrikant
Expert
Expert

any inputs

Reply
0 Kudos
amolnjadhav
Enthusiast
Enthusiast

Hi Rajeev,

  

     I understood your point.

     In this case $response variable will have XML file as a output, when web-invoke command successfully completed. In case API command unable to execute you don't need to handle in the script it will return error in case it failed on the screen.

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Regards Amol Jadhav VCP NSXT | VCP NSXV | VCIX6-NV | VCAP-DCA | CCNA | CCNP - BSCI
Reply
0 Kudos
amolnjadhav
Enthusiast
Enthusiast

Please go though this blog post. it may help you to solve your issue

Aaron's Code Notes: How to make an HTTP request with PowerShell

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Regards Amol Jadhav VCP NSXT | VCP NSXV | VCIX6-NV | VCAP-DCA | CCNA | CCNP - BSCI
Reply
0 Kudos
DaleCoghlan
VMware Employee
VMware Employee

Why not just use the built in cmdlets in PowerNSX? We put a lot of work into making this really useable for Day 2 operations. After  the cmdlet executes, it return the updated object.

Get-NsxIpSet blah | Add-NsxIpSetMember -IPAddress 1.2.3.4,192.168.0.0/16,10.1.2.1-10.1.3.15

Dale

Reply
0 Kudos
rajeevsrikant
Expert
Expert

Hi Amol

Regarding the below point how to capture the output into the XML  ?

Below is the command which I am suing the script.

Invoke-RestMethod -method "put"-uri "/api/2.0/services/ipset/

How to export the output of this into the xml file ?

Reply
0 Kudos
amolnjadhav
Enthusiast
Enthusiast

Hi Rajeev,

  Are you getting any error while running the command?

  Following command should return you the output in xml format :

[xml]$ipSets= Invoke-RestMethod -method "put"-uri "/api/2.0/services/ipset/

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Regards Amol Jadhav VCP NSXT | VCP NSXV | VCIX6-NV | VCAP-DCA | CCNA | CCNP - BSCI
Reply
0 Kudos
rajeevsrikant
Expert
Expert

Hi Amol

Thanks.

The command works fine. But what I am expecting is that I want to log the output of the command.

Even if it is successful or failed, I need to log the output in the output file.

I tried the command which u highlighted below. The .xml file did not have any details & it was empty.

Reply
0 Kudos
amolnjadhav
Enthusiast
Enthusiast

Hi Rajeev,

    

     I am not quite sure why it is not returns the output to variable in case of failure.

     May be you use try & catch block to resolve this issue. In case command failed it will go to catch block.

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Regards Amol Jadhav VCP NSXT | VCP NSXV | VCIX6-NV | VCAP-DCA | CCNA | CCNP - BSCI
Reply
0 Kudos