如何解决Powersell删除子文件夹,其中名称不在SQL输出中
尝试删除名称不属于SQL输出的子文件夹。尝试了以下内容,但-exclude似乎未排除任何内容。我缺少明显的东西吗?
$SQL = Invoke-Sqlcmd -Query "SELECT id FROM files" -Database dbname -ServerInstance .\SQLEXPRESS
Get-ChildItem -Path C:\temp\files -Exclude $SQL | Remove-Item -Force -Recurse
解决方法
#Setup:
PS> get-childitem -path G:\Test\TestDelete\Music -recurse -directory
Directory: G:\Test\TestDelete\Music
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/26/2020 7:58 PM Bread
d----- 8/26/2020 7:58 PM Buddy Holly
d----- 8/26/2020 7:58 PM Cat Stevens
PS> $sql = "Bread","Cat Stevens"
#If your sql does not return an array it won't work!
PS> $sql -is [Array]
True
#Test of Command:
PS> get-childitem -path G:\Test\TestDelete\Music -recurse -directory -exclude $sql | Remove-Item -Force -Recurse
#Verification of results:
PS> get-childitem -path G:\Test\TestDelete\Music -recurse -directory
Directory: G:\Test\TestDelete\Music
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/26/2020 8:01 PM Bread
d----- 8/26/2020 8:01 PM Cat Stevens
注意:确保您的路径包含您不想删除的最后一个目录!
HTH
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。