相关疑难解决方法(0)

无法使用Powershell中的Get-ChildItem -Exclude参数排除目录

我正在使用Powershell v 2.0.并将文件和目录从一个位置复制到另一个位置.我使用字符串[]来过滤掉文件类型,还需要过滤掉被复制的目录.正在过滤掉文件,但是,我尝试过滤的目录obj仍在被复制.

$exclude = @('*.cs', '*.csproj', '*.pdb', 'obj')
    $items = Get-ChildItem $parentPath -Recurse -Exclude $exclude
    foreach($item in $items)
    {
        $target = Join-Path $destinationPath $item.FullName.Substring($parentPath.length)
        if( -not( $item.PSIsContainer -and (Test-Path($target))))
        {
            Copy-Item -Path $item.FullName -Destination $target
        }
    }
Run Code Online (Sandbox Code Playgroud)

我已经试过各种方法对其进行过滤,\obj*obj*\obj\ 但似乎没有任何工作.

谢谢你的帮助.

powershell powershell-2.0

24
推荐指数
3
解决办法
4万
查看次数

对于 PowerShell cmdlet,我可以始终将脚本块传递给字符串参数吗?

我正在查看 PowerShell 的Rename-Itemcmdlet的文档,有一个这样的例子。

Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace '\.txt','.log' }
Run Code Online (Sandbox Code Playgroud)

此示例说明如何使用 Replace 运算符重命名多个文件,即使 NewName 参数不接受通配符也是如此。

此命令将当前目录中的所有 .txt 文件重命名为 .log。

该命令使用 Get-ChildItem cmdlet 获取当前文件夹中具有 .txt 文件扩展名的所有文件。然后,它使用管道运算符 (|) 将这些文件发送到 Rename-Item 。

NewName 的值是在将值提交给 NewName 参数之前运行的脚本块。

注意最后一句话:

NewName 的值是在将值提交给 NewName 参数之前运行的脚本块

实际上NewName是一个字符串:

[-NewName] <String>
Run Code Online (Sandbox Code Playgroud)

那么这是否意味着当所需的参数类型是字符串时,我总是可以使用脚本块?

parameters powershell cmdlets scriptblock delay-bind

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

Powershell 参数绑定 ByPropertyName 和 ByValue

我想停止名为“ALG”的服务,所以我使用:"alg" | stop-service

有用。

Get-help stop-service -parameter name说:

Pipeline input:true(ByPropertyName, ByValue)而“alg”是“ByPropertyValue”,对吗?

我想停止名为记事本的进程,因此我使用:"notepad" | stop-process并且收到错误。

Get-help stop-process -parameter name

说:Pipeline input true(ByPropertyName)“记事本”是“ByPropertyName”?

为什么会出现这个错误?

parameters powershell

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