Module: JamfReports

Defined in:
lib/JamfReports.rb,
lib/JamfReports/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.checkTokenExpirationObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/JamfReports.rb', line 32

def self.checkTokenExpiration
    #check if token is valid

    current_time= Time.now.to_i
    # puts "Checking if Token is valid"
    if $tokenExpirationEpoch >= current_time
        # puts "Epoch time is #{$tokenExpirationEpoch}" 
        time_difference = $tokenExpirationEpoch - current_time
        time_till_token_expire = Time.at(time_difference).utc.strftime("%H:%M:%S")
        # puts "Token is valid"
        # puts "Token expires in: " + time_till_token_expire
    else
        # puts "Token is invalid"
    end
end

.getAllApplicationInventoryObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/JamfReports.rb', line 70

def self.getAllApplicationInventory
    #this is the main function
    #loops through all computers inventory and only looks at Applications section
    $applicationHash = Hash.new(0)

    url = URI("#{$jamfpro_url}/api/v1/computers-inventory?section=APPLICATIONS&page=0&page-size=2000&sort=id%3Aasc")
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    request = Net::HTTP::Get.new(url)
    request["accept"] = 'application/json'
    request["Authorization"] = "Bearer #{$bearerToken}"
    response = http.request(request)
    results = JSON.parse(response.read_body)
    
    results["results"].each do |item|
        item["applications"].each do |item|
            $applicationName = item["name"]
            $applicationHash["#{$applicationName}"] +=1
        end
    end
end

.getTokenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/JamfReports.rb', line 13

def self.getToken
    #request New token
    url = URI("#{$jamfpro_url}/api//v1/auth/token")
    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true
    request = Net::HTTP::Post.new(url)
    request["Content-Type"] = "application/json"
    request["Authorization"] = "Basic #{$api_pw}"
    response = https.request(request)
    results = JSON.parse(response.read_body)
    $bearerToken=results['token']
    $tokenExpiration=results['expires']
    $tokenExpirationEpoch=Time.parse($tokenExpiration).to_i

    ### SANITY CHECK
    # puts "Token granted"
    # puts "#{$bearerToken}"
end

.invalidateTokenObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/JamfReports.rb', line 48

def self.invalidateToken
    #revoke token
    url = URI("#{$jamfpro_url}/api/v1/auth/invalidate-token")
    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true
    request = Net::HTTP::Post.new(url)
    request["Authorization"] = "Bearer #{$bearerToken}"
    response = https.request(request)
    status = response.code

    ### SANITY CHECK
    # if status == "204"
    #     puts "Token successfully invalidated"
    # elsif status == "401"
    #     puts "Token already invalid"
    # else
    #     puts "error: something went wrong"
    # end
end

.listAllInstalledAppsObject



93
94
95
96
97
98
# File 'lib/JamfReports.rb', line 93

def self.listAllInstalledApps
    # Prints back report sorty by most installed apps to least
    $applicationHash.sort_by {|_key, value| value}.reverse.each do |k,v|
        puts " #{k} = #{v}"
    end
end

.listAllInstalledApps_exporttocsvObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/JamfReports.rb', line 154

def self.listAllInstalledApps_exporttocsv
    $currentUser=Etc.getlogin
    reportName="Installed Applications Report"
    $reportPath="/Users/#{$currentUser}/Desktop/#{reportName}.csv"
    File.write("#{$reportPath}", "name,count\n")
    
    $applicationHash.sort_by {|_key, value| value}.reverse.each do |k,v|
        open("#{$reportPath}", "a") { |f| f << "#{k},#{v}\n" } 
    end
end

.listofOneInstallAppsObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/JamfReports.rb', line 114

def self.listofOneInstallApps
    #prints list of all apps by name that have been installed once
    @oneInstalledApps = Hash.new
    $applicationHash.each do |name, count|
        if count == 1
            @oneInstalledApps["#{name}"] = "#{count}"
        end
    end

    puts @oneInstalledApps.keys
end

.totalNumberOfOneInstalledAppsObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/JamfReports.rb', line 101

def self.totalNumberOfOneInstalledApps
    #prints the total number of apps that have been installed once
    @oneOffApps = Hash.new
    $applicationHash.each do |name, count|
        if count == 1
            @oneOffApps["#{name}"] = "#{count}"
        end
    end

    puts @oneOffApps.count
end

.webBrowserReportObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/JamfReports.rb', line 127

def self.webBrowserReport
    #reports back on how many installs of these browsers there are
    $webBroswerArray = [
        "Google Chrome.app",
        "Google Chrome Canary.app",
        "Firefox.app",
        "Firefox Developer Edition.app",
        "Safari.app",
        "Safari Technology Preview.app",
        "Microsoft Edge.app", 
        "Brave Browser.app", 
        "Arc.app",
        "Opera.app",
        "LinCastor Browser.app",
        "LockDown Browser.app", 
        "Tor Browser.app",
        "Vivaldi.app",
        "DuckDuckGo.app"
    ]

    $applicationHash.sort_by {|_key, value| value}.reverse.each do |k,v|
        if $webBroswerArray.include?("#{k}")
            puts "#{k} = #{v}"
        end
    end
end