Category Archives: Code

Any Coding or Hacks to perform a task

Standard Logon Script (*.BAT)

@echo off
cls
REM #############################################################
NET TIME /DOMAIN /SET /Y
echo User: %username%=20
echo Computer: %computername%=20
date /T
time /T

REM Creates a Folder on the server based on the *username*

if not exist \\*server*\Users\%username% mkdir \\*server*\Users\%username%

REM copies a BAT file to the local PC which allows users to simple do
"start -> run -> ip" and displays there ip address in a dos window.

XCOPY \\*server*\NETLOGON\Logon_Software\ip.bat %systemroot%\system32 /y /i

BAT CMD for creating a share on the system:

net share Data="S:\Data" /remark:"Share on Server"

List all AD users & email addresses

Simply put this into the Start -> Run bar and hit enter! to get a print out of users & email addresses within the domain.

  • cmd /c dsquery.exe * -limit 0 -filter “(&(objectCategory=person)(objectClass=user)(mail=*))” -attr name mail >”c:\PrimaryEmailAddresses.txt”
  • cmd /c csvde.exe -r “(&(objectCategory=person)(objectClass=user)(mail=*))” -l name,mail -f “c:\PrimaryEmailAddresses.csv”

Exchange 2007 | EMC Powershell

Open “Exchange Management Shell”

Shortcut refs to: (C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1” -noexit -command “. ‘C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1′”)

If this is not installed, look at the following article for installing Exchange: http://technet.microsoft.com/en-us/library/bb123694(EXCHG.80).aspx

Don’t get the Exchange Management Shell & Windows Powershell mixed up – for Windows Powershell see:


Adding send as permissions to mailbox:

Add-ADPermission "Mailbox" -User "Domain\User" -Extendedrights "Send As"

Adding full access permissions to mailbox:

Add-MailboxPermission "Mailbox" -User "Trusted User" -AccessRights FullAccess

Adding full access permissions to ALL mailboxes:

Get-Mailboxdatabase | Add-AdPermission -User "Username" -AccessRights GenericAll

List all mailbox stores in size order:

Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount

List all mailbox stores in size order (Export to CSV / on Desktop):

Get-MailboxStatistics -Database “Mailbox Database” | Select DisplayName, LastLoggedOnUserAccount, ItemCount, TotalItemSize, LastLogonTime, LastLogoffTime | Export-CSV test.csv

List all mailbox stores in size order:

Get-MailboxStatistics -Database “Mailbox Database” | Sort -Property TotalItemsize | Format-Table DisplayName, LastLoggedOnUserAccount, ItemCount, @{expression={$_.totalitemsize.value.ToMB()};label=”Size(MB)”}, LastLogonTime, LastLogoffTime

Purge all disconnected mailboxes:

(Add all the disconnected mailboxes into a var by typing the below:)

$users = Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid,Database

Purge all the disconnected mailboxes in the previously made var by typing the below:

$users | ForEach { Remove-Mailbox -Database $_.Database -StoreMailboxIdentity $_.MailboxGuid -confirm:$false }

List all Public folder email addresses (output to file) 

I had a request for all the email addresses associated to public mailboxes, as these are not displayed on the client in Outlook.

Method1:

Displays a list of Public Folders & Email addresses output to TXT File (FL = Formats list with full text)

Get-PublicFolder -Recurse | Get-MailPublicFolder | fl DisplayName,Emailaddresses > c:\publicfolders_list.txt

Method2: Displays a list of Public Folders & Email addresses output to CSV File:

# Export-CSV PowerShell Spreadsheet
Clear-Host
$FilePath = "c:\publicfolders.csv"
Get-MailPublicFolder | Select-Object DisplayName -expand emailaddresses| Export-CSV $FilePath

List Exchange 2007 Product Version

Get-ExchangeServer | fl name,edition,admindisplayversion

List Exchange 2007 Database Size

