Powershell Scripting Game - Jan 2016

简介:

每个月Powershell.Org上面都会有一期小挑战,一般都是要求实现一些实践性很强的小脚本。


这一期的链接如下

http://powershell.org/wp/2016/01/02/january-2016-scripting-games-puzzle/


基本要求是:


Server uptime is the lifeblood of system administrators. We strive on it, get addicted to it..we need…more server uptime! Don’t you think something as addictive and important as server uptime be measured?  How do we know we’re getting our uptime fix?  As that famous quote goes, “Reality does not exist until it’s measured.”.  Let’s measure it not only for our own sake but also to give a pretty report to our manager with all those whizbang, doohickey Excel juju that they love to see!

For this month’s challenge, I want you to create a PowerShell function that you can remotely point to a Windows server to see how long it has been up for. Here’s an example of what it should output.

image001

Requirements:

1.     Support pipeline input so that you can pipe computer names directly to it.

2.     Process multiple computer names at once time and output each computer’s stats with each one being a single object.

3.     It should not try to query computers that are offline. If an offline computer is found, it should write a warning to the console yet still output an object but with Status of OFFLINE.

4.     If the function is not able to find the uptime it should show ERROR in the Status field.

5.     If the function is able to get the uptime, it should show ‘OK’ in the Status field.

6.     It should include the time the server started up and the uptime in days (rounded to 1/10 of a day)

7.     If no ComputerName is passed, it should default to the local computer.

 

Bonus:

1.     The function should show a MightNeedPatched property of $true ONLY if it has been up for more than 30 days (rounded to 1/10 of a month).  If it has been up for less than 30 days, MightNeedPatched should be $false.


豆子自己写了个简单的函数,时间有限,基本实现了题目90%的功能。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function  Get-Uptime {
<#
.Synopsis
    Get machine uptime from remtoe machine
.DESCRIPTION
    Get machine from remtoe machine
.EXAMPLE
    Get-Uptime sydav01,sydit01
    This will get the up time of server sydav01 and sydit01
.INPUTS
    String name of server names
.OUTPUTS
    Output from this cmdlet (if any)
.NOTES
    This is a test function
.COMPONENT
    The component this cmdlet belongs to Yuan Li
.ROLE
    The role this cmdlet belongs to Yuan Li
.FUNCTIONALITY
    The functionality that best describes this cmdlet
#>
 
 
 
     Param
     (
         # Param1 help description
         [ Parameter (
                    ValueFromPipelineByPropertyName = $true ,  
                    Position =1)
                    ] [string[]] $ComputerNames = $env:COMPUTERNAME
 
    
     )
 
#
 
$pp = $null
$pp = [ordered] @{ 'Computername' = $null ; 'StartTime' = $null ; 'Uptime(Days)' = $null ; 'Status' = $null ; 'MightNeedPatched' = $null }
$obj = New-Object  -TypeName psobject -property  $pp
 
 
$result =@()
 
 
 
 
foreach  ( $b  in  $ComputerNames ){
 
 
if  ( Test-Connection  $b  -Count 1 -Quiet){
 
 
$os = Get-CimInstance  -ComputerName  $b  -ClassName win32_operatingsystem 
 
 
$objtemp = $obj  |select  *
$objtemp .Computername= $b
 
$objtemp .starttime= $os .LastBootUpTime
 
if ( $os .LastBootUpTime  -ne  $null ){
 
$objtemp .status= "Online"
 
}
else
{
$objtemp .status= "Error"
}
 
 
$objtemp . 'Uptime(Days)' =( $os .LocalDateTime- $os .LastBootUpTime).Days
 
if  ( $objtemp . 'Uptime(Days)'  -gt  30)
{
$objtemp .MightNeedPatched= $True
}
else
{ $objtemp .MightNeedPatched= $False }
 
 
$result += $objtemp
 
}
else {
$warning "Server " + $b + " is Offline!"
$warning  Write-Warning
 
$objtemp = $obj  |select  *
$objtemp .Computername= $b
$objtemp .status= "Offline"
 
$result += $objtemp
 
 
 
}
 
}
 
 
$result  | sort Status | ft
}
 
$names = get-adcomputer  - Filter  {operatingsystem  -like  "*2012*"
Get-Uptime  -ComputerNames  $names .name


基本思路很简单:创建一个空的自定义的对象,根据PING的结果把不同的值放进这个对象

需要注意的小地方:

  1. Hash表默认是无序的,因此定义的时候需要指定为有序,不然生成的对象顺序也是随机的。

  2. 获取wmi的值,powershell里面可以用get-wmiobject 或者 get-ciminstance ,后者支持管道和智能提示,不过只能在Powershell v3以后 版本使用。这里我使用的是后者,因为他的lastbootuptime会自动转换成人更可读的格式。前者还需要手动转换一下。

  3. Test-Connection 判断PING,这里我只发送了一个ICMP包作测试


不足之处:(稍后有空补上)

  1.  我没有做任何容错处理,只有一个简单的判断语句而已;应该加入try..catch 和 ErrorAction/Error Value等

  2. 我没有考虑时间的四舍五入进位(1/10天)



结果如下:


wKioL1adxxzC1twXAAC2i8X7P-c298.png










本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1736443,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
53 0
|
6月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
51 0
|
9月前
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
9月前
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
102 0
|
应用服务中间件 nginx Windows
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
|
XML 监控 数据格式
利用powershell进行windows日志分析
0x00 前言   Windows 中提供了 2 个分析事件日志的 PowerShell cmdlet:一个是Get-WinEvent,超级强大,但使用起来比较麻烦;另一个是Get-EventLog,使得起来相当简单,可以实时筛选,接下来,我们利用PowerShell 来自动筛选 Windows 事件日志。
2438 0
|
监控 Windows
Windows Server端口监控之powershell脚本
powershell检测端口并重启程序
603 0