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?

This topic contains 9 replies, has 3 voices, and was last updated by Avatar BumbleBee 2 years, 9 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • Avatar
    BumbleBee
    Participant
    #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

    Avatar
    wullieb1
    Moderator
    #245552

    Have a look at get-content https://technet.microsoft.com/en-us/library/ee176843.aspx?f=255&MSPPError=-2147217396

    so something like this

    $serverlist = get-content

    foreach ($server in $serverlist)
    {
    write-host $server
    }

    http://www.admin-magazine.com/Articles/PowerShell-Part-3-Keeping-PowerShell-in-the-Loop

    Avatar
    BumbleBee
    Participant
    #366811

    Thank you very much

    Avatar
    BumbleBee
    Participant
    #366812

    Ok, so I’m running this script after modifying and receiving the following error:

    ForEach ($system in Get-Content “list.txt”)
    {

    $LogFolder=”\$systemD$LogsMyAppLogs”
    $Arcfolder=”\$systemD$MyAppLogs_backup”
    $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}

    }

    Error:

    \192.168.1.2D$LogsMyAppLogsMyAppLogsLog578.csv moved to \192.168.1.2D$MyAppLogs_backup.

    You cannot call a method on a null-valued expression.
    At D:scriptsNewBackupAndArchivetest.ps1:15 char:5
    + If ($WMIQuery.Compress()) {Write-Host “$FullName compressed successfully.”-Foreg …
    + ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    Can you help?

    Thanks

    Avatar
    wullieb1
    Moderator
    #245555

    What does this line return

    $WMIQuery = Get-WmiObject -Query “SELECT * FROM CIM_DataFile WHERE Name=’$WMIFileName’“

    DO a wrtie-host $WMIQuery and it will tell you

    Avatar
    BumbleBee
    Participant
    #366813

    Returns empty

    Avatar
    Ossian
    Moderator
    #191504

    Start working back through your code to confirm the variables are getting correctly populated

    Avatar
    wullieb1
    Moderator
    #245556

    ^^This^^

    Remove all the ancilliary code and get to a point where you know exactly what you are getting where.

    If i get a chance today i’ll try and knock something up, don’t hold me to it as i’ll be pretty busy.

    Avatar
    wullieb1
    Moderator
    #245557

    Right i’ve had a look at this inbetween work today.

    Couple of things.

    Can you give us a link to the actual source of the script? Where did you get it.

    Your if statement isn’t really needed IMHO.

    Does the original source call another function somehwere?

    I assume you are trying to archive a set of log files, compressing them at the same time. Why not simply use compress-archive? https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.archive/Compress-Archive

    Avatar
    BumbleBee
    Participant
    #366814

    Hi,

    Sorry for the late response. Here is the original source:

    https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Compress-Log-121e63b5

    Thanks

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.