91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Powershell Scripting Game - Jan 2016

發布時間:2020-07-18 12:52:36 來源:網絡 閱讀:553 作者:beanxyz 欄目:開發技術

每個月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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

五常市| 清涧县| 镶黄旗| 昭通市| 宣武区| 循化| 郁南县| 贵阳市| 祁阳县| 绍兴县| 泾川县| 珠海市| 青冈县| 桑植县| 西藏| 丽水市| 咸丰县| 寿宁县| 策勒县| 南康市| 来宾市| 哈巴河县| 车致| 鄂尔多斯市| 安多县| 壶关县| 万全县| 张家口市| 政和县| 绥芬河市| 鄢陵县| 胶南市| 西乌珠穆沁旗| 黔西县| 抚州市| 北流市| 哈尔滨市| 土默特右旗| 五常市| 搜索| 磐安县|