A script to check a log file. If conditions are met the script will then delete, and copy in a file into the folder.
I wrote a quick script to automate a process I found myself doing every few months.
We have software that has a maximum number of licenses available. Unfortunately, when a user logs out, the software does not release that license. To solve this issue we simply delete a .log and .dat file, and then paste a old copy of the .dat file back into that location. Sounds kind of complicated, but it might make a little more sense looking at the script. Lets take a look:
The software will output a license count to a logfile that looks something like this.
$split = Import-Csv C:\Users\alexg\Desktop\Scripts\license.log -Delimiter " " -Header Name,Amount $Count = $split | Measure-Object 'Amount' -Sum | Select-Object -expandProperty Sum
So now that we have the log file imported and arranged in an array of objects, we can select the object we want to work with and work with it.
The second line of that snippet tells PowerShell to get the sum of the objects in the Amount column. I used the Measure-Object cmdlet, specified the object (Amount), and specified the measurement I wanted (in my case the Sum).
So now we have the number of licenses that are being used. We can now use that number in the rest of the script.
if($count -gt 50){ Remove-Item -Path C:\Users\alexg\Desktop\Scripts\software.dat Remove-Item -Path C:\Users\alexg\Desktop\Scripts\license.log Copy-Item -Path C:\Users\alexg\Desktop\net.dat -Destination C:\Users\alexg\Desktop\Scripts }
If the license count is greater than 50, the script will remove the .dat file and the .log file. It will then copy the old .dat file from a separate location and import it into the desired location. The final script looks like this:
$split = Import-Csv C:\Users\alexg\Desktop\Scripts\pcm_server.log -Delimiter " " -Header Name,Amount $Count = $split | Measure-Object 'Amount' -Sum | Select-Object -expandProperty Sum if($count -gt 50){ Remove-Item -Path C:\Users\alexg\Desktop\Scripts\software.dat Remove-Item -Path C:\Users\alexg\Desktop\Scripts\license.log Copy-Item -Path C:\Users\alexg\Desktop\net.dat -Destination C:\Users\alexg\Desktop\Scripts }
This is just a quick script that will prevent us from having to do this manually.
Comments
Post a Comment