I am using Power NSX tool to automate adding IP address to IPSets periodically along with Power Shell.
I am using rest invoke method.
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.
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 $_"
}
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
any inputs
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 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
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
Hi Amol
Regarding the below point how to capture the output into the XML ?
Below is the command which I am suing the script.
How to export the output of this into the xml file ?
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/
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.
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.