Wednesday, September 10, 2014

Everyday Powershell - Part 28 - Add-IPToSMTPRelay

Here's one that came up when we'd provisioned a new powershell integration server and couldn't send email from it!

Turns out our email server's weren't allowing it relay. So we needed to add it's IP to the RemoteIPRanges parameter and as Paul Cunningham  points out this can be tricky.

function Add-IPToSMTPRelay{
    <#
    Add-IPToSMTPRelay
    .SYNOPSIS
    Adds an IP address to the allow anonymous SMTP relay Receive connector on TMAIL

    .DESCRIPTION
    You will need to know your receive connectors name... use get-recieveconnector We only use one so it's been hard coded into the funtion, this could be parameterised if you need
    
    .PARAMETER
    IP IP Address to add to allowed anonymous SMTP relay 

    .EXAMPLE
    Add-IPToSMTPRelay 192.168.1.1
    #>

    [CmdletBinding()]
    param (
        [Parameter(mandatory=$true,ValueFromPipeline=$true)]
        [ipaddress]$ip
    )
    process{
        $remoteipranges = (Get-ReceiveConnector "smtp relays").remoteipranges
        $remoteipranges += ($ip).ToString()
        Set-ReceiveConnector "smtp relays" -RemoteIPRanges $remoteipranges
    }
}

You may be saying... "WOAH WOAH WOAH this looks different to your usual posts! What's all this function param process stuff?"

Well as you can see we wrapped up the powershell in a Function. The Comment block at the top is the help. The Param block defines any parameters in this case we've just got $IP. The process block is what actually happens and would be what we'd normally post.

This allows us to embed the function in a module and then stick the module in our powershell profile so it's always available!

This is far better than digging around in a scripts folder we'll dig into setting up a module in the next few weeks.

Friday, September 5, 2014

Everyday Powershell - Part 27 - Uploading files to a FTP server

Full credit to Goyuix for his awesome example on StackOverflow which I am shamlessly ripping off for this Post!

$logpath = "C:\SomePath"
$ftpserver = "ftp://someFTP.com//"
$user = "User"
$password = "Password"
$ftppath = "Somefolder"
$todayslogs = Get-ChildItem $logpath | where {$_.CreationTime -gt ((get-date).AddDays(-1))}

foreach($log in $todayslogs){
    $uploadpath = $ftpserver + $ftppath + "/" + $log.Name 
    # create the FtpWebRequest and configure it
    $ftp = [System.Net.FtpWebRequest]::Create($uploadpath)
    $ftp = [System.Net.FtpWebRequest]$ftp
    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $ftp.Credentials = new-object System.Net.NetworkCredential($user,$password)
    $ftp.UseBinary = $true
    $ftp.UsePassive = $true
    # read in the file to upload as a byte array
    $content = [System.IO.File]::ReadAllBytes($log.FullName)
    $ftp.ContentLength = $content.Length
    # get the request stream, and write the bytes into it
    $rs = $ftp.GetRequestStream()
    $rs.Write($content, 0, $content.Length)
    # be sure to clean up after ourselves
    $rs.Close()
    $rs.Dispose()
}

This one was written to automate the upload of some logs. It happens every day so that's why we just (get-date).addays(-1) but that where on get-childitem can be anything you like.

One thing to note is the hardcoded password. You may not want to do this, depends on your security requirements. You can save the credentials in another file as a secure.string if you like. There's probably a post coming about that.