最近PowerShell的QQ群里面流行用GUI界面装B,为了紧跟潮流,豆子也做了个小程序,这个小程序的可以根据操作系统和关键字,扫描域内的计算机是否安装了对应的软件。
界面如下
这个界面是通过PowerShell Studio 2015做的,主要使用了label,combox, textbox, datagridview,savafilediag, button几个控件。
几个关键点
1. 根据操作系统和关键字查询
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
|
function
Get-Software
{
[
cmdletbinding
()]
param
(
[
parameter
(
mandatory
=
$true
,
position
= 1)]
[string]
$software
,
[string]
$computername
=
"*"
,
[string]
$OS
)
Write-Verbose
"Scanning Computers..."
if
(
$computername
-ne
'*'
)
{
$a
=
Get-ADComputer
-
Filter
"operatingsystem -like '*$OS*' -and name -like '*$computername*' "
-Properties operatingsystem, ipv4address |
Where-Object
{
$_
.ipv4address
-ne
$null
} | select -ExpandProperty name
}
else
{
$a
=
Get-ADComputer
-
Filter
"operatingsystem -like '*$OS*' "
-Properties operatingsystem, ipv4address |
Where-Object
{
$_
.ipv4address
-ne
$null
} | select -ExpandProperty name
}
$progressbaroverlay1
.Maximum=100
$progressbaroverlay1
.Value=20
#$MainForm.Refresh()
Write-Verbose
"Scanning Software ..."
$s
=
Invoke-Command
-ComputerName
$a
-erroraction SilentlyContinue -ErrorVariable disconnect{
param
(
[string]
$name
)
if
(
[System.IntPtr]
::Size
-eq
4)
{
Get-ItemProperty
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object
{
$_
.displayname
-like
"*$name*"
} |
Select-Object
DisplayName, DisplayVersion, Publisher, InstallDate
}
else
{
Get-ItemProperty
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object
{
$_
.displayname
-like
"*$name*"
} |
Select-Object
DisplayName, DisplayVersion, Publisher, InstallDate
}
} -ArgumentList
$software
$progressbaroverlay1
.Value=80
#$MainForm.Refresh()
#Write-Verbose "Disconnected Computers"
#$disconnect.targetobject
$progressbaroverlay1
.Value |
Out-String
|Write
-Host
$s
}
|
2. 把上面生成的对象绑定到datagridview 里面 注意 ConvertTo-DataTable 是通过自带的Snippet生成的(拖曳Snippet到自己的脚本块即可)
1
2
3
|
$table
=
ConvertTo-DataTable
-InputObject
$result
Load-DataGridView -DataGridView
$datagridview1
-Item
$table
$progressbaroverlay1
.Visible =
$false
|
3. DataGridView排序
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$datagridview1_ColumnHeaderMouseClick
=
[System.Windows.Forms.DataGridViewCellMouseEventHandler]
{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
if
(
$datagridview1
.DataSource
-is
[System.Data.DataTable]
)
{
$column
=
$datagridview1
.Columns[
$_
.ColumnIndex]
$direction
=
[System.ComponentModel.ListSortDirection]
::Ascending
if
(
$column
.HeaderCell.SortGlyphDirection
-eq
'Descending'
)
{
$direction
=
[System.ComponentModel.ListSortDirection]
::Descending
}
$datagridview1
.Sort(
$datagridview1
.Columns[
$_
.ColumnIndex],
$direction
)
}
}
|
4. 保存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Function
Set-FileName
(
$initialDirectory
)
{
[System.Reflection.Assembly]
::LoadWithPartialName(
"System.Windows.Forms"
)
|Out
-Null
$savefiledialog1
.InitialDirectory =
$initialDirectory
$savefiledialog1
.
Filter
=
"All files (*.*) | *.*"
$savefiledialog1
.ShowDialog() |
Out-Null
$savefiledialog1
.FileName
#model:Set-FileName -initialDirectory "D:\Scripts\例子"
}
$button
保存_Click = {
#TODO: Place custom script here
$file
=
set-FileName
Write-Verbose
"Print Out"
$datagridview1
.Rows |
select -expand DataBoundItem |
export-csv
$file
-NoType
}
|
5.进度条
我是手动在对应的地方配置的进度比例。当然也可以根据循环自动的增加进度step。
比如快结束的时候,我设定进度为90%
1
|
$progressbaroverlay1
.Value = 90
|
附件里整个project的源代码,感兴趣的可以下载看看。
本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1783800,如需转载请自行联系原作者