关于_使用

简短说明

允许你指示会话中使用的命名空间。

详细说明

using 语句允许您指定会话中使用的命名空间。 添加命名空间可简化 .NET 类和成员的使用,并允许您从脚本模块和程序集导入类。

using 语句必须位于脚本或模块中的其他任何语句之前。 没有未注释的语句可以位于它前面,包括参数。

using 语句不得包含任何变量。

using 语句不应与 using: 变量的 scope 修饰符混淆。 有关详细信息,请参阅 about_Remote_Variables

命名空间语法

要指定要从中解析类型的 .NET 命名空间,请执行以下作:

using namespace <.NET-namespace>

通过指定命名空间,可以更轻松地按其短名称引用类型。

模块语法

要从 PowerShell 模块加载类:

using module <module-name>

<module-name> 的值可以是模块名称、完整模块规范或模块文件的路径。

<module-name> 为路径时,路径可以完全限定或相对。 相对于包含 using 语句的脚本解析相对路径。

<module-name> 是名称或模块规范时,PowerShell 会在 PSModulePath 中搜索指定的模块。

模块规范是具有以下键的哈希表。

  • ModuleName - 必需 指定模块名称。
  • GUID - 可选 指定模块的 GUID。
  • 还需要指定以下三个键之一。 这些键不能一起使用。
    • ModuleVersion - 指定模块的最低可接受版本。
    • RequiredVersion - 指定模块的确切所需版本。
    • MaximumVersion - 指定模块的最大可接受版本。

using module 语句从脚本模块或二进制模块的根模块 ()ModuleToProcess 导入类。 它不会始终如一地导入嵌套模块中定义的类或脚本中定义的类,这些类是点源到模块中的。 您希望模块外部的用户可用的类应在 root 模块中定义。

在开发脚本模块期间,通常会对代码进行更改,然后使用 Import-Module 参数加载新版本的模块。 这仅适用于根模块中函数的更改。 Import-Module 不会重新加载任何嵌套模块。 此外,无法加载任何更新的类。

要确保您运行的是最新版本,您必须使用 Remove-Module cmdlet 卸载模块。 Remove-Module 删除根模块、所有嵌套模块以及模块中定义的任何类。 然后,你可以使用 using Import-Module 和 the using module 语句重新加载模块和类。

程序集语法

要从 .NET 程序集预加载类型:

using assembly <.NET-assembly-path>

加载程序集会在分析时将该程序集中的 .NET 类型预加载到脚本中。 这允许您创建使用预加载程序集中的类型的新 PowerShell 类。

如果不创建新的 PowerShell 类, Add-Type 请改用 cmdlet。 有关详细信息,请参阅 Add-Type

例子

示例 1 - 为 typename 解析添加命名空间

以下脚本获取“Hello World”字符串的加密哈希。

请注意 和 using namespace System.Textusing namespace System.IO 如何简化对 [UnicodeEncoding] in System.Text 和 in 以及 [Stream][MemoryStream] in System.IO的引用。

using namespace System.Text
using namespace System.IO

[string]$string = "Hello World"
## Valid values are "SHA1", "SHA256", "SHA384", "SHA512", "MD5"
[string]$algorithm = "SHA256"

[byte[]]$stringbytes = [UnicodeEncoding]::Unicode.GetBytes($string)

[Stream]$memorystream = [MemoryStream]::new($stringbytes)
$hashfromstream = Get-FileHash -InputStream $memorystream `
  -Algorithm $algorithm
$hashfromstream.Hash.ToString()

示例 2 - 从脚本模块加载类

在此示例中,我们有一个名为 CardGames 的 PowerShell 脚本模块,该模块定义以下类:

  • CardGames.Deck 卡牌
  • CardGames.Card 卡牌

Import-Module#requires 语句仅导入模块定义的模块函数、别名和变量。 不导入类。 using module 命令导入模块,并加载类定义。

using module CardGames
using namespace CardGames

[Deck]$deck = [Deck]::new()
$deck.Shuffle()
[Card[]]$hand1 = $deck.Deal(5)
[Card[]]$hand2 = $deck.Deal(5)
[Card[]]$hand3 = $deck.Deal(5)

示例 3 - 从程序集加载类

此示例加载程序集,以便其类可用于创建新的 PowerShell 类。 以下脚本创建一个派生自 DirectoryContext 类的新 PowerShell 类。

using assembly 'C:\Program Files\PowerShell\7\System.DirectoryServices.dll'
using namespace System.DirectoryServices.ActiveDirectory

class myDirectoryClass : System.DirectoryServices.ActiveDirectory.DirectoryContext
{

  [DirectoryContext]$domain

  myDirectoryClass([DirectoryContextType]$ctx) : base($ctx)
  {
    $this.domain = [DirectoryContext]::new([DirectoryContextType]$ctx)
  }

}

$myDomain = [myDirectoryClass]::new([DirectoryContextType]::Domain)
$myDomain
domain                                                    Name UserName ContextType
------                                                    ---- -------- -----------
System.DirectoryServices.ActiveDirectory.DirectoryContext                    Domain