How to add multiple servers from a txt list to a powershell maintenance script?

Home Forums Scripting PowerShell How to add multiple servers from a txt list to a powershell maintenance script?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    BumbleBee
    Member
    #166784

    Hi,

    I have this PS maintenance script that I want to use in order to compress and archive old logs.
    I need it to run from one server and use a list of multiple server IPs that it will run on.
    The list of server IPs will be given in a plain txt file.

    Can you help how to make this happen?

    < #
    .SYNOPSIS
    Compresses and moves log files older than 7 days from specified folder to another.
    .DESCRIPTION
    If files older than 7 days are found in a given folder, the script will use NTFS compression and move them to archive folder.
    .NOTES
    File Name : compress-logs.ps1
    Author: Nikolay Petkov
    http://power-shell.com/
    #>
    $LogFolder=“Z:Logs”
    $Arcfolder=”Z:Logbackup”
    $LastWrite=(get-date).AddDays(-7).ToString(“MM/dd/yyyy”)
    If ($Logs = get-childitem $LogFolder | Where-Object {$_.LastWriteTime -le $LastWrite -and !($_.PSIsContainer)} | sort-object LastWriteTime)
    {
    foreach ($L in $Logs)
    {
    $FullName=$L.FullName
    $WMIFileName= $FullName.Replace(“”, “”)
    $WMIQuery = Get-WmiObject -Query “SELECT * FROM CIM_DataFile WHERE Name=’$WMIFileName’“
    If ($WMIQuery.Compress()) {Write-Host “$FullName compressed successfully.”-ForegroundColor Green}
    else {Write-Host “$FullName was not compressed.” -ForegroundColor Red}
    If (Move-Item $FullName $Arcfolder -PassThru) {Write-Host “$FullName moved to $Arcfolder.” -ForegroundColor Green}
    else {Write-Host “$FullName was not moved to $Arcfolder.” -ForegroundColor Red}
    }}
    else {Write-Host “Found no log files older than 7 days.” -ForegroundColor Green}

    Thanks

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.