Looks like we just clocked over 40,000 page views!
Never really expected anyone to to read this blog when it started and it's really gratifying to know that what we're putting out there is helping people at least some of the time.
So a big thanks to all the Readers, Robots, Bots, Crawlers, Advertisers oh and actual honest to goodness humans that are making our page views climb!
Tuesday, May 5, 2015
Friday, April 10, 2015
Everyday Powershell - Part 32 - Create-TaskbarPinnedShortcut
So you may have a need to pin things to taskbars... We Certainly do apparently clicking on the start menu is a step too far for some users... Long and boring story.
Anyway here's some powershell.
$itemstopin = "C:\Program Files
(x86)\Microsoft Office\Office15\excel.exe",
"C:\Program Files (x86)\Microsoft Office\Office15\outlook.exe", "C:\Program Files (x86)\Microsoft Office\Office15\winword.exe" foreach($item in $itemstopin){ $path = [system.io.path]::getdirectoryname($item) $app = [system.io.path]::getfilename($item) $shell = new-object -com "Shell.Application" $folder = $shell.Namespace($path) $item = $folder.Parsename($app) $item.InvokeVerb('taskbarpin') } |
All credit goes to this post on stackoverflow http://stackoverflow.com/questions/9739772/how-to-pin-to-taskbar-using-powershell I look at that site and often wonder how many programmers there really are versus how many cut and paste from there.
SOMEONE is writing all this code that the rest of us are copying. Wonder what the ratio really is?
SOMEONE is writing all this code that the rest of us are copying. Wonder what the ratio really is?
Friday, February 13, 2015
Everyday Powershell - Part 31 - Send email with multiple attachments
So back in Part 24 reader sivaramsharma asked if send-mailmessage could handle multiple attachments so we threw together this post...
It's a neat little example of generating some HTML in a loop then sending it off using send-mailmessage.
$files = get-childitem C:\reports | where name -like "*.png"
$body = @() $attachments = @() foreach($file in $files){ $filename = [system.io.path]::GetFileName($file.FullName) $attachments += $file.fullname $body += "TEST <br /><img src='" + $filename + "'/>TEST <br />"
}
$body = $body | Out-String Send-MailMessage -to someuser@someserver.com -From someotheruser@someserver.com -SmtpServer somemailserver -Subject "Test" -BodyAsHtml $body -Attachments $attachments |
It's a neat little example of generating some HTML in a loop then sending it off using send-mailmessage.
Thursday, February 5, 2015
Everyday Powershell - Part 30 - Beware of reinventing the wheel
Hmmm bit of a funny one today... So I spent a few minutes writing this;
function get-recursegroupmembers{ [CmdletBinding()] param ( [Parameter(mandatory=$true,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)] [string]$groupname ) $users = @() $groups = get-adgroupmember $groupname foreach($object in $groups){ if ($object.objectclass -eq "group"){ get-recursegroupmembers $object } if ($object.objectclass -eq "user"){ $users += $object } } $users } |
When I realised that this would do just fine;
get-adgroupmember $group -Recursive
|
So yeah, lesson for the day is don't spend valuable time writing stuff when the problem has already been solved.
Tuesday, October 14, 2014
Everyday Powershell - Part 29 - Add-SMTPAlias
It's funny. When you think about how many clicks you need to go through to add an SMTP alias to an Exchange mailbox... There's a lot of clicks! Or you could just use this function and be done in 5 seconds.
function Add-SMTPAlias{
<# Add-SMTPAlias
.SYNOPSIS
Adds smtp alias to a given mailbox
.DESCRIPTION
.PARAMETER
alias
Users
Alias
.PARAMETER
newsmtpaddress
desired email alias
.EXAMPLE
Add-IPToSMTPRelay
-alias someuser -newsmtpaddress someusersotheremail@someserver.com
#>
[CmdletBinding()] param ( [Parameter(mandatory=$true,ValueFromPipeline=$true)] $alias, [Parameter(mandatory=$true,ValueFromPipeline=$true)] [mailaddress]$newsmtpaddress ) process{ $emailaddresses = (get-mailbox $alias).emailaddresses $emailaddresses += $newsmtpaddress.ToString() try{ Set-Mailbox $alias -EmailAddresses $emailaddresses -ErrorAction stop } catch{ write-warning ("Problem setting email address " + $_) } } } |
5 minutes or 5 seconds, the choice is yours GUI fans. Maybe it's time to give in to the power of the shell?
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.
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.
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.
Labels:
function,
get-receiveconnector,
param,
process,
set-receiveconnector
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.
Subscribe to:
Posts (Atom)