Microsoft Windows PowerShell快速指南 - 第四部分






4.70/5 (18投票s)
本文(第 4 部分)的目的是向您介绍如何将 Microsoft Windows PowerShell 与 Microsoft .NET 库集成。
目录
- 引言
- 我们实际要做什么?
- Microsoft Windows PowerShell 与 Microsoft .NET 库
- Microsoft.NET 库 - 命名空间
System.Windows.Forms
System.Collections
System.Drawing
System.Reflection
System.IO
System.Xml
- 获取帮助
- Microsoft.NET 库 - 命名空间
- 结论
- 参考
- 历史
引言
本文(第 4 部分)的目的是介绍如何将 Microsoft Windows PowerShell 与 Microsoft .NET 库集成;希望您知道 PowerShell 的一项强大功能就是与 Microsoft .NET Framework 库的集成。
在本文中,我们将讨论 Microsoft Windows PowerShell 与 Microsoft .NET Framework 库的集成。
我们实际要做什么?
我们将尝试演示一些在 PowerShell 脚本中常用的 .NET 类,以及如何在 PowerShell 中编写自定义类。这是一个系列文章,所以如果您错过了前面的文章,不用担心。我希望您能从下面的链接中阅读前面的部分。
Microsoft Windows PowerShell 快速指南
- Microsoft Windows PowerShell 快速指南 - 第 1 部分
- Microsoft Windows PowerShell 快速指南 - 第 2 部分
- Microsoft Windows PowerShell 快速指南 - 第 3 部分
如果您已经熟悉了前面的内容,那么我们还在等什么?让我们开始吧……
Microsoft Windows PowerShell 与 Microsoft .NET 库
我希望您已经掌握了 Microsoft Windows PowerShell 的所有基础知识,并且假设您已经熟悉了 Microsoft .NET 库。所以我们将处理下面列出的项目。
Microsoft.NET 库 - 命名空间
.NET Framework 是一个包含公共语言运行时和基类库的软件框架,它提供了大部分功能。 .NET Framework 有几个版本。
PowerShell 兼容 2.0、3.0 和 3.5 版本。 System.Windows.Forms
。在本节中,我们将讨论其中一些内容,它们列在下面。
System.Windows.Forms
System.Drawing
System.Collections
- 等等。
有关更多信息,请访问此链接。
System.Windows.Forms
System.Windows.Forms
命名空间包含用于创建基于 Windows 的应用程序的类,这些应用程序可以充分利用 Microsoft Windows 操作系统提供的丰富用户界面功能。
有关更多信息,请访问此链接。
好的,现在我们将编写一个 PowerShell 脚本,该脚本将显示一个带有显示消息和退出按钮的 Windows 窗体。示例如下。
示例
$psScriptName = "ScriptWinForm.ps1"
$psScriptAuthor = "Md. Marufuzzaman"
$psScriptDate = "19/02/2009"
## .Net Namespace
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## Creating instance of System.Windows.Forms.Form
$objForm = New-Object System.Windows.Forms.Form
## Set Form caption
$objForm.Text = "The Code Project"
##Set window size
$objForm.Size = New-Object System.Drawing.Size(350,150)
## Set window start position
$objForm.StartPosition = "CenterScreen"
## Add event for key press
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "I will not say I have failed 1000 times; " +
"I will say that I have discovered 1000 " +
"ways that can cause failure – Thomas Edison."
$objForm.Controls.Add($objLabel)
## Creating System.Windows.Forms.Form.Button object
$OkButton = New-Object System.Windows.Forms.Button
## Set the button location / position
$OkButton.Location = New-Object System.Drawing.Size(150,75)
## Set button size
$OkButton.Size = New-Object System.Drawing.Size(75,23)
## Set button text
$OkButton.Text = "OKay"
## Set button click event
$OkButton.Add_Click({$objForm.Close()})
## Add the button into the From
$objForm.Controls.Add($OkButton)
$objForm.Topmost = $True
$objForm
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
图 A 是上述 PowerShell 脚本的输出。
System.Drawing
System.Drawing
命名空间提供了对 GDI+ 基本图形功能的访问。更高级的功能在 System.Drawing.Drawing2D
、System.Drawing.Imaging
和 System.Drawing.Text
命名空间中提供。
有关更多信息,请访问此链接。
好的,现在我们将编写一个 PowerShell 脚本,该脚本将重点介绍 Pen
和 SolidBrush
对象。GDI+ 提供了各种其他绘图对象。以下示例创建了 SolidBrush
和 Pen
对象,它们将执行绘图操作。
示例
$psScriptName = "ScriptSystem.Drawing.ps1"
$psScriptAuthor = "Md. Marufuzzaman"
$psScriptDate = "19/02/2009"
## .Net Namespace
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## Creating instance of System.Windows.Forms.Form
$objForm = New-Object System.Windows.Forms.Form
## Set Form caption
$objForm.Text = "The Code Project"
##Set window size
$objForm.Size = New-Object System.Drawing.Size(250,150)
## Set window start position
$objForm.StartPosition = "CenterScreen"
## Add event for key press
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
## Creating System.Windows.Forms.Form.Button object
$OkButton = New-Object System.Windows.Forms.Button
## Set the button location / position
$OkButton.Location = New-Object System.Drawing.Size(150,75)
## Set button size
$OkButton.Size = New-Object System.Drawing.Size(75,23)
## Set button text
$OkButton.Text = "OKay"
## Set button click event
$OkButton.Add_Click({$objForm.Close()})
$formGraphics = $objForm.createGraphics()
$objForm.add_paint(
{
$myBrush = New-Object System.Drawing.SolidBrush green
$myPen = New-Object System.Drawing.Pen red
$myPen.color = "red"
$myPen.width = 10
$Point1 = new-object Drawing.Point 10, 10
$Point2 = new-object Drawing.Point 100, 30
$Point3 = new-object Drawing.Point 170, 10
$Point4 = new-object Drawing.Point 200, 60
$formGraphics.DrawBezier($myPen, $Point1, $Point2, $Point3, $Point4)
}
)
## Add the button into the From
$objForm.Controls.Add($OkButton)
$objForm.Topmost = $True
$objForm
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
图 B 是上述 PowerShell 脚本的输出。
System.Collections
System.Collections
命名空间包含定义各种对象集合的接口和类,例如列表、队列、位数组、哈希表和字典。
有关更多信息,请访问此链接。
示例
$psScriptName = "ScriptSystem.Collection.ps1"
$psScriptAuthor = "Md. Marufuzzaman"
$psScriptDate = "19/02/2009"
### Variable declaration ###
### hash table ###
$DictionaryEntry = new-object system.collections.DictionaryEntry;
$myHashTable = new-object system.collections.hashtable
### ArrayList ###
$myArrayList = new-object system.collections.ArrayList
## Assign some values
$myHashTable = @{"" = 1; "Code Project" = 2; "Is" = 3; "Cool" = 4}
$myArrayList = @{"" = 1; "Code Project" = 2; "Is" = 3; "Cool" = 4}
foreach($entry in $myHashTable)
{
write-host ($entry.Keys , $entry.Values)
}
foreach($entry in $myArrayList)
{
write-host ($entry.Keys , $entry.Values )
}
输出
Code Project Is Cool 2 3 4 1
Code Project Is Cool 2 3 4 1
获取帮助
Get-Help cmdlet 显示有关 Windows PowerShell cmdlet 和概念的信息。您也可以使用“Help {
语法
- Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-full] [<CommonParameters>]
- Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-detailed] [<CommonParameters>]
- Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-examples] [<CommonParameters>]
- Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-parameter <string>] [<CommonParameters>]
参考文献
- Microsoft 开发网络
结论
希望这对您有所帮助。祝您使用愉快!
历史
- 2010 年 8 月 23 日:初始发布