Good afternoon. There is a script that performs:
Can you tell me how to modify the script to take the computer name from the list, execute the script, then go to the next computer from the list?
#cleaning up errors
$Error.Clear()
#storage days - 10
$int = 10
#connecting a network drive from computer 1
New-PSDrive -Name "E" -PSProvider "FileSystem" -Root "\\Computer1\c`$\Folder" -Persist
#deleting files
FORFILES /p E:\ /s /m *.* /d -$int /c "CMD /c del /Q @FILE"
#disabling a network drive
Remove-PSDrive E
#recording information in the log
$date = Get-Date -Format dd-MM-yyyy
$LogfilePath = "C:\LOGS\Log_$date.txt"
(Get-Date) >> $LogfilePath
if ($Error) {
"No files found." | Add-content -path $LogfilePath -Force -Confirm:$false
}
Else {
"Files deleted." | Add-Content -path $LogfilePath -Force -Confirm:$false
}
# Define computers
$Computers = "Computer1","Computer2","Computer3","Computer4"
# Go through each computer
foreach ($Computer in $Computers){
#cleaning up errors
$Error.Clear()
#storage days - 10
$int = 10
#connecting a network drive from computer 1
New-PSDrive -Name "E" -PSProvider "FileSystem" -Root "\\$Computer\c`$\Folder" -Persist
#deleting files
FORFILES /p E:\ /s /m *.* /d -$int /c "CMD /c del /Q @FILE"
#disabling a network drive
Remove-PSDrive E
#recording information in the log
$date = Get-Date -Format dd-MM-yyyy
$LogfilePath = "C:\LOGS\Log_$date.txt"
(Get-Date) >> $LogfilePath
if ($Error) {
"No files found." | Add-content -path $LogfilePath -Force -Confirm:$false
}
Else {
"Files deleted." | Add-Content -path $LogfilePath -Force -Confirm:$false
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.