Get-MailboxDatabase | foreach-object {add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinGB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter ('name=''' + $_.edbfilepath.pathname.replace("\","\\") + '''')).filesize / 1GB),2)) -passthru} | Sort-Object mailboxdbsizeinGB -Descending | format-table identity,mailboxdbsizeinGB

List Exchange 2007 GUID

Get-MailboxDatabase -Identity "<server name>\<storage group name>\<database name>" | Format-Table Name, GUID

Public Folder Permissions via Powershell

Type the below, replacing ‘public folder name’ with your public folder name/path and you must keep the \ at the front of it and also replace ‘username’ with the username:

Add-PublicFolderClientPermission -Identity "\public folder name" -AccessRights Owner -User username

Owner can be replaced with the following roles:

  • None
  • Owner
  • PublishingEditor
  • Editor
  • PublishingAuthor
  • Author
  • Non-Editing Author
  • Reviewer
  • Contributor

Source: http://technet.microsoft.com/en-us/library/bb310789(EXCHG.80).aspx


Finding a Mailbox via Powershell

Get-Mailbox -identity findmyemail@mydomain.com

List Members of a Distribution List (output on screen)

Get-DistributionGroupMember-identity "staff.technical"

List Members of a Distribution List including their primary email address and formatted (output to CSV on C:\)

Get-DistributionGroupMember –identity “staff.technical” | ft name, primarysmtpaddress > c:\members.csv

List Members of a Dynamic Distribution List

$group = Get-DynamicDistributionGroup –identity “AllStaff-DL”
Get-Recipient –RecipientPreviewFilter $group.RecipientFilter | sort name | select name > C:\dlist_members.txt

Continue reading

Static Routes (Add/Delete)

To add a static IP route:

  • Start -> Run -> “CMD”:
  • route add 192.168.xxx.xxx MASK 255.255.255.0  192.168.xxx.xxx /p

To remove a static IP route:

  • Start -> Run -> “CMD”:
  • route delete destination (ie: route delete 192.168.xxx.xxx)

where: destination specifies either an IP address or network name for the remote TCP/IP host computer or network.
For example, to delete a static route to the 10.0.0.0 network, you type the following at a command prompt:
route delete 10.0.0.0

To print static routes:

  • Start -> Run -> “CMD”:
  • route print

Report of all Users and Groups in my AD (DomainUinfo.vbs)

I usually get asked a lot for lists, specifically lists of AD users and members of groups etc. There are a couple of methods to obtain this…

Output user list in txt format using CMD:

  • net user /domain > C:users.txt (Only Lists Logon Usernames / no OU or Group Info)

Output user list in .txt format (more detail) using VBS:

  • Simple VBS Script to printout the AD information
  • Text file output to the same folder the script was run from…
  • I’ve slightly tweaked this script to make the output data more easy on the eyes.

Continue reading

Outlook 2003/2007 | Recover Deleted Item in (Exchange 2007)

Outook 2007
1. Open an Outlook client that has access to the mailbox that has deleted items.
2. Select the Deleted Items folder.
3. From the Tools menu, select Recover Deleted Items.
4. Select the item that you want to recover, and then click Recover Selected Items.

Outlook 2003
For Some reason this option is greyed out on 2003 why???
1. Start, Run, “Regedit”
2. Locate: HKEY_LOCAL_MACHINESOFTWAREMicrosoftExchangeClientOptions
3. Add DWORD Value “DumpsterAlwaysOn” Value: “1” Value: “Decimal”7.
4. Close Registry Editor.
5. Restart Outlook.

Deleted item retention must be configured on the server


IP Display in Command Prompt (*.BAT)

For users that cant find the IP!! Add this bat file to the system32 folder, this allows users to simple run the file by “start -> run -> ip”

“ip.bat”
@echo off
:: Quick IP – Simply displays local IP address using “run -> ip”
:: Date: Sep 2010
:: Author: de
:: ===========//=============
ipconfig
pause

To automatically add the above file to the system32 during startup create another *.bat file and use this code. Remember for Windows7 they will need admin privileges to access the system32 folder.

“Loginscript.bat”
XCOPY **ServerDomain***NETLOGONLogon_Softwareip.bat %systemroot%system32 /y /i

BGinfo Auto Background set @ Login (*.VBS)

“bginfo_admin_background.vbs”

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "\\domain.com\NETLOGON\Logon_Software\bginfo.exe \\domain.com\NETLOGON\Logon_Software\BGINFO_background.bgi /timer:0", 1, True

I usually do this layout:

“<Host Name>”

Default Gateway: <Default Gateway>
IP Address: <IP Address>
Logon Domain: <Logon Domain>
Logon Server: <Logon Server>
Machine Domain: <Machine Domain>
OS Version: <OS Version>
Service Pack: <Service Pack>

Easy zip up of these settings here

Requires BGinfo: Sysinternals

Microsoft Office Outlook 2003 Blocked attachments (*.exe)

Regedit to stop *.com & *.exe in outlook 2003 (different reg entry for 2007)

Regedit
 
[HKEY_CURRENT_USERSoftwareMicrosoftOffice11.0OutlookSecurity]
"Level"=dword:00000003
"UseCRLChasing"=dword:00000001
"Level1Remove"=".exe;.com"

Remove Proxy Settings in IE (*.REG)

Regedit

“remove-Proxy-Settings.reg”

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settings]
"EnableAutoProxyResultCache"=dword:00000000
"EnableNegotiate"=dword:00000000
"ProxyEnable"=dword:00000000
"AutoConfigURL"=""
"ProxyServer"=""
"ProxyOverride"=""

BAT File (Call the reg Procedure):

“run-remove-Proxy-Settings.bat”

"reg import remove-Proxy-Settings.reg"