简体   繁体   中英

Powershell. Using information from the xls file in the script code

Good afternoon. There is a script that performs:

  1. Clearing error logs,
  2. Connecting a.network drive,
  3. Deleting information from a disk older than 10 days,
  4. Disconnecting the.network drive,
  5. Recording information in the log.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM