Powershell Scripting Game - Jan 2016
admin
2023-02-16 21:00:06
0

每个月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.

Powershell Scripting Game - Jan 2016

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%的功能。


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天)



结果如下:


Powershell Scripting Game - Jan 2016

相关内容

热门资讯

河北一演唱会音响“翻车”,人声... 近日,来自北京的庞女士向都市现场记者反映,她7月11日在河北唐山观看“青春超燃巨星演唱会”时遭遇了极...
尺素金声|中国经济“失速论”站... 一季度GDP同比增长5.0%、二季度增长4.3%、上半年增长4.7%,2026年中国经济半年报发布后...
NVIDIA最深的护城河要凉!... 7月24日消息,AMD在Advancing AI预简报会上表示,CUDA已不再是外界想象中的那条护城...
廖杰远:三大智能体协同闭环,A... 中国日报网7月24日电 7月22日,在2026年世界互联网大会数字丝路发展论坛数智健康分论坛上,微医...
AI时代构筑超级底座 维谛技术... 文/本报记者 当前,人工智能产业竞争持续向产业链上游延伸,算力竞争早已不再局限于计算芯片性能比拼。具...
刚签核协议不到24小时,美国突... 美国东部时间7月22日下午,美国宣布与沙特阿拉伯签署了一项民用核能协议。据美国有线电视新闻网(CNN...
中国信通院牵头,数据编织国际标... IT之家 7 月 24 日消息,中国信通院今日发文称,国际电信联盟第 21 研究组(ITU-T SG...
国内首颗商业空间碎片监测卫星发... 【国内首颗商业空间碎片监测卫星发射成功】《科创板日报》24日讯,7月24日上午,中科宇航力箭一号运载...
邓煜,诗人,数学家,老二次元 来源 视觉中国菲尔兹奖的通知邮件抵达时,普林斯顿大学数学博士、芝加哥大学数学系教授邓煜正在看一本恋爱...
安全生产许可证状态不是“有效”... 工程公司的安全生产许可证在官方系统显示为“其他”状态,有一栏则显示“整改”,却成为高标准农田项目的第...