0

运行脚本时我一直收到错误消息。它将访问驱动器并将文件名写入我指定的日志,但由于权限错误,不会删除共享驱动器上的文件。我以管理员身份登录并重新写入了整个驱动器的权限,但仍然没有访问权限。

这是代码:

$servers = Get-content "D:\Scripts\servers.txt"
$logpath = "d$\Documents"
$logFile = "D:\Scripts\log.txt"
$Date = Get-Date
$error.clear()

foreach($server in $servers){

$fail =" $Date - Failed.  $server did not respond to a ping command.  No files have been deletedfrom $server."
$notpresent = "$Date - Failed.  The specified log folder doesn't seem to be present on $server."
$nologs = "There are no log files to be deleted on $server."

$UNCpath = "\\" +$server+ "\" +$logpath
#Ping server to validate the server is online and accessible
If ($server|?{ (gwmi Win32_PingStatus -Filter "Address='$_'").StatusCode -eq 0 }){

    # The server IS pingable, but the folder does not exist.
    If (!(Test-Path $UNCpath)){Write-output $notpresent `r`n | Out-File $logFile -append}

    # The server IS pingable, and the folder does exist
    else{
        $files = (dir -r $UNCpath -include *.doc,*.pdf,*.docx,*.xls,*.xlsx | where {$_.lastwritetime -le (get-date).adddays(-90) -and !$_.psiscontainer})
        foreach ($file in $files){
            If ($file -eq $null){write-output $nologs `r`n | Out-File $logFile -append}
            Else{
                write-output $file.name | Out-File $logFile -append
                remove-item $file
            }
        }
    }
}

# The server is NOT pingable.
else{Write-output $fail `r`n | Out-File $logFile -append}

If ($error.count -ne "0"){write-output $error | Out-File $logfile -append}
Else{write-output "The log file removal script has completed successfully." | Out-File $logfile -append}'
4

1 回答 1

2

OP 发表了他自己的答案作为评论。我把它作为一个“真实”的答案放在这里。我的粗体/斜体强调。

在机器 A 上运行脚本以访问机器 B 上的文件。我找到了解决方案——我只是在 remove-item $file 之后添加了 -force。 像魅力一样工作。– gp80586 8 月 10 日 20:48

请不要对这个答案投票,这不是我的。

于 2011-11-17T07:46:24.267 回答