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/"
$response = invoke-nsxrestmethod -method "POST" -uri $URI -connection $connection -body $body
write-host "IP-Set created sucessfully"
Throw "Unable to create ipset's $_"
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
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.
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 220.127.116.11,192.168.0.0/16,10.1.2.1-10.1.3.15
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 ?
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/
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.
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.