在 Get-ChildItem PowerShell 语句中排除子文件夹
Exclude SubFolder in Get-ChildItem PowerShell Statement
我一直在尽最大努力更加熟悉 PowerShell,以此来自动执行我们的许多常规任务,从而努力消除人为错误。我支持基于 Web 的应用程序,我当前的目标是创建一个脚本以将应用程序从一台服务器迁移到另一台服务器。到目前为止,我已经获得了在新服务器上安装应用程序的脚本,从旧服务器复制配置文件,并在进行任何其他更改之前备份所有文件。我需要做的最后一件事是搜索配置目录和所有子文件夹以替换主机名、IP 地址和目录位置的所有实例(如果新服务器上的驱动器号发生变化)有一个文件夹名为 X:\Website\Tools\Database\Upgrade 的文件夹,其中包含客户曾经使用过的每个版本的文件夹,该文件夹中是数据库升级脚本。我无权更改此文件夹中的任何内容,坦率地说,我真的不想更改 X:\Website\tools 中的任何内容。
我将这部分脚本写成如下:
#Replace all instances of hostname, IP address, and drive letter
$path = "\$newip$newdriveletter$\Website"
$exclude = @("$path\tools\*")
$files = get-childitem $path\*.* -recurse -exclude $exclude
$files | %{
(gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace
"${olddriveletter}:\Website", "${newDriveLetter}:\Website" | set-content
$_.fullname
}
当我这样做时,它替换了我想要它替换的所有内容,但它仍然抛出以下错误:
gc : Access to the path
'\10.5.17.60\E$\Website\Tools\Database\Upgrade.2.0' is denied.
At C:\Folder\Powershell\Migrate.ps1:72 char:6
+ (gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -repl ...
+ ~~~~~
+ CategoryInfo : PermissionDenied:
(\10.5.17.60\E$...e\Upgrade.2.0:String) [Get-Content],
UnauthorizedAccessException
+ FullyQualifiedErrorId :
我还了解到 -exclude 参数在 PowerShell 中几乎已损坏,并且不能很好地排除文件夹。建议将其格式化为:
#Replace all instances of hostname, IP address, and drive letter
$path = "\$newip$newdriveletter$\Website"
$files = get-childitem $path\*.* -recurse | select-object -expandproperty
fullname | where-object{$_ -notlike "\tools\"}
$files | %{
(gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace "${olddriveletter}:\Website", "${newDriveLetter}:\Website" | set-content $_.fullname
}
不幸的是,当我 运行 这个时,我得到了错误:
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At C:\Folder\Powershell\Migrate.ps1:72 char:151
+ ... \WebAccess", "${newDriveLetter}:\Website" | set-content $_.fullname
所以基本上我已经实现了最终目标,但我真的很想在不弹出任何错误的情况下执行此操作。如果人们开始 运行ning 并看到红色,他们可能会惊慌失措。所以我认为必须有一种方法来排除甚至查看这些文件夹。可能是一个 If 语句,但我不太确定如何格式化它。有什么建议吗?
如果你只是 运行
get-childitem $path\*.* -recurse -exclude $exclude
您应该会看到它仍在返回您试图排除的文件夹。这是因为 get-childitem -exclude 不适用于容器。
改为尝试
$files = get-childitem $path\*.* -recurse | where-object{$_.fullname -notlike $exclude}
不确定 PS 版本是否存在差异(我使用的是 4).. 但我必须进行一些调整才能使其正常工作。这就是我进行测试的方式...
$oldhost = "RBABBITT1"
$oldip = "192.168.1.21"
$olddriveletter = "C"
$newhost = "RBABBITT2"
$newip = "192.168.1.22"
$newdriveletter = "D"
$path = "\$newip$newdriveletter$\test1"
$exclude = "$path\tools\*"
$files = get-childitem $path -Recurse | where-object{$_.fullname -notlike $exclude -and $_.PSIsContainer -eq $false}
$files | %{
(gc $_.FullName) -replace $oldhost, $newhost -replace $oldip, $newip -replace [regex]::Escape("${olddriveletter}:\Website"), "${newDriveLetter}:\Website" | set-content $_.fullname
}
我一直在尽最大努力更加熟悉 PowerShell,以此来自动执行我们的许多常规任务,从而努力消除人为错误。我支持基于 Web 的应用程序,我当前的目标是创建一个脚本以将应用程序从一台服务器迁移到另一台服务器。到目前为止,我已经获得了在新服务器上安装应用程序的脚本,从旧服务器复制配置文件,并在进行任何其他更改之前备份所有文件。我需要做的最后一件事是搜索配置目录和所有子文件夹以替换主机名、IP 地址和目录位置的所有实例(如果新服务器上的驱动器号发生变化)有一个文件夹名为 X:\Website\Tools\Database\Upgrade 的文件夹,其中包含客户曾经使用过的每个版本的文件夹,该文件夹中是数据库升级脚本。我无权更改此文件夹中的任何内容,坦率地说,我真的不想更改 X:\Website\tools 中的任何内容。
我将这部分脚本写成如下:
#Replace all instances of hostname, IP address, and drive letter
$path = "\$newip$newdriveletter$\Website"
$exclude = @("$path\tools\*")
$files = get-childitem $path\*.* -recurse -exclude $exclude
$files | %{
(gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace
"${olddriveletter}:\Website", "${newDriveLetter}:\Website" | set-content
$_.fullname
}
当我这样做时,它替换了我想要它替换的所有内容,但它仍然抛出以下错误:
gc : Access to the path
'\10.5.17.60\E$\Website\Tools\Database\Upgrade.2.0' is denied.
At C:\Folder\Powershell\Migrate.ps1:72 char:6
+ (gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -repl ...
+ ~~~~~
+ CategoryInfo : PermissionDenied:
(\10.5.17.60\E$...e\Upgrade.2.0:String) [Get-Content],
UnauthorizedAccessException
+ FullyQualifiedErrorId :
我还了解到 -exclude 参数在 PowerShell 中几乎已损坏,并且不能很好地排除文件夹。建议将其格式化为:
#Replace all instances of hostname, IP address, and drive letter
$path = "\$newip$newdriveletter$\Website"
$files = get-childitem $path\*.* -recurse | select-object -expandproperty
fullname | where-object{$_ -notlike "\tools\"}
$files | %{
(gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace "${olddriveletter}:\Website", "${newDriveLetter}:\Website" | set-content $_.fullname
}
不幸的是,当我 运行 这个时,我得到了错误:
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At C:\Folder\Powershell\Migrate.ps1:72 char:151
+ ... \WebAccess", "${newDriveLetter}:\Website" | set-content $_.fullname
所以基本上我已经实现了最终目标,但我真的很想在不弹出任何错误的情况下执行此操作。如果人们开始 运行ning 并看到红色,他们可能会惊慌失措。所以我认为必须有一种方法来排除甚至查看这些文件夹。可能是一个 If 语句,但我不太确定如何格式化它。有什么建议吗?
如果你只是 运行
get-childitem $path\*.* -recurse -exclude $exclude
您应该会看到它仍在返回您试图排除的文件夹。这是因为 get-childitem -exclude 不适用于容器。
改为尝试
$files = get-childitem $path\*.* -recurse | where-object{$_.fullname -notlike $exclude}
不确定 PS 版本是否存在差异(我使用的是 4).. 但我必须进行一些调整才能使其正常工作。这就是我进行测试的方式...
$oldhost = "RBABBITT1"
$oldip = "192.168.1.21"
$olddriveletter = "C"
$newhost = "RBABBITT2"
$newip = "192.168.1.22"
$newdriveletter = "D"
$path = "\$newip$newdriveletter$\test1"
$exclude = "$path\tools\*"
$files = get-childitem $path -Recurse | where-object{$_.fullname -notlike $exclude -and $_.PSIsContainer -eq $false}
$files | %{
(gc $_.FullName) -replace $oldhost, $newhost -replace $oldip, $newip -replace [regex]::Escape("${olddriveletter}:\Website"), "${newDriveLetter}:\Website" | set-content $_.fullname
}