Thursday, May 14, 2015

Open Socket using PowerShell

To open a socket connection on a specific host and port using PowerShell do the following:

(new-object Net.Sockets.TcpClient).Connect("", "8080")

If it can't connect it will timeout. If it can connect it will return to the Powershell prompt.

wget equivalent in PowerShell

As some have pointed out there is not the equivalent for wget in PowerShell 1 or 2. It is included as Invoke-WebRequest in PowerShell 3.

To implement it yourself in PowerShell 1 or 2, you can use the following:

## Declare a function that takes source and destination arguments
Function Get-Webclient ($url, $out) {
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$request = New-Object System.Net.WebCLient
$request.UseDefaultCredentials = $true ## Proxy credentials only
$request.Proxy.Credentials = $request.Credentials
$request.DownloadFile($url, $out)

## Call the function with our source and destination.
Get-Webclient "" "C:\Foo3.txt"

Credit to this post.

Tuesday, May 12, 2015

Merge multiple MP3 files into one file

It is not a simple thing to merge MP3 files because they have header information at the beginning of each file before the actual content. So, in theory you can't just merge them together without writing a single header for the new merged file. However, many MP3 players will still read the files okay even though there are multiple headers (all but one mixed where the content should be). This hack exploits this robust implementation of MP3 players.

WARNING: This is most definitely a hack and does not always work because it is technically not correct. However, in a pinch it can work (sometimes). Do not trash your original files unless you don't mind losing the data.

Here is the very simple DOS command for merging multiple MP3 files into one MP3 file (that will likely play on most MP3 players, but is technically not correctly formatted so it will not play on all MP3 players).

copy /b *.mp3 output.mp3

Monday, May 11, 2015

Using Powershell to email free space

Below are two script

Put the following into a PowerShell file (EmailFreeSpace.ps1) and execute.

$Body = Get-WmiObject Win32_logicaldisk -ComputerName LocalHost `
| Format-Table DeviceID, MediaType, `
@{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
@{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
@{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `

$EmailFrom = ""
$EmailTo = ""
$Subject = "Notification from XYZ"  

$SMTPServer = ""
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("", "password here");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

The output of the first part of the script will look like this:

DeviceID MediaType Size(GB) Free Space(GB) Free (%)
-------- --------- -------- -------------- --------
C:              12      232            174   75 %
D:              12      233            155   67 %
H:               0     7500           5634   75 %
J:               0      100            100  100 %
K:              11        0              0
Q:              12        0              0

The second part of the script will send an email using Gmail. You need to have a Gmail account and you will need to put the password on the second to last line of the script. You can also use any other STMP server such as Hotmail, etc.