Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Email Signature Automation Outlook 2010

Here is a powershell script that will create a signature in Outlook from a pre-existing template. It takes information from AD andf inserts it into the siganure, and sets the signature as the default signature.

It is quite simple create a signature in out look with all the formating you need to fit in with your company brand. Set placeholders for name, role and numbers we replace these with the AD settings of the logged on user.

The reason I am posting this I have seen a lot of long and over conplicated solutions to do this that doesn't work half the time. This is just simple and works. And if you need to change the way it looks you just change the source siganture, you dont need to re - write 200 lines of HTML.

I have some validation in the copy I deploy to create a slightly different signature for people who are in a certain department or who dont have a mobile number. If you are a fter this send me a message.

 #Setting variables
$strName = $env:username
$name = Get-ADUser $strName -properties * | select -ExpandProperty DisplayName
$role = Get-ADUser $strName -properties * | select  -ExpandProperty Description
$ipPhone = Get-ADUser $strName -properties * | select -ExpandProperty telephoneNumber
$mobile = Get-ADUser $strName -properties * | select -ExpandProperty MobilePhone
$filepath = location of your signature

#Sets the signature.
#Finds the word "Name" and replaces it with the users name in Active Directory
#Finds the word "Role" and replaces it with the users Job Title in Active Directory
 #Finds the word "number" and replaces it with the users IP phone number in Active Directory
#Finds the word "mobile" and replaces it with the users mobile number in Active Directory
#Test Signature
(gc $filepath) -replace "User",$name -replace "\bRole\b",$role -replace "\bnumber\b",$ipPhone -replace "\bmobile\b", "| m: +44 (0) ""+$mobile" > C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Signatures\test.txt


#Rich text signature
(gc $filepath) -replace "User",$name -replace "\bRole\b",$role -replace "\bnumber\b",$ipPhone -replace "\bmobile\b", "| m: +44 (0) ""+$mobile" > C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Signatures\test.rtf


#HTML signature
(gc $filepath) -replace "User",$name -replace "\bRole\b",$role -replace "\bnumber\b",$ipPhone -replace "\bmobile\b",  $mobile > C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Signatures\test.htm


#Sets signature named "test" as default signature
$CompanyName="test"
$MSWord = New-Object -com word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$EmailSignature.NewMessageSignature=$CompanyName
$EmailSignature.ReplyMessageSignature=$CompanyName
$MSWord.Quit()

Enjoy!!

Powershell Custom Column Label Names

Sometimes in Powershell the Column label names can be not very intuitive or you may have to present the results to a non technical person who may not understand. In this case we can edit the labels to suit our needs.

The format is quite simple we will use Get-Process in this example. When filtering the property you wish to change the label of use the following code:

@{} Use @ and all your code for the custom column goes between the curly brackets.
Inside of the curly brackets insert the following:
Label="Custom column Label";
Expression={$_.Property Name}

And all together it will look like below.

Get-Process | Format-Table Name,ID,@{Label="Paged Memory Size";Expression={$_.PM }} -AutoSize







You can also further format the output so it better suits your needs. Here we will use Get-WmiObject to see available disk space and format the column header and the output to show us how many GB of free space we have to 2 decimal places.

Here we nee to add some extra code to get the outcome we desire.

/1GB We have divided the $_.freespace property by 1GB you can also divide it by MB,TB and PB if you desire.

formatstring="N2" restricts the output to 2 decimal places you just need to change the number after the N if you desire more or less decimal places.

width=20 Specifies the column header width.

align="Left" Sets the alignment of the data within the column.

All together it looks like:

@{Name="FreeSpaceGB";Expression{$_.freespace /1GB};formatstring="N2";width=20;align="left"} 

And the full script will look like:

Get-WmiObject Win32_LogicalDisk -Filter DriveType=3 | Format-Table SystemName,VolumeName,DeviceId,@{Name="Free Space GB";Expression={$_.freespace / 1GB};formatstring="N2";width=20;align="left"} 



Add user to local Computer group via Comand Line

You can add users to a local security group via the command line with the following  command:

netlocalgroup "groupname" "username" /add

and to confirm it worked:

netlocalgroup "groupname"

I wrote a simple little script to run this via powershell with user input.


























If you have the psexec tools installed you can run it for a remote machine using the following command

psexec \\RemotePC -u UserName -p password net localgroup "Localgroup" "Username" /add

You can download PsExec tools from here.

Compare Active Directory Group Membership with Powershell

Here is a nice little Powershell script I wrote to compare group membership of two active directory users. You can use this script for many different purposes just edit the reference command and property to whatever object you wish to compare.

$UserReference = Get-ADPrincipalGroupmembership (Read-Host "Reference User")
$UserDifference = Get-ADPrincipalGroupmembership (Read-Host "Difference User")

Compare-object $UserReference $UserDifference -property SamAccountName


=> Means the Difference User is a member of  a group and the Reference User is not a member.
<= Means the Reference User is a member of the group and the Difference User is not a member.

You can also use the -IncludeEqual parameter to include groups both users are a member of that will be represented by the SideIndicator value of:  ==

Log off user session on remote machine using Powershell

This is a handy little tool you can use with Powershell or in command prompt to see who is logged onto a remote machine and to end there session. Great for servers when you get the "too many users already logged in" message when trying to access a server.

1. You need to get the session ID of a user to log off by running the following command:
 qwinsta /server:servername









2. Enter the following command with the session ID at the end to log the user of:
logoff /server:servername (session id)

I used these commands to put together the following script. It will prompt you for the server name and give you the output of current open sessions then prompt you for the session ID to log off. Enjoy:

$server = read-host "Hostname"

qwinsta /server:$server

$ID = read-host "Session ID to log off"

logoff /server:$server $ID

Powershell: Reset user password and set to change at next logon

This powershell script can be used to reset user passwords quickly and force them to change it on their next logon. Copy this script into powerhell ISE and save it as a .ps1 file. When you run the script it will prompt you for the username. You can change the password variable to be whatever you want.

$Identity = read-host "Enter User name"
$password = P455w0rd

Set-adaccountpassword -Identity $Identity -reset -newpassword (ConvertTo-SecureString -AsPlainText $password -Force)
Set-aduser $user -changepasswordatlogon $true

or to statically assign the password use the following script:

Set-adaccountpassword -Identity (read-host "Enter User name") -reset
-NewPassword (Read-Host -AsSecureString "New Password")

Find who is logged onto a computer within domain

Someone causing havoc on your network and all you have is the computername? Here is a handy little powershell script to find the culprit.

1. Open Powershell

2. Enter the following script.

get-wmiobject win32_computersystem -comp "computername" | select USername,Caption,Manufacturer











3. Done

Give Calendar permissions with Powershell

Here is a handy Powershell script I wrote to give Calendar permissions via Powershell. Paste the script into Powershell ISE and play it. We often get requests from PA's and senior staff needing fast access to users calendars this makes short work of it. It will add the exchange snap-in, then prompt you for the users mailbox. Enter the user whose calendar is being granted access to. Then the current calendar permissions will be shown then you will be prompted to enter the user requiring access. Then enter permission level and the script will finally display the permissions after the change. (This is for Exchange 2010 for other versions remove the snapin command from the first line.)

 add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
cls

$user = $null

$user = read-host "Calendar Being Configured"
$user = $user + ":\calendar"

get-mailboxfolderpermission -Identity $user | ft user,accessrights

$Identity = $null

$Identity = read-host "User Being Granted Access"

$AccessRights = $null
$AccessRights = read-host "Access Required - Editor,Owner,Reviewer,Contributor"


Add-MailboxFolderPermission -Identity $User –User $Identity –AccessRights $AccessRights

get-mailboxfolderpermission -Identity $user | ft user,accessrights