zoukankan      html  css  js  c++  java
  • PowerShell2.0之桌面计算机维护(九)磁盘管理

    1 处理磁盘分区

    PC通常只会有一个磁盘,Windows的管理策略会以分区形式管理磁盘,这样即可将物理硬件从操作系统中抽象出来。分区概念对于高效率地维护系统和文件很有好处,用户可以在“计算机管理”的“磁盘管理”工具中查看磁盘和磁盘分区之间的关系,如图23所示。

    image

    图1 输出磁盘信息

    image

    图2 磁盘和磁盘分区的关系

    创建名为“ReportDiskPartition.ps1”的脚本,用于获取系统中存在的分区属性。其中将检查变量$args的值,以判断执行脚本时是否传递参数。如果不存在该变量,则表明在运行脚本时未提供参数。此时脚本会作为本地计算机处理,即传递localhost给$args变量。如果传递问号给脚本,则返回当前脚本的帮助信息,该脚本的代码如下:

    if(!$args)

    {

    Write-Host -foregroundcolor green `

    'Querying localhost ...'

    $args = 'localhost'

    }

    if($args -eq "?")

    { "

    ReportDiskPartition.ps1

    DESCRIPTION:

    This script can take a single argument, computer name.

    It will display drive configuration on either a local

    or a remote computer. You can supply either a ? or a

    name of a local machine.

    EXAMPLE:

    ReportDiskPartition.ps1 remoteComputerName

    reports on disk partition information on a computer named

    remoteComputerName

    The script will also display this help file. This is

    done via the ? argument as seen here.

    ReportDiskPartition.ps1 ?

    "

    }

    Get-WmiObject -Class Win32_DiskPartition `

    -computer $args

    其中使用Get-WmiObject cmdlet及-class参数搜索Win32_DiskPartition WMI类,并获得磁盘分区的配置信息和值。如果使用$args参数提供了要查询磁盘分区信息的计算机名,则可使用-computer参数为Get-WmiObject提供计算机名,执行结果如图24所示。

    image

    图3 执行结果

    2 匹配磁盘和分区

    匹配驱动器和分区之后,还需要相应处理磁盘和分区的脚本,因为有时需要特定驱动器的分区信息。创建名为“ReportSpecificDiskPartition.ps1”的脚本来获取硬盘特定分区的配置信息,其代码如下:

    param($computer="localhost",$disk="磁盘 #0,分区 #0",$help)

    if($computer)

    {

    Write-Host -foregroundcolor green `

    "Querying $computer ..."

    }

    if($disk)

    {

    Write-Host -foregroundcolor green `

    "Querying $disk for partition information ..."

    }

    if($help)

    { "

    ReportSpecificDiskPartition.ps1

    DESCRIPTION:

    This script can take a multiple arguments, computer name,

    drive number and help.

    It will display partition configuration on either a local

    or a remote computer. You can supply either help, drive and

    name of a local or remote machine.

    EXAMPLE:

    ReportSpecificDiskPartition.ps1 -computer remoteComputername

    reports on disk partition on drive 0 on a computer named

    remoteComputerName

    ReportSpecificDiskPartition.ps1 -computer remoteComputername -disk '磁盘 #0,分区 #0'

    reports on disk partition on drive 1 on a computer named

    remoteComputerName

    ReportSpecificDiskPartition.ps1 -help y

    Prints out the help information seen here.

    "

    Exit

    }

    Get-WmiObject -Class Win32_DiskPartition `

    -computer $computer | Where-Object { $_.name -match $Disk } |

    format-list [a-z]*

    image

    图4 执行结果

    这个脚本的3个参数分别是-computer、-disk和-help,其中-computer默认为localhost,即查询本机的分区信息;-disk指定特定磁盘分区信息,这里将默认值设置为“磁盘#0,分区#0”,即第1块硬盘的第1个分区;-help输出脚本的名称、描述信息及语法范例,输出帮助信息后将会使用exit语句退出脚本。该脚本的执行结果如图25所示。

    3 处理逻辑磁盘

    为了能够获得计算机中有关逻辑磁盘的配置信息,需要使用Get-WmiObject cmdlet。然后使用-class参数查询Win32_LogicalDisk WMI类,并且可以通过设定-computer参数查询指定计算机的信息。

    所有参数通过$args变量传递到脚本中,如果该变量不存在,则根据默认值获取第1个逻辑磁盘的信息。创建名为“ReportLogicalDiskConfiguration.ps1”的脚本查询系统中存在的逻辑磁盘,其代码如下:

    param($computer="localhost",$disk="C:",$help)

    if($computer)

    {

    Write-Host -foregroundcolor green `

    'Querying localhost ...'

    }

    if($disk)

    {

    Write-Host -foregroundcolor green `

    'Querying $disk for logical disk info...'

    # $args = 'localhost'

    }

    if($help)

    { "

    ReportLogicalDiskConfiguration.ps1

    DESCRIPTION:

    This script can take a single argument, computer name.

    It will display logical disk configuration on either a local

    or a remote computer. You can supply either a ? or a

    name of a local machine.

    EXAMPLE:

    ReportLogicalDiskConfiguration.ps1 remoteComputerName

    reports on logical disk configuration on a computer named

    remoteComputerName

    The script will also display this help file. This is

    done via the ? argument as seen here.

    ReportLogicalDiskConfiguration.ps1 ?

    "

    }

    Get-WmiObject -Class Win32_LogicalDisk `

    -computer $computer | Where-Object {$_.deviceID -match $Disk} |

    Format-List [a-z]*

    此脚本的执行结果如图26所示。

    image

    图5 执行结果

    作者: 付海军
    出处:http://fuhj02.cnblogs.com
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
    要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
    个人网站: http://txj.lzuer.com/

  • 相关阅读:
    400 Bad Request
    Django 中间件 阅读目录 初步了解 中间件 中间件的介绍 自定义中间件 process_template_response(用的比较少) 中间件的执行流程 中间件版登录验证
    关于python语言优化的一些思考
    从系统角度考虑性能优化 【被面试官吊打】从系统角度考虑性能优化 目录 什么是性能? 定义系统范围 重定义功能 关注价值 总结
    需求设计
    开源软件评选白热化,这些项目冲击 Top 5
    两个向量的outer product
    协同过滤算法中皮尔逊相关系数的计算 C++
    求向量间的点积
    string 类型的翻转
  • 原文地址:https://www.cnblogs.com/fuhj02/p/1936505.html
Copyright © 2011-2022 走看看