我在我的PowerShell可执行文件中有一个从PHP脚本返回的数组值列表。这些值对应于我的Windows Server上的活动项目。我的C:/驱动器中有一个projects文件夹,该文件夹为该服务器处理的每个项目都有一个子文件夹。该结构如下所示:
/project-files
/1
/2
/3
/4上述信号表明,到目前为止,服务器已经处理了四个项目。
我运行一个计划好的Task脚本,它每天清理project-files文件夹。当我运行我的脚本时,我只想删除与当前不在服务器上运行的项目对应的子文件夹。
我有以下Powershell:
$active_projects = php c:/path/to/php/script/active_projects.php
if($active_projects -ne "No active projects"){
# Convert the returned value from JSON to an Powershell array
$active_projects = $active_projects | ConvertFrom-Json
# Delete sub folders from projects folder
Get-ChildItem -Path "c:\project-files\ -Recurse -Force |
Select -ExpandProperty FullName |
Where {$_ -notlike 'C:\project-files\every value in $active_projects*'}
Remove-Item -Force
}如果子文件夹号与project-files数组中的项目号相对应,我希望不删除$active_projects文件夹中的子文件夹。
我将如何在这里编写Where语句?
发布于 2016-04-07 19:05:43
您应该使用-notcontains操作符来查看每个项目是否被列为活动项目。在下面的文章中,我假设PHP脚本中的JSON字符串返回一个字符串列表。
$active_projects = php c:/path/to/php/script/active_projects.php
if ($active_projects -ne "No active projects") {
# Convert the returned value from JSON to a PowerShell array
$active_projects = $active_projects | ConvertFrom-Json
# Go through each project folder
foreach ($project in Get-ChildItem C:\project-files) {
# Test if the current project isn't in the list of active projects
if ($active_projects -notcontains $project) {
# Remove the project since it wasn't listed as an active project
Remove-Item -Recurse -Force $project
}
}
}但是,如果您的JSON数组是一个整数列表,那么测试行应该是:
if ($active_projects -notcontains ([int] $project.Name)) {https://stackoverflow.com/questions/36484523
复制相似问题