• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Windows Powershell创建对象

Windows Powershell创建对象

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了windows powershell,windowspowershell2.0,windows powershell64,windows10 powershell,windows7 powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

通过New-Object创建新对象

如果使用构造函数创建一个指定类型的实例对象,该类型必须至少包含一个签名相匹配的构造函数。例如可以通过字符和数字创建一个包含指定个数字符的字符串:

PS C:Powershell> New-Object String(‘*',100)
</div>

*******************************************************************************
*********************

为什么支持上面的方法,原因是String类中包含一个Void .ctor(Char, Int32) 构造函数

PS C:Powershell> [String].GetConstructors() | foreach {$_.tostring()}
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char[])
Void .ctor(Char, Int32)
</div>

通过类型转换创建对象

通过类型转换可以替代New-Object

PS C:Powershell> $date="1999-9-1 10:23:44"
PS C:Powershell> $date.GetType().fullName
System.String
PS C:Powershell> $date
1999-9-1 10:23:44
PS C:Powershell> [DateTime]$date="1999-9-1 10:23:44"
PS C:Powershell> $date.GetType().FullName
System.DateTime
PS C:Powershell> $date

1999年9月1日 10:23:44
</div>

如果条件允许,也可以直接将对象转换成数组

PS C:Powershell> [char[]]"mossfly.com"
m
o
s
s
f
l
y
.
c
o
m
PS C:Powershell> [int[]][char[]]"mossfly.com"
109
111
115
115
102
108
121
46
99
111
109
</div>

加载程序集

自定义一个简单的C#类库编译为Test.dll:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace Test
{
    public class Student
    {
        public string Name { set; get; }
        public int Age { set; get; }
        public Student(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public override string  ToString()
        {
            return string.Format("Name={0};Age={1}", this.Name,this.Age);
        }
    }
}
</div>

在Powershell中加载这个dll并使用其中的Student类的构造函数生成一个实例,最后调用ToString()方法。

PS C:Powershell> ls .Test.dll

    目录: C:Powershell

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2012/1/13     10:49       4608 Test.dll

PS C:Powershell> $TestDLL=ls .Test.dll
PS C:Powershell> [reflection.assembly]::LoadFile($TestDLL.FullName)

GAC    Version        Location
---    -------        --------
False  v2.0.50727     C:PowershellTest.dll

PS C:Powershell> $stu=New-Object Test.Student('Mosser',22)
PS C:Powershell> $stu

Name                                                                        Age
----                                                                        ---
Mosser                                                                       22

PS C:Powershell> $stu.ToString()
Name=Mosser;Age=22
</div>

使用COM对象

作为.NET的补充,Powershell可以加载和访问COM对象。

查看可用的COM对象

每一个COM对象都有存储在注册表中的唯一标识符,想遍历访问可用的COM对象,可是直接访问注册表。

Dir REGISTRY::HKEY_CLASSES_ROOTCLSID  -include PROGID -recurse | foreach {$_.GetValue("")}
DAO.DBEngine.36
DAO.PrivateDBEngine.36
DAO.TableDef.36
DAO.Field.36
DAO.Index.36
PS C:Powershell> Dir REGISTRY::HKEY_CLASSES_ROOTCLSID -include PROGID -recurse
| foreach {$_.GetValue("")} | select -First 10
DAO.DBEngine.36
DAO.PrivateDBEngine.36
DAO.TableDef.36
DAO.Field.36
DAO.Index.36
DAO.Group.36
DAO.User.36
DAO.QueryDef.36
DAO.Relation.36
file
......
</div>

怎样使用COM对象

一旦

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • Powershell 获取特定的网页信息的代码
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • 简单谈谈PowerShell 4.0中的新命令

相关文章

  • powershell解决win10开始菜单和通知中心无法打开
  • PowerShell中编程清空IE缓存方法
  • Powershell小技巧之使用Copy-Item添加程序到开机启动
  • PowerShell实现批量重命名文件
  • Shell实现程序造死循环的几种方法示例
  • PowerShell中的加法运算详解
  • Powershell实现克隆NTFS文件系统权限
  • PowerShell中按文件后缀过滤的实现代码
  • Powershell小技巧之屏蔽输出结果
  • PowerShell脚本开发之收发UDP消息包

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用PowerShell修改注册表
    • PowerShell函数中的开关参数介绍和创建实例
    • PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例
    • PowerShell显示隐藏文件和系统文件的方法
    • PowerShell检查网卡状态和对应的电源设置
    • PowerShell小技巧之从函数中返回多个值
    • PowerShell中读取多行文本示例
    • PowerShell中按修改时间查找文件的方法
    • Powershell读取本机注册表中的所有软件关联扩展名
    • Windows Powershell 命令集 cmdlets

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有