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 に答える