Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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

  • #2
    Have a look at get-content https://technet.microsoft.com/en-us/...or=-2147217396

    so something like this

    $serverlist = get-content <mytextfilewithservers>

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

    http://www.admin-magazine.com/Articl...ll-in-the-Loop

    Comment


    • #3
      Thank you very much

      Comment


      • #4
        Ok, so I'm running this script after modifying and receiving the following error:

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


        $LogFolder="\\$system\D$\Logs\MyAppLogs”
        $Arcfolder="\\$system\D$\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.2\D$\Logs\MyAppLogs\MyAppLogsLog578.cs v moved to \\192.168.1.2\D$\MyAppLogs_backup.

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



        Can you help?

        Thanks

        Comment


        • #5
          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

          Comment


          • #6
            Returns empty

            Comment


            • #7
              Start working back through your code to confirm the variables are getting correctly populated
              Tom Jones
              MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
              PhD, MSc, FIAP, MIITT
              IT Trainer / Consultant
              Ossian Ltd
              Scotland

              ** Remember to give credit where credit is due and leave reputation points where appropriate **

              Comment


              • #8
                ^^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.

                Comment


                • #9
                  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/powershel...mpress-Archive

                  Comment


                  • #10
                    Hi,

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

                    https://gallery.technet.microsoft.co...s-Log-121e63b5

                    Thanks

                    Comment

                    Working...
                    X