【问题标题】:Files not getting deleted from remote servers文件未从远程服务器中删除
【发布时间】:2015-01-25 03:54:34
【问题描述】:

我使用此论坛上的帖子编写了以下脚本。该脚本会删除超过 15 天的文件:

cls
$servers = Get-Content server.txt
$limit = (Get-Date).AddDays(-15)
$path = "E:\CheckDBOutput"

ForEach ($line in $servers)
{
  Invoke-Command -cn $line -ScriptBlock {
    Get-ChildItem -Path $path -Recurse -Force | Where-Object {
      !$_.PSIsContainer -and $_.CreationTime -lt $limit
    } | Remove-Item -Force
  }
}

脚本执行正常,但没有文件被删除。

【问题讨论】:

标签: powershell


【解决方案1】:

正如在 cmets 中已经提到的,您需要将要在脚本块内使用的参数作为参数传递给 -ArgumentList 参数:

$RemoveOldFiles = {
  param(
    [string]$path,
    [datetime]$limit
  )

  Get-ChildItem -Path $path -Recurse -Force | Where-Object {
    !$_.PSIsContainer -and $_.CreationTime -lt $limit
    } | Remove-Item -Force
}

$servers = Get-Content server.txt
$limit = (Get-Date).AddDays(-15)
$path = "E:\CheckDBOutput"

ForEach ($line in $servers)
{
  Invoke-Command -cn $line -ScriptBlock $RemoveOldFiles -ArgumentList $path,$limit
}

【讨论】:

    【解决方案2】:

    PowerShell 1.0 版中不存在删除项目 - 确保您的目标服务器已安装 v2.0 或更高版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多