gpt4 book ai didi

powershell - 如何在 PowerShell 中继续脚本 block 的行?

转载 作者:行者123 更新时间:2023-12-03 12:35:46 25 4
gpt4 key购买 nike

我正在尝试编写一个 Powershell 函数来创建一个 OrganizationalUnit。我是一个 PS 新手,但我拼凑了:

function makeOU ($cn, $path)
{
$sb = [scriptblock]::Create(
"New-ADOrganizationalUnit $cn -path `"$path`"
-ProtectedFromAccidentalDeletion 0"
)

Invoke-Command -ComputerName $server -Credential $Credential `
-ScriptBlock $sb
}

但是当我稍后在脚本中调用它时,我收到一条消息,指出 -ProtectedFromAccidentalDeletion 是一个未知的 cmdlet。如果我将命令设为一行

"New-ADOrganizationalUnit $cn -path `"$path`" -ProtectedFromAccidentalDeletion 0"

有效。

如我所见,在

      "New-ADOrganizationalUnit $cn -path `"$path`"

有一个左括号和一个左引号,所以 PS 应该寻找更多的输入。以反勾结束该行没有帮助。也没有将 Create() 的参数转换为 @"... "@ 的形式。 (这与 Why I am getting PowerShell Parsing Error? 不同,因为我没有任何反引号,后面当然没有空格。)

如果我在这里犯了一个新手错误,并且有更好的方法将函数参数传递给 Invoke-Command,我愿意重写但失败了,如何打破传递的字符串将 Create() 放到多行上?

最佳答案

Chris Nelson: Your "if you really want to" makes it sound like I'm doing something grossly anti-idomatic to PS. I'm perfectly willing to believe that so I wonder how you'd write it.

事实上,反引号在 Posh community 中是不受欢迎的:

In general, the community feels you should avoid using those backticks as "line continuation characters" when possible. They're hard to read, easy to miss, and easy to mistype. Also, if you add an extra whitespace after the backtick in the above example, then the command won't work. The resulting error is hard to correlate to the actual problem, making debugging the issue harder.

The preferred way to avoid long lines is to use splatting (see About_Splatting) and PowerShell's implied line continuation inside parentheses, brackets, and braces -- these should always be used in preference to the backtick for line continuation when applicable, even for strings

既然你问了,我会怎么写,这里有一些例子:

“结构化”

function makeOU ($cn, $path)
{
$Template = 'New-ADOrganizationalUnit {0} -Path "{1}" -ProtectedFromAccidentalDeletion 0'
$ScriptBlock = [scriptblock]::Create(($Template -f $cn, $path))

Invoke-Command -ComputerName $server -Credential $Credential -ScriptBlock $ScriptBlock
}

“溅”

function makeOU ($cn, $path)
{
$Template = 'New-ADOrganizationalUnit {0} -Path "{1}" -ProtectedFromAccidentalDeletion 0'
$ScriptBlock = $Template -f $cn, $path

$Splat = @{
ComputerName = $server
Credential = $Credential
ScriptBlock = [scriptblock]::Create($ScriptBlock)
}

Invoke-Command @Splat
}

“单线”

function makeOU ($cn, $path)
{
Invoke-Command -ComputerName $server -Credential $Credential -ScriptBlock (
[scriptblock]::Create(
('New-ADOrganizationalUnit {0} -Path "{1}" -ProtectedFromAccidentalDeletion 0' -f $cn, $path)
)
)
}

# Or, using parentheses:

function makeOU ($cn, $path)
{
Invoke-Command -ComputerName (
$server
) -Credential (
$Credential
) -ScriptBlock (
[scriptblock]::Create(
('New-ADOrganizationalUnit {0} -Path "{1}" -ProtectedFromAccidentalDeletion 0' -f $cn, $path)
)
)
}

关于powershell - 如何在 PowerShell 中继续脚本 block 的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com