To find the full filename and path of a running script use:
Just the filename
split-path $MyInvocation.MyCommand.Definition –Leaf
Just the path
split-path $MyInvocation.MyCommand.Definition -Parent
I needed a script to parse a directory full of zip files, extract them, and then delete the source zip file.
I used powershell and the script can be found here: https://pastebin.com/mLizttME
Using Powershell, we can count the number of files by filetype, using the following command:
get-childitem -recurse | select -expandproperty extension | group | select name,count | sort count –desc
I had a server with a system drive low on disk space. Manually clearing the diagnostic log files resolved the issue, but only temporarily. I needed to clear the log files on a schedule until the disk could be expanded. This script was used as a scheduled task.
Small gotcha that caught me recently, I needed to do a test-path but action if a file did NOT exist, and I had trouble getting it to work first go, so I’m writing this here:
## check file exists
if (!(Test-Path $FileToCheck))
throw "$FileToCheck not found"
This will throw the exception if the file can not be found.
I had a bunch of MKV files that I needed to convert to mp4. I don’t have any fancy video editing software, and found that a program called ffmpeg exists, which is a command line tool that will do this. Once I verified that it would do what I wanted, I decided to write a powershell script that will do the work for me, which you can find here.
You will need to download and install ffmpeg and run the script. I have defaulted ffmpeg to “c:\program files\ffmpeg\ffmpeg.exe” as this seems like the logical place for me.
If you have any comments, or ideas on how to improve, please let me know in the comments below.
I saw a great article about a 1950s Science Fiction magazine, now available for download (http://arstechnica.com/the-multiverse/2016/02/you-can-now-read-the-entirety-of-sci-fi-magazine-if-for-free/).
In the comments, someone had collated a list of URLs to provide to a download manager. Since I didn’t have one, and I knew that Powershell had a way to download a file from a URL, I decided to create my own.
The script available here: http://pastebin.com/7NDkzEJN will allow you to provide a text file containing a list of URL’s and download the file.
I’ve inherited a customer with a badly running WSUS server. It is running on Server 2012 with SQL 2012. Maintenance had never been run, and attempts to do so failed with the cleanup wizard timing out.
I’ve created a powershell script, available here: http://pastebin.com/u2yuexXf that I have been able to use to clean it up. It also requires the WSUSDBMaintenace.sql file from here, and SQL Management Studio if not already installed, available here.
The script performs the following:
Run the cleanup wizard;
Reindex the database;
Decline Itanium updates;
Shrink the database;
Optionally send an email.
I needed to write a string to the start of each line in a text file. I used Powershell to achieve this. You can see the code here: http://pastebin.com/vg2SDrqX
I needed to create a quick and dirty HTML file that had 2 columns. 1 column was the website path to the image, the 2nd column was the image itself. This would need to be created dynamically.
I have powershell code on pastebin.com here: http://pastebin.com/YXZqg1yb.
The following command:
.CreateHTMLWithImages.ps1 -website http://www.anonit.net/images/2016/1/3/ -sourcepath \web01blogimages201613 -HTMLFile \web01blog20160103.html -YSize 100 -XSize 100
Will produce a HTML file similar to this.