truefalsefalsefalse';
####
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.014;
use MIME::Base64;
use Data::Dumper;
# Load the AnyData XML to CSV conversion modules
use XML::LibXML;
use XML::Simple;
use XML::Parser;
use XML::Twig;
# http://search.cpan.org/~makamaka/JSON/lib/JSON.pm
# Example install using cpanm:
# sudo cpanm -i JSON
use JSON;
# http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
# Example install using cpanm:
# sudo cpanm -i REST::Client
use REST::Client;
# Set the request parameters
my $host = 'https://host.service-now.com';
# Eg. User name="admin", Password="admin" for this code sample.
my $user = 'USER';
my $pwd = 'PASSWD';
my $client = REST::Client->new(host => $host);
my $encoded_auth = encode_base64("$user:$pwd", '');
my $header = 'u_ci_id,u_display_name,name,fqdn,ip_address,mac_address';
#$client->GET("/api/now/table/cmdb_ci?sysparm_limit=100000000&sysparm_fields=u_ci_id, name, hostname, fqdn, ip_address, mac_address",
$client->GET("/api/now/table/cmdb_ci?sysparm_limit=1",
#$client->GET("/api/now/table/cmdb_ci?sysparm_limit=100&sysparm_fields=mac_address,ip_address,fqdn,name,u_display_name,u_ci_id",
{'Authorization' => "Basic $encoded_auth",
'Accept' => 'application/xml'});
# print 'Response: ' . $client->responseContent() . "\n";
my $input_xml = $client->responseContent();
print Dumper ($input_xml);
print $header;
print "\n";
my $field= $ARGV[0] || 'u_ci_id';
my $twig= new XML::Twig;
$twig->xparse($input_xml);
my $root= $twig->root;
my @records = $root->children;
my @sorted= sort { $b->first_child( $field)->text
cmp $a->first_child( $field)->text }
@records;
for my $record (@sorted) {
my @info_tags = $record->children;
my @data;
for my $info_tag (@info_tags) {
push @data, $info_tag->text;
}
say join ',', @data;
}