1

I'll get into this quick, basically, I'm trying to get the output of this function to be sent to a file. The file is creating without an issue but it's always 0 bytes. It's not piping the information through and I don't understand why.

First Attempt

function Get-MD5{
<#
.Synopsis
MD5 file hasher
.Description
Generates the MD5 hash of a file you feed in.
.Example
Get-MD5 -FilePath c:\windows\system32\cmd.exe
#>
Param(
    [Parameter(Mandatory=$True)]
    [string]$FilePath = "C:\Windows\system32\cmd.exe"
)
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePath))).Replace('-', '')
Write-Host `nFile: $FilePath`nMD5: $hash`n | Out-File -FilePath "D:\AyyInfo\ MD5-%computername%-$(((get-date).ToUniversalTime()).ToString("ddMMyyyy_hhmmss")).txt"
}

Second Attempt

function Get-MD5{
<#
.Synopsis
MD5 file hasher
.Description
Generates the MD5 hash of a file you feed in.
.Example
Get-MD5 -FilePath c:\windows\system32\cmd.exe
#>
Param(
    [Parameter(Mandatory=$True)]
    [string]$FilePath = "C:\Windows\system32\cmd.exe"
)
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePath))).Replace('-', '')
Write-Host `nFile: $FilePath`nMD5: $hash`n > "D:\AyyInfo\ MD5-%computername%-$(((get-date).ToUniversalTime()).ToString("ddMMyyyy_hhmmss")).txt"
}

Final Attempt

function Get-MD5{
<#
.Synopsis
MD5 file hasher
.Description
Generates the MD5 hash of a file you feed in.
.Example
Get-MD5 -FilePath c:\windows\system32\cmd.exe
#>
Param(
    [Parameter(Mandatory=$True)]
    [string]$FilePath = "C:\Windows\system32\cmd.exe"
)
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePath))).Replace('-', '')
Out-File -FilePath "D:\AyyInfo\ MD5-%computername%-$(((get-date).ToUniversalTime()).ToString("ddMMyyyy_hhmmss")).txt"
}

Any help would be much appreciated.

1 Answer 1

1

you need to pipe the content of your variable $hash to the out-file cmdlet:

"$filepath`t$hash" | out-file "D:\AyyInfo\MD5-$($env:computername)-$(((get-date).ToUniversalTime()).ToString("ddMMyyyy_hhmmss")).txt"

I've changed the %computername% with the right way to build a string with the actual computer name using environment variable.

In your two first code in addition to lacking the pipe of your variable you can't use the cmdlet write-host cause it force the output only to the console and nowhere else.

Sign up to request clarification or add additional context in comments.

5 Comments

That's what I did in the first example though right? I mean, `n was only to make the output more visible in the console, I've removed them now but it's still creating blank files. And thanks for changing the computername thing, I wasn't sure about that one either but in my defence, hadn't done much looking into it yet :D
I went back and realised what you were saying, that works. Any idea how to force a line break into the file? Right now it's just throwing both variables in a single line with no formatting.
The first example has a pipe but it's also outputting to the host which I figured might have been an issue. I'll just write 2 separate lines.
ideally, I want to be outputting $FilePath so there's a filename attached to the hash in the .txt
ok, edited my answer again. Remember to accept it as the answer if it fhelp ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.