PowerShell Function之获取OS信息

来源:互联网 发布:淘宝上买电器可靠吗 编辑:程序博客网 时间:2024/04/24 01:14

我想写一个Function系列,把常用的获取信息的语句写成一个又一个的Function,也方便后面写PowerShell生成Html系列文章时,直接采取以写好的Function来调用就好了。

最熟悉不过的就是获取OS信息啦,那么也就从这个开始写吧。

说明,获取计算机信息可以使用Get-ADComputer,但我在本系列文章将采用Get-WmiObject来获取相关信息。说明一下,Get-WmiObject在powershell 3.0以后就逐渐被Get-CimInstance取代了。不过Get-WmiObject在powershell 3.0及以后的版本还是可以运行的,只不过Get-CimInstance执行的结果只是名副其实的Instance,没有包含任何方法信息。

想了下,应该是直接上脚本呢,还是写个过程引导读者如何写出最终的脚本。考虑到这一系列的文章适合小白阅读,那么还是将整个过程简单写出来会更好些。

1. 确定合适的class

WMI的类有很多,到底哪个class才会提供OS的信息给我们呢?等等,OS是什么的简写?OperatingSystem!因此你是不是应该想到这个class的名称应该为Win32_OperatingSystem呢?当然,作为小白的话,可能你都不确定这个class是否存在,那么则可以运行以下脚本来获取到所有的class(在PowerShell介绍第六回有提到过).
$i=0 $Type = "Win32" $WMI = Get-WmiObject -List | Where-Object {$_.name -Match $Type}Foreach ($Class in $WMI) {$Class.name | out-file –filepath e:\win32.csv -append; $i++}
确定了合适的class,第一条句子应该就已经会写了:
$OS = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName
先在PS上运行下,看看得出来的都是一些什么信息。

是不是发现了上面好像缺了一些我们想要的信息,对吧。难道只能获取到这么点信息么?不可能吧!
那么,应该怎么查看是否还能获取到其他信息?添加个| FL没用!
应该使用Get-CimInstance Win32_OperatingSystem | select -ExpandProperty CimInstanceProperties | fl *
运行结果一看,不要太激动!是不是看到了你想要的信息了,例如系统版本,安装时间等等几十种信息!
 

2. 自定义输出属性

用上面的命令可以获取到OS的几十种属性,那么,我应该如何选择我需要输出的属性呢?方法也很简单。最简单的就是直接select,想要输出什么属性就select的什么。但是在此我想介绍另外一种更高级的方法,通过创建数组来定义输出的属性。
$os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName$props = @{'OSVersion'=$os.version;'Manufacturer'=$os.Manufacturer;'OperatingSystem'=$os.Caption;'InstallDate'=$os.InstallDate}New-Object -TypeName PSObject -Property $props
但是这里还有点美中不足的就是$os.InstallDate出来的格式不是我们想要的,那么又应该如何转换成我们想要的时间格式呢?

此时应该隆重介绍ConvertToDateTime()出场啦!使用ConvertToDateTime()可以将日期和时间的指定字符串表示形式转换为等效的日期和时间值。
$osInstallDate=$osInstallDate=$os.ConvertToDateTime($os.InstallDate)
但这样的显示结果往往还不是我们想要的,在脚本中多数看到的格式都是yyyyMMdd这些类型个格式,那么又应该如何转换呢?此时应该再介绍一个好东西ToString
$osInstallDates = $osInstallDate.ToString("yyyy-MM-dd HH:mm:ss")
输出效果如下:


那么此时,应该优化我们都的脚本啦!
$os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName;$osInstallDate=$os.ConvertToDateTime($os.InstallDate);$osInstallDates = $osInstallDate.ToString("yyyyMMdd");$props = @{'OSVersion'=$os.version;'Manufacturer'=$os.Manufacturer;'OperatingSystem'=$os.Caption;'InstallDate'=$osInstallDates;}New-Object -TypeName PSObject -Property $props
运行结果如下:


是不是很有成就感呢!哎呀,是不是又觉得我们跑题了,说好的function呢?别急,接下来我们就优化脚本成一个function吧!

3. 编写Function函数

此时应该再复习一下function的语法哈!
function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]          {              param([type]$parameter1 [,[type]$parameter2])          }
说明:
函数中包含以下各项:
- 一个 Function 关键字
- 一个作用域(可选)
- 一个由您选择的名称
- 任意数目的命名参数(可选)
- 括在大括号 ({}) 内的一条或多条 Windows PowerShell 命令
function Get-InfoOS {$os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName;$osInstallDate=$os.ConvertToDateTime($os.InstallDate);$osInstallDates = $osInstallDate.ToString("yyyy-MM-dd HH:mm:ss");$props = @{'OSVersion'=$os.version;'Manufacturer'=$os.Manufacturer;'OperatingSystem'=$os.Caption;'InstallDate'=$osInstallDates;}New-Object -TypeName PSObject -Property $props}
是不是很简单?运行下试试?

再继续玩点高级的,定义个可输入的参数如何?

4.定义接受输入的参数

就是前面提到的param啦,直接上脚本,不清楚的童鞋可以复习之前的文章!
function Get-InfoOS {[CmdletBinding()]param([Parameter(Mandatory=$True)][string]$ComputerName)$os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName;$osInstallDate=$os.ConvertToDateTime($os.InstallDate);$osInstallDates = $osInstallDate.ToString("yyyy-MM-dd HH:mm:ss");$props = @{'OSVersion'=$os.version;'Manufacturer'=$os.Manufacturer;'OperatingSystem'=$os.Caption;'InstallDate'=$osInstallDates;}New-Object -TypeName PSObject -Property $props}


是否还有童鞋说,我有很多计算机啊,我不想手动敲进去啊,我可以准备个list,能不能读取list清单就好了呢?当然没问题啦!

5. Import 计算机清单

可以通过import-CSV和get-content来实现上面的需求,但是我个人比较喜欢使用import-CSV
function Get-InfoOS {[CmdletBinding()]param([Parameter(Mandatory=$True)][string]$FilePath)$Check_Day = (Get-Date).ToString("yyyyMMdd");$servers = import-csv .\serverlist.txt;$Output_file_OSInfo = ".\OSInfo$Check_Day.html";Foreach ($server in $Servers){$ComputerName = $server.server;$os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName;$osInstallDate=$os.ConvertToDateTime($os.InstallDate);$osInstallDates = $osInstallDate.ToString("yyyy-MM-dd HH:mm:ss");$props = @{'OSVersion'=$os.version;'Manufacturer'=$os.Manufacturer;'OperatingSystem'=$os.Caption;'InstallDate'=$osInstallDates;}$OSInfo=New-Object -TypeName PSObject -Property $props;$ComputerName,$OSInfo | Out-File $output_file_OSInfo -Append -Encoding UTF8}}
虽然输出的html很丑,这个不在本章节的探讨范围之内。后续会写上如何优化我们的输出。
0 0
原创粉丝点击