WindowsPowerShell与.NET框架的深入探索
立即解锁
发布时间: 2025-08-19 01:07:19 阅读量: 1 订阅数: 3 


PowerShell 2.0编程入门与实践
### Windows PowerShell定制与.NET框架资源访问
#### 1. Windows PowerShell定制技巧
在深入了解.NET框架以及Windows PowerShell如何使用cmdlet访问.NET资源之前,我们先看看另一种定制Windows PowerShell的方法。
你可以通过修改PowerShell快捷方式来定制工作环境。此外,还能通过创建名为`profile.ps1`的PowerShell脚本文件并将其存储在配置文件文件夹(由`$profile`变量指定)中,以编程方式配置PowerShell环境。每次启动新的Windows PowerShell会话时,`profile.ps1`文件中的脚本会自动执行,且不影响其他用户。
若将`profile.ps1`文件移动到`C:\Windows\System32\WindowsPowerShell\v1.0`文件夹,每次启动新的Windows PowerShell会话时,该脚本会为计算机的每个用户自动运行。
计算机管理员可以开发`profile.ps1`脚本,将其远程部署到多台计算机,从而管理大量计算机,无需逐台访问和配置。以下是一个示例脚本:
```powershell
# *************************************************************************
#
# Script Name: Profile.ps1 (PowerShell Profile configuration Scripts)
# Version: 2.0
# Author: Jerry Lee Ford, Jr.
# Date: October 10, 2008
#
# Description: This PowerShell script contains commands that customize
# the Windows PowerShell execution environment.
#
# *************************************************************************
#Create a custom alias command
Set-Alias ds Write-Host
#Clear the Windows command console screen
Clear-Host
#Display custom greeting
ds
ds "This computer and network are private. By using this computer you"
ds "agree to all terms outlined in the company's security policy."
ds "Failure to comply with these policies may result in criminal"
ds "prosecution."
ds
```
执行该脚本时,会创建一个名为`ds`的自定义别名,它是`Write-Host` cmdlet的快捷方式。接着清屏,并使用新创建的别名显示有关公司安全策略的消息。
#### 2. 微软.NET框架
.NET框架是现代Windows PowerShell脚本的核心组件。Windows PowerShell 1.0和2.0都需要.NET框架2.0版本。因此,PowerShell程序员需深入了解.NET框架的主要组件和服务。
.NET是微软开发的框架,支持桌面、网络和基于互联网的应用程序及脚本,也支持为PDA等设备开发移动应用。框架是资源集合,可简化新程序和脚本的开发,让程序员专注于解决特定问题的高级逻辑。
.NET框架支持与任何.NET兼容的应用程序或脚本开发编程语言结合使用。微软围绕.NET框架开发了一系列应用程序编程语言,如Visual Basic、C++、C#和J#。微软将.NET框架作为新编程语言的关键组件,所以将其集成到Windows PowerShell中,使其脚本语言能立即访问大量资源和命令。
#### 3. .NET框架的关键组件
.NET框架是Windows PowerShell与操作系统之间的接口,负责将脚本代码转换为计算机可执行的格式。它由两个关键组件组成:
- .NET框架类库
- CLR(公共语言运行时)
##### 3.1 .NET类库
传统命令外壳对数据的识别有限,通常仅识别字符串和数字。而Windows PowerShell强制使用结构化数据,能处理多种类型的数据,如字符串、日期、整数、浮点数、布尔数据等。
结构化数据是.NET框架的关键特性。在.NET中,结构化数据被分组到不同集合中,定义复杂的结构化类。类作为创建对象的模板,对象代表Windows PowerShell可访问和操作的事物。例如,文件、文件夹和磁盘驱动器在Windows PowerShell中都被视为对象。
对象具有定义其特定特征的属性,如文件夹有名称。对象还提供对预定义代码集合(即方法)的访问,可执行这些方法来与对象交互和控制对象。例如,文件对象提供用于对文件执行各种操作的方法,包括打开、关闭和删除文件。
##### 3.2 公共语言运行时
公共语言运行时(CLR)负责将Windows PowerShell脚本转换为计算机可理解和运行的可执行格式。CLR还为Windows PowerShell提供其他服务,包括:
- 编译
- 安全
- 内存管理
- 异常处理
#### 4. 访问.NET框架资源
Windows PowerShell通过cmdlet访问.NET框架资源。cmdlet在提供对.NET资源访问的同时,隐藏了很多复杂性。作为PowerShell程序员,你只需知道使用哪些cmdlet来获取脚本所需的资源类型。以下是一些示例:
##### 4.1 执行Get-ChildItem cmdlet
```powershell
PS C:\MyScripts> GGet-ChildItem
Directory: Microsoft.PowerShell.Core\FileSystem::C:\MyScripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/26/2006 6:14 PM 4661 FortuneTeller.ps1
-a--- 10/3/2008 11:51 AM 1077 KnockKnock.ps1
-a--- 10/3/2008 11:51 AM 832 Profile.ps1
-a--- 10/3/2008 11:51 AM 7701 SeinfeldTrivia.ps1
-a--- 9/23/2006 2:22 PM 7598 ThreeAmigos.ps1
PS C:\MyScripts>
```
此命令检索当前工作目录中每个文件和子文件夹的对象列表。默认情况下,Windows PowerShell仅显示文件对象的部分属性。
##### 4.2 聚焦特定文件
```powershell
PS C:\MyScripts> GGet-ChildItem Profile.ps1
Directory: Microsoft.PowerShell.Core\FileSystem::C:\MyScripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/3/2008 11:51 AM 832 Profile.ps1
PS C:\MyScripts>
```
将`profile.ps1`作为参数传递给`Get-ChildItem` cmdlet,仅显示该文件对象的属性信息。
##### 4.3 查看更多属性信息
```powershell
PS C:\MyScripts> GGet-ChildItem Profile.ps1 | Get-Member -MemberType Property
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property System.DateTime CreationTime {get;set;}
CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property System.String DirectoryName {get;}
Exists Property System.Boolean Exists {get;}
Extension Property System.String Extension {get;}
FullName Property System.String FullName {get;}
IsReadOnly Property System.Boolean IsReadOnly {get;set;}
LastAccessTime Property System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime Property System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;}
Length Property System.Int64 Length {get;}
Name Property System.String Name {get;}
PS C:\MyScripts>
```
将`Get-ChildItem` cmdlet的输出传递给`Get-Member` cmdlet,并指定`-MemberType`为`Property`,可显示.NET框架为该文件存储的其他属性信息。
##### 4.4 选择特定属性
```powershell
PS C:\MyScripts> GGet-ChildItem Profile.ps1 | Select-Object name, extension, directory
Name Extension Directory
---- --------- ---------
Profile.ps1 .ps1 C:\MyScripts
PS C:\MyScripts>
```
使用`Select-Object` cmdlet检索并显示文件对象的特定属性,如`Name`、`Extension`和`Directory
0
0
复制全文
相关推荐










