Invoke-Formatter
根据输入设置或默认设置设置脚本文本的格式。
语法
__AllParameterSets
Invoke-Formatter
[-ScriptDefinition] <string>
[[-Settings] <Object>]
[[-Range] <int[]>]
[<CommonParameters>]
说明
Invoke-Formatter该命令小工具会根据定义的设置格式化字符串输入。 如果 没有设置 参数,该 cmdlet 将采用 Custom 中定义 Settings/CodeFormatting.psd1的默认代码格式设置。
示例
示例 1 - 使用默认设置设置设置输入脚本文本的格式
$scriptDefinition = @'
function foo {
"hello"
}
'@
Invoke-Formatter -ScriptDefinition $scriptDefinition
function foo {
"hello"
}
示例 2 - 使用哈希表中定义的设置设置格式化输入脚本
$scriptDefinition = @'
function foo {
"hello"
}
'@
$settings = @{
IncludeRules = @("PSPlaceOpenBrace", "PSUseConsistentIndentation")
Rules = @{
PSPlaceOpenBrace = @{
Enable = $true
OnSameLine = $false
}
PSUseConsistentIndentation = @{
Enable = $true
}
}
}
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
function foo
{
"hello"
}
示例3 - 使用“.psd1”文件中定义的设置格式化输入脚本文本
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings /path/to/settings.psd1
参数
-Range
应在其中设置格式的区域。 此参数的值必须是四个整数的数组。 这些数字必须大于 0。 四个整数按以下顺序表示以下四个值:
- 起始行号
- 起始列号
- 结束行号
- 结束列号
参数属性
| 类型: | Int32[] |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | 3 |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | True |
| 来自剩余参数的值: | False |
-ScriptDefinition
要格式化的脚本的文本表示为字符串。 这不是 ScriptBlock 对象。
参数属性
| 类型: | String |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | 1 |
| 必需: | True |
| 来自管道的值: | True |
| 来自管道的值(按属性名称): | True |
| 来自剩余参数的值: | False |
-Settings
设置哈希表或包含设置的 PowerShell 数据文件(.psd1)路径。
参数属性
| 类型: | Object |
| 默认值: | CodeFormatting |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | 2 |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | True |
| 来自剩余参数的值: | False |
CommonParameters
此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters。
输入
String
ScriptDefinition参数接受流水线中的字符串。
Object
设置参数接受流水线中的数值。
Int32
范围参数接受流水线的输入。
输出
String
格式化的字符串结果。