小编Chr*_*son的帖子

如何从嵌套管道访问更高级别的$ _管道变量?

我正在编写一个脚本,它将查看父VHD文件的目录,然后评估哪些VM正在使用这些父VHD.

我有它的机制工作,但我遇到一个问题,我真的需要从嵌套管道的上下文中引用自动管道变量($ _)

sudo代码将是这样的:

For each File in Files
Iterate over all VMs that have differencing disks
and return all the VMs that have a disk whose parent disk is File
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止实现的实际powershell代码:

$NAVParentFiles = get-childitem '\\hypervc2n2\c$\ClusterStorage\Volume1\ParentVHDs' | where {$_.Name -notLike "*diff*"} | select name
$NAVParentFiles | % { Get-VM | where {$_.VirtualHardDisks | where {$_.VHDType -eq "Differencing" -and ($_.ParentDisk.Location | split-path -leaf) -like <$_ from the outer for each loop goes here> } } 
Run Code Online (Sandbox Code Playgroud)

感谢您提供有关如何从嵌套管道优雅地访问外部管道变量的任何帮助.

powershell

18
推荐指数
1
解决办法
2万
查看次数

我应该以什么顺序在C#类中放置属性,事件,函数,函数覆盖等?

在创建新的C#类时,我不确定用于声明属性,事件委托,函数,函数覆盖等的最佳逻辑顺序是什么,以及在决定该顺序时应该考虑哪些因素.

通常在创建WebUserControl类后面的代码时,我最终会按以下顺序放置内容:

  1. 活动
  2. 属性
  3. 生命周期事件覆盖功能
  4. 其他功能

有没有更合理的方法来做到这一点,在决定如何在类文件中订购类的这些元素时,我应该考虑哪些因素?

c# readability

4
推荐指数
1
解决办法
378
查看次数

我可以在 PowerShell 中为参数指定条件默认值吗?

我想如果这是可能的,它可能会使用参数集工作,所以我尝试了以下操作:

Function New-TestMultipleDefaultValues {
    [CmdletBinding(DefaultParameterSetName="Default1")]
    param (
        [Parameter(Mandatory,ParameterSetName="Default1")]$SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1,
        [Parameter(ParameterSetName="Default1")]$Test1 = "Value1",
        [Parameter(ParameterSetName="Default2")]$Test1 = "Value2"
    )
    $PSBoundParameters
}
Run Code Online (Sandbox Code Playgroud)

执行此操作以创建函数会导致错误,Duplicate parameter $test1 in parameter list.因此这种方式看起来不是一种选择。

在这一点上,我唯一能想到的就是做这样的事情:

Function New-TestMultipleDefaultValues {
    param (
        $SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1,
        $Test1
    )
    if (-not $Test1 -and $SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1) {
        $Test1 = "Value1"
    } elseif (-not $Test1 -and -not $SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1) {
        $Test1 = "Value2"
    }

    $Test1
}
Run Code Online (Sandbox Code Playgroud)

哪个有效但看起来很丑:

PS C:\Users\user> New-TestMultipleDefaultValues -SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1 "thing"
Value1
PS C:\Users\user> New-TestMultipleDefaultValues
Value2
PS C:\Users\user> New-TestMultipleDefaultValues -Test1 "test"
test
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来实现这一点?

parameters powershell default-value

4
推荐指数
1
解决办法
1188
查看次数

如何使用 Invoke-WebRequest 的 -OutFile 参数转义文件路径中的方括号

当您[1]在文件名中包含类似File[1].txtInvoke-WebRequest-OutFile参数一起使用的内容时,您会收到错误消息Cannot perform operation because the wildcard path File[1].txt did not resolve to a file.

这是由此处记录的行为引起的。

对于其他 cmdlet,您可以-LiteralPath用来强制按字面意思采用路径,但在这种情况下,这不是一个选项。

我试过用 ` or转义[]字符,\但它仍然给出同样的错误。

为了简化测试时,可以重现相同的问题有Out-FileTest-Path等等。

#Fails
Out-File -FilePath "file[1].txt"
Out-File -FilePath "file`[1`].txt"
Out-File -FilePath "file\[1\].txt"

#Succeeds
Out-File -LiteralPath "file[1].txt"

#Fails
Test-Path -Path "file[1].txt"

#Succeeds
Test-Path -LiteralPath "file[1].txt"
Run Code Online (Sandbox Code Playgroud)

我怎样才能逃避将被用来表示在通配符的字符-Path-FilePath-OutFile,等,让它们的功能就像是指定的字符串-LiteralPath,因为-LiteralPath是不可用Invoke-WebRequest

powershell glob escaping

4
推荐指数
1
解决办法
2910
查看次数

迭代在这里的字符串中的子表达式导致额外的空格,为什么?

问题

在PowerShell版本5.1.15063.726中运行以下命令

@"
$(foreach ($Number in 1..4) {"$Number`r`n"})
"@
Run Code Online (Sandbox Code Playgroud)

结果是

1
 2
 3
 4
Run Code Online (Sandbox Code Playgroud)

在每行的新行之后似乎添加了尾随空格.

Expirments

我想也许这是一个怪癖

`r`n
Run Code Online (Sandbox Code Playgroud)

但使用[Environment]::NewLine具有相同的问题:

@"
$(foreach ($Number in 1..4) {"$Number" + [Environment]::NewLine})
"@

1
 2
 3
 4
Run Code Online (Sandbox Code Playgroud)

在没有迭代的情况下做一些看似平等的事情按预期工作:

@"
$("1`r`n" + "2`r`n" + "3`r`n" + "4`r`n")
"@
1
2
3
4
Run Code Online (Sandbox Code Playgroud)

以下也有效:

@"
$("1" + [Environment]::NewLine)$("2" + [Environment]::NewLine)$("3" + [Environment]::NewLine)$("4" + [Environment]::NewLine)
"@

@"
$("1`r`n")$("2`r`n")$("3`r`n")$("4`r`n")
"@
Run Code Online (Sandbox Code Playgroud)

以下不起作用,导致新行后的奇数额外空格:

@"
$(1..4 | ForEach-Object {"$_`r`n"})
"@

@"
$(1..4 | ForEach-Object {$("$_`r`n")})
"@
Run Code Online (Sandbox Code Playgroud)

解决方法

想想也许这个问题与在循环中返回多个字符串有关,我试图构建一个字符串,然后返回那个有效的循环形式:

$String = …
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
1
解决办法
114
查看次数