Я получаю ошибки при запуске скрипта.Он получит доступ к диску и запишет имена файлов в мой журнал, как указано, но не удалит файлы на общем диске из-за ошибки разрешений.Я вошел в систему как администратор и переписал разрешения для всего диска, но по-прежнему не имею доступа.
Вот код:
$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}'