Category Archives: Powershell

Powershell if file does not exist

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:

$FileToCheck="c:\windows\system32\notepad.exe"
## check file exists
if (!(Test-Path $FileToCheck))
{
     throw "$FileToCheck not found"
}

This will throw the exception if the file can not be found.

Batch converting video combining FFMpeg and Powershell

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.

Download a list of files from websites using Powershell

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.

WSUS Cleanup Server 2012

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.

Create a html file with images

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.

Batch Renaming Files in Powershell

I needed to rename a number of files, and include a number at the end.  eg:

Rename the following files:

IMG_20140725_143539.jpg
IMG_20140816_130503.jpg
IMG_20140816_130511.jpg
IMG_20140816_130513.jpg
IMG_20140818_143330.jpg
IMG_20140818_143331.jpg
IMG_20140818_143337.jpg
IMG_20140818_143357.jpg
IMG_20140818_143721.jpg
IMG_20140908_144527.jpg
IMG_20140908_144543.jpg
IMG_20140908_144549.jpg
IMG_20140909_070841.jpg
IMG_20140909_070849.jpg
IMG_20140909_111920.jpg
IMG_20140909_111927.jpg
IMG_20140909_122855.jpg
IMG_20140909_122910.jpg
IMG_20140909_122935.jpg
IMG_20140909_122945.jpg
IMG_20140909_122950.jpg
IMG_20140909_122953.jpg
IMG_20140909_123106.jpg
IMG_20140909_123115.jpg

 
to

stormpic01.jpg
stormpic02.jpg
stormpic03.jpg
stormpic04.jpg
stormpic05.jpg
stormpic06.jpg
stormpic07.jpg
stormpic08.jpg
stormpic09.jpg
stormpic10.jpg
stormpic11.jpg
stormpic12.jpg
stormpic13.jpg
stormpic14.jpg
stormpic15.jpg
stormpic16.jpg
stormpic17.jpg
stormpic18.jpg
stormpic19.jpg
stormpic20.jpg
stormpic21.jpg
stormpic22.jpg
stormpic23.jpg
stormpic24.jpg

I created a powershell script to do this, available here: http://pastebin.com/kTUyivgA

I used the command:

.renamefiles.ps1 -filepath c:temprenfile -renameto stormpic -leadingzeros $true
 

Generate List of RDS Logon and Logoff Events

A powershell script to list:

Date/Time , logon or logoff, Event ID, Username, SessionID, Source IPAddress, Computer user logged onto.
Eg:
2015-04-28T15:38:22,23,logoff,andrewst,6,,Server08
2015-04-28T15:36:37,23,logoff,sheffieldd,3,,Server10
2015-04-28T15:30:40,21,logon,ryank,4,172.16.0.122,Server08
2015-04-28T15:21:13,23,logoff,powelll,8,,Server09
2015-04-28T15:12:35,21,logon,sheffieldd,3,172.16.0.138,Server10
 You can get the script here:  http://pastebin.com/4vhqVCQE