Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236222
  • 博文数量: 50
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1487
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:31
文章分类
文章存档

2014年(20)

2013年(30)

分类: 网络与安全

2013-09-27 14:37:49

我们可以通过提供的cmdlet来帮助快速提取信息从远程客户端。
然后,我们可以拥有的PowerShell的标志值将被视为一个可接受的范围内。


下面的代码是一个小例子,我将展示给大家如何提取客户端的硬盘可用空间。
这个简单的例子将展示使用颜色标记潜在的问题。

这有一个开关参数cmdlet - passthru。
因为这个代码是用于不是特别喜欢PowerShell用户,默认的行为是显示彩色信息。

使用passthru参数抑制使用写主机和允许对象被传递到管道。

在这种情况下,我们有对象。通过这样做,我们有能力继续通过我们的对象进入管道。下面是一些例子:



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
    
Function Get-DiskInfo
{
    Param(
        $ComputerName = $env:COMPUTERNAME,
        [Switch]$PassThru
    )
                                                                                                                                                    
    Function Get-ColorSplat
    {
        # Create color Splats
        $C1 = @{ForegroundColor="Green";BackgroundColor="DarkGreen"}
        $C2 = @{ForegroundColor="Yellow";BackgroundColor="DarkYellow"}
        $C3 = @{ForegroundColor="White";BackgroundColor="DarkRed"}
        $C4 = @{ForegroundColor="Blue";BackgroundColor="Gray"}
                                                                                                                                                    
        # Create color constants in the previous scope.
        New-Variable -Name "Good" -Value $C1 -Scope 1
        New-Variable -Name "Problem" -Value $C2 -Scope 1
        New-Variable -Name "Bad" -Value $C3 -Scope 1
        New-Variable -Name "Header" -Value $C4 -Scope 1
    } # End: Get-ColorSplat
                                                                                                                                                    
    Function Write-ColorOutput
    {
                                                                                                                                                    
        Param($DiskInfo)
                                                                                                                                                    
        # Display the headers.
        Write-host "DiskInfo | FreeSpaceGB | PercentFreeSpace"
                                                                                                                                                    
        # Display the data.
        ForEach ($D in $DiskInfo)
        {
            $DeviceID = $D.DeviceID.PadRight(6)
            $FSGB = $D.FreeSpaceGB.ToString().PadRight(6).Remove(5)
            $PFS = $D.PercentFS.ToString().PadRight(6).Remove(5)
                                                                                                                                                    
            If ($D.PercentFS -ge 80)
            { Write-Host "$($DeviceID)   | $($FSGB)       | $($PFS)" @Good }
            ElseIf (($D.PercentFS -lt 80) -and ($D.PercentFS -GE 60))
            { Write-Host "$($DeviceID)   | $($FSGB)       | $($PFS)" @Problem }
            Else
            { Write-Host "$($DeviceID)   | $($FSGB)       | $($PFS)" @Bad }
                                                                                                                                                    
        }
    }
                                                                                                                                                    
    # Get the color splats
    Get-ColorSplat
                                                                                                                                                    
    $DiskInfo = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName |
     Select-Object -Property DeviceID,
     @{Name="FreeSpaceGB";Expression={$_.Freespace/1GB}},
     @{Name="PercentFS";Expression={($_.FreeSpace/$_.Size)*100}}
                                                                                                                                                    
     If (!$PassThru) {Write-ColorOutput -DiskInfo $DiskInfo}
     Else {Write-Output $DiskInfo}
}




在这个示例中,我们看到默认的输出不同颜色的信息绘制技术员到硬盘,需要注意

150718281.png


在这个示例中,使用passthru参数和我们再次处理的对象。记得代码和显示信息,是相关的和适合他们的技能水平。


1
2
3
4
5
6
7
8
    
PS> Get-DiskInfo -ComputerName CantgisPC2 -PassThru | Format-Table -AutoSize
                                                      
DeviceID      FreeSpaceGB        PercentFS
--------      -----------        ---------
C:       34.6056213378906 31.7261065852248
D:       202.438598632813 84.9355612944946
E:       79.9115943908691 33.5278754613268
Z:       83.1089553833008 8.95466512825099



这几种方法将允许PowerShell的工作人员把重点放在什么需要他们的注意的地方,从而忽略那些不过滤cmdlet的输出。
阅读(1730) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~