@@ -16,18 +16,18 @@ import sys # pylint: disable=C0413
1616
1717import lib .args # pylint: disable=C0413
1818import lib .base # pylint: disable=C0413
19- import lib .shell # pylint: disable=C0413
20- from lib .globals import (STATE_CRIT , STATE_OK , # pylint: disable=C0413
21- STATE_UNKNOWN , STATE_WARN )
19+ import lib .powershell # pylint: disable=C0413
20+ import lib .txt # pylint: disable=C0413
21+ from lib .globals import (STATE_OK , STATE_UNKNOWN )
22+
2223
2324__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
24- __version__ = '2023112901 '
25+ __version__ = '2025061701 '
2526
2627DESCRIPTION = 'Checks the number of pending Windows updates.'
2728
28- DEFAULT_CRIT = 50 # #updates
29- DEFAULT_TIMEOUT = 300 # seconds
30- DEFAULT_WARN = 2 # #updates
29+ DEFAULT_CRIT = 50 # #updates
30+ DEFAULT_WARN = 2 # #updates
3131
3232
3333def parse_args ():
@@ -38,7 +38,7 @@ def parse_args():
3838 parser .add_argument (
3939 '-V' , '--version' ,
4040 action = 'version' ,
41- version = '{0} : v{1 } by {2}' . format ( '%(prog)s' , __version__ , __author__ ),
41+ version = f'%(prog)s : v{ __version__ } by { __author__ } '
4242 )
4343
4444 parser .add_argument (
@@ -53,23 +53,25 @@ def parse_args():
5353 '-c' , '--critical' ,
5454 default = DEFAULT_CRIT ,
5555 dest = 'CRIT' ,
56- help = 'Set the critical threshold for the number of pending updates. Default: %(default)s' ,
56+ help = 'Set the critical threshold for the number of pending updates. '
57+ 'Default: %(default)s' ,
5758 type = lib .args .int_or_none ,
5859 )
5960
6061 parser .add_argument (
6162 '--timeout' ,
62- help = 'Network timeout in seconds. Default: %(default)s (seconds)' ,
63+ help = argparse . SUPPRESS ,
6364 dest = 'TIMEOUT' ,
6465 type = int ,
65- default = DEFAULT_TIMEOUT ,
66+ default = 300 ,
6667 )
6768
6869 parser .add_argument (
6970 '-w' , '--warning' ,
7071 default = DEFAULT_WARN ,
7172 dest = 'WARN' ,
72- help = 'Set the warning threshold for the number of pending updates. Default: %(default)s' ,
73+ help = 'Set the warning threshold for the number of pending updates. '
74+ 'Default: %(default)s' ,
7375 type = lib .args .int_or_none ,
7476 )
7577
@@ -88,31 +90,34 @@ def main():
8890
8991 # fetch data
9092 cmd = '''
91- powershell -Command "
9293 $WindowsUpdates = New-Object -ComObject 'Microsoft.Update.Session';
9394 $SearchIndex = $WindowsUpdates.CreateUpdateSearcher();
9495 $Pending = $SearchIndex.Search('IsInstalled=0');
9596 foreach ($update in $Pending.Updates) {
96- [string] $name = [string]::Format(' {0} [{1}]', $update.Title, $update.LastDeploymentChangeTime) ;
97+ $name = " {0} [{1}]" -f $update.Title, $update.LastDeploymentChangeTime;
9798 Write-Output $name;
9899 }
99- " '''
100+ '''
100101
101102 # execute the shell command and return its result and exit code
102- stdout , stderr , retc = lib .base . coe ( lib . shell . shell_exec ( cmd , timeout = args . TIMEOUT ) )
103- if stderr :
104- # Could not find any documentation in the return codes of 'query user'.
105- # Therefore we assume any is ok.
106- lib .base .cu (stderr )
107- result = stdout .strip ()
103+ result = lib .powershell . run_ps ( cmd )
104+ if not result :
105+ lib . base . cu ( 'Error calling PowerShell.' )
106+ if result [ 'retc' ]:
107+ lib .base .cu (result [ ' stderr' ] )
108+ result = result [ ' stdout' ] .strip ()
108109
109110 count = sum (1 for line in result .splitlines ())
110111 perfdata = lib .base .get_perfdata ('pending_updates' , count , None , args .WARN , args .CRIT , 0 , None )
111112
112113 if count :
113114 state = lib .base .get_state (count , args .WARN , args .CRIT )
114115 lib .base .oao (
115- 'There are {} pending updates:\n \n * ' .format (count ) + result .replace ('\n ' , '\n * ' ),
116+ (
117+ f"There { lib .txt .pluralize ('' , count , 'is,are' )} { count } pending "
118+ f"{ lib .txt .pluralize ('update' , count )} :\n \n "
119+ f"* { result .replace ('\n ' , '\n * ' )} "
120+ ),
116121 state ,
117122 perfdata ,
118123 always_ok = args .ALWAYS_OK ,
0 commit comments