当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

作者:精品下载站 日期:2024-12-14 04:52:03 浏览:13 分类:玩电脑

PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?


[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

此处介绍 PowerShell 字符串

PowerShell Here-String 是在封装内插入多行字符串或命令的方法,也称为字符串数组,用自由文本和字符串块表示数据的最佳方式是用 @ ” “@ 带单引号或双引号,可以执行文本、表达式、子表达式,并创建不同格式的文件。

语法

这里的PowerShell-String用括弧@”“@,来表示,有些人也将其称为PowerShell String数组。

PowerShell“此处字符串”如何工作?

这里的双引号代表Powershell——字符串@ ” ”@ 或者单引号@' '@, 但是两者并不相同;当涉及到变量或表达式的扩展时,它们有显着的差异。

首先,让我们简单地尝试一下我们的多行字符串外壳来检查它是如何工作的。

@"
This is a string block with multiple lines.
This is the second line
This is the third line
"@

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

我们前面已经提到过;这是自由文本,因此您可以将字符串放置在块内的任何位置,它将按照提到的方式显示。

@"
This is the first line
This is the second line
This is the third line
"@

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

当您声明此处字符串块@”“@时,空格是一个非常重要的因素。您可以在语法开头提供空格 @ ”,但不能在语法末尾提供空格 “@ ,它被视为无效。请参阅下面的示例,

@"
This is the first line
This is the second line
This is the third line
"@

结束块“@包含空格,输出将产生错误。

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

这里带有单引号的字符串类似于编写文本时的双引号括起来。我们也可以将上面的字符串输入写成如下所示。

@'
This is the first line
This is the second line
This is the third line
'@

我们还可以将此处的字符串存储到变量中。

$str = @"
This is the first line
This is the second line
This is the third line
"@
$str

单引号和双引号此处字符串之间的显着区别在于我们在其中使用变量的时间。这里的单引号-string不能扩展变量的名称。

$val = 10
@"
This is a string
The value is : $val
"@

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

使用单引号括起来时。

$val = 10
@'
This is a string
The value is : $val
'@

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

可以看到单引号括起来不能扩展变量名。同样,当扩展变量单引号here-string命令时也无法扩展它,如下所示。

@'
This is a string
Today's date is : $(Get-Date)
Total: $(4 + 5 + 6)
'@

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

当我们在这里使用双引号时,字符串。

@"
This is a string
Today's date is : $(Get-Date)
Total: $(4 + 5 + 6)
"@

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

here-String方法不仅限于显示字符串的多行或变量或表达式的扩展,我们还可以使用其数据创建CSV、JSON、HTML等文件,然后将它们转换为各自的格式。文件格式,它们如下面的示例所示。

这里是 PowerShell 字符串的示例

下面给出了 PowerShell 此处字符串的示例:

示例 #1 - 使用 Here-String 创建 CSV 文件。

要与数据一起创建 CSV 文件格式,我们可以使用 Here-string 方法,如下所示。

$csv = @"
Name, EmpID, City, Role
Jack, 1001, Atlanta, Permanent
Thomas, 1002, Sweden, Contractor
Lisa, 1003, London, Permanent
"@
$csv | ConvertFrom-Csv

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

要将输出存储为 CSV,您可以使用以下命令。

$csv | 从 Csv 转换 |导出 CSV C:\temp\empdata.csv -NoTypeInformation

示例 #2 - 使用 Here-String 命令创建 JSON 文件。

使用 Here-String 创建 JSON 文件。

@"
{
"Type": "Fruits",
"Fruits": [
"Apple",
"Grapes",
"Mango"
],
"Country": [
"India",
"US"
]
}
"@ | ConvertFrom-Json

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

示例 #3 - 使用 Here-String 创建哈希表。

我们可以转换的另一种数据类型是哈希表。要使用 Here-String 命令创建哈希表,请使用以下方法。

$hash = @"
Name = Jackson
Employer = BigData
EmpID = 2032
Type = Permanent
"@
$hash | ConvertFrom-StringData

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

示例 #4 - 使用此处字符串创建 HTML 文件。

我们可以使用 PowerShell here-string 命令创建一个 HTML 文件,如下所示。

$html = @'
<!DOCTYPE html>
<html>
<head>
<title> PowerShell HTML Page </title>
</head>
<body>
<h1> This is a Heading </h1>
<p> This is a paragraph </p>
</body>
</html>
'@
$html | Out-File C:\Temp\testhtml.html

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

示例 #5 - 使用 Here-String 创建新的 PowerShell 函数。

Here-String 命令非常有用,您甚至可以创建 PowerShell 函数并将其存储在新文件中。如下图所示。

@'
function PrintName{
param(
[Parameter(Mandatory=$true)]
[String]$Name
)
Write-Output "Here-String.. Print Name: $Name"
}
'@ | Out-File C:\Temp\PrintName.ps1

我们在这里使用单引号字符串是因为我们不想打印 $Name 变量,但我们需要将其存储到文件中。如果直接存储文件不带函数的话,可以直接从命令行传递参数,如下图。

@'
param(
[Parameter(Mandatory=$true)]
[String]$Name
)
Write-Output "Here-String.. Print Name: $Name"
'@ | Out-File C:\Temp\PrintName.ps1

输出:

[玩转系统] PowerShell 此处字符串 |了解 PowerShell Here 字符串是如何工作的?

有如此多的示例,您几乎可以使用here-string命令创建任何类型的文件、函数、表达式,这种性质使得here-string更加强大。不仅在 PowerShell 中,其他编程语言也因其灵活性而使用 here-string 命令。

结论

PowerShell here-string 是编写多行字符串的最有效方法之一,包括在封装内扩展变量、表达式、子表达式。我们还可以使用here-string创建CSV、JSON、HTML等结构,然后letter将它们转换为各自的格式。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