Wednesday, October 26, 2011

Exchange 2010 Gal Picture Upload Script.

Alright I been working on migrating to Exchange 2010 from Exchange 2007.  Once moved we wanted to upload the pictures for users. You can do this on Exchange 2007 not covered here though.

I made the below script by using multiple source to compile this. Some script I found did something I liked but lacked few things so I went ahead and made my own with all thse things I needed. I am pretty new to Powershell so if you find I can improve then do let me know :)

I added option to check if the picture is already on the user ad attribute to save time. You can disable it if you dont want that. I also used hashtable to keep a log file for successful or failed uploads of pics.

If you have any suggestions to add to this script do let me know :)

You probably need to change Pickup folder location which is $PicFolder

=============Update: 10/11/11===========

I did not explain what this script does cause I thought it was pretty much straight forward cause of comments I added. But anyways I looked at the post today and thought hell it doesn't really say that this can do.

So here it is :- (I listed it in way of execution)

1- Gets users from exchange with get-mailbox | foreach

2- Create and datestamp log files with start of script. (I didnt know how to datestamp each line so had to create it head of execution of script. Maybe someone can help me out with this.

3- Initiate Hashtables for Success Log (Slist) and Failed Log (Flist).

4- For each users found with get-mailbox fetch $alias from it and check $alias.jpg file in pickup folder. If the pic is not there for $alias skip user.

5- Function ChkPic is to check if user has picture already assigned if so skip the user. This is usefull if want to re-run this script with new pictures added to pickup folder. Method used here is Export-RecipientDataProperty with username to see if it shows something. Usually if pic is there it will show RunspaceID in the result. I using that as my ref for pic is there.

6- Check picture file size if its less then or equal to 10k. If bigger than 10k skip user. Otherwise go ahead.

7- So finally we reached here after all that checks YAY. Go ahead and import picture.

8- So you remember that ChkPic (Step 5) I created? we are going to use it again to check if it uploaded the picture successfully. Does the same thing but after uploading. If it finds the pic it says all good otherwise logs it to Flist and displays error.

9- Display information about the logs and location to user.

10- End Script

Please leave a comment cause sometimes I feel that no one is reading this LOL. :p anyways it comes handy for me :)

================ImporToExCh2010=====================
#Pickup folder for pictures.
$PicFolder = "c:\pics"

#Fetch users from Exchange
$results = Get-Mailbox | Foreach {$_.alias}

#DateStamp
$Date=$(Get-Date -format g) | add-content c:\ExPicSuccess.log, c:\ExPicFailed.log

#Using hashtable for logs.
$Slist=@{} #Successful Log file
$Flist=@{} #Failed Log File

#Function to check if pic is in user ad attr
Function ChkPic {
Param ($alias)

#Try to see if user already has pic uploaded
$ChkUpload = Export-RecipientDataProperty -Identity $alias -Picture | out-string

#Match for RunspaceId in result string
$RChk = $ChkUpload -match "RunspaceId"                            }
#End Function ChkPic

Foreach ($alias in $results)
{
$FileLoc = "$PicFolder\$alias.jpg"
$FileExists = Test-path $FileLoc

If ($FileExists -eq $True){
Write-host -f green "`nPicture found for $alias....checking size..."

#Check photo size if its less than 10k
$PhotoSize = Get-ChildItem $FileLoc | select Length

If ($PhotoSize.Length -le 10000) { Write-Host "[Size OK]" -ForeGroundColor Green }
Else {
$Flist.Add($alias, "Pic Size Exceeded 10k - Skipped!")
Write-Host "[Pic Size more than 10Kb..Skipping User!]" -ForeGroundColor Red; Continue}

#Precheck to make sure pic is not assigned. If assigned skip user. (You can disable this)
ChkPic $alias

If ($RChk -eq $True) { Write-Host "User already has picture assigned. Skipping User! `n" -f red;Continue}

#All good going ahead with uploading..
Write-host -f yellow "Uploading..."

#Import Picture to user ad attr
Import-RecipientDataProperty -Identity "$alias" -Picture -FileData `
([Byte[]]$(Get-Content -Path "$FileLoc" -Encoding Byte -ReadCount 0))

Write-Host -f green "Uploaded picture for $alias"
Write-host -f yellow "Checking if uploaded successfully..."

#Calling ChkPic Function
ChkPic $alias
#If statement to see if value is true which means successful and false means pic attr not found.
If ($RChk -eq $True) {
Write-Host -f green "[Uploaded Successfully] `n"

If ($alias) {$Slist.Add($alias, "Successful")}
}
Else {
Write-Host -f red "[Upload Failed]"

$Flist.Add($alias, "Upload Failed.")
}
                          }

Else{
Write-host -f red "Picture not found for $alias in $PicFolder"

If ($alias) {$Flist.Add($alias, "Picture not found.")}
    }
}

Write-Host -f yellow "#Note: Logs files can be found in C: with name of ExPicSuccess.log & ExPicFailed.log `n"

#Add Successful list to log file
$Slist | out-string | add-content c:\ExPicSuccess.log
Write-host -f green "`n Displaying Successful Upload Userlist `n"
$Slist

#Add Failed to log
$Flist | out-string | add-content c:\ExPicFailed.log
Write-host -f red "`n Displaying Failed Upload Userlist `n"
$Flist
=======================================================

Download ps1 from Google Docs

You can download the ps1 incase the copy paste doesnt work. Link provided above

Thursday, October 6, 2011

PowerShell print server inventory script by Michel Stevelmans

Scripts looks very interesting. I haven't tried it but will do that in awhile :)
--------------------------------------------------------------------------------------------
PowerShell script which lists all installed printers and gathers information like the printer name, driver, share name, location and the IP address (the actual IP address, not the portname without the “IP” prefix).




# Print server inventory script
# Created by Michel Stevelmans - http://www.michelstevelmans.com

# Set print server name
$Printserver = "PRINTSERVER"

# Create new Excel workbook
$Excel = new-Object -comobject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Cells.Item(1,1) = "Printer Name"
$Sheet.Cells.Item(1,2) = "Location"
$Sheet.Cells.Item(1,3) = "Comment"
$Sheet.Cells.Item(1,4) = "IP Address"
$Sheet.Cells.Item(1,5) = "Driver Name"
$Sheet.Cells.Item(1,6) = "Shared"
$Sheet.Cells.Item(1,7) = "Share Name"
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True

# Get printer information
$Printers = Get-WMIObject Win32_Printer -computername $Printserver
foreach ($Printer in $Printers)
{
    $Sheet.Cells.Item($intRow, 1) = $Printer.Name
    $Sheet.Cells.Item($intRow, 2) = $Printer.Location
    $Sheet.Cells.Item($intRow, 3) = $Printer.Comment
    $Ports = Get-WmiObject Win32_TcpIpPrinterPort -computername $Printserver
        foreach ($Port in $Ports)
        {
            if ($Port.Name -eq $Printer.PortName)
            {
            $Sheet.Cells.Item($intRow, 4) = $Port.HostAddress
            }
        }
    $Sheet.Cells.Item($intRow, 5) = $Printer.DriverName
    $Sheet.Cells.Item($intRow, 6) = $Printer.Shared
    $Sheet.Cells.Item($intRow, 7) = $Printer.ShareName
    $intRow = $intRow + 1
}

$WorkBook.EntireColumn.AutoFit()
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,1) = "Print server inventory - Created by Michel Stevelmans - http://www.michelstevelmans.com"

Source : http://www.michelstevelmans.com/powershell-print-server-inventory-script/

Wednesday, October 5, 2011

Control Panel Shortcut




Control Panel Files [CPL's]

File name Purpose
-----------------------------------------------------------------------
Access.cpl Accessibility properties
Appwiz.cpl Add/Remove Programs properties
Desk.cpl Display properties
FindFast.cpl FindFast (included with Microsoft Office for Windows 95)
Inetcpl.cpl Internet properties
Intl.cpl Regional Settings properties
Joy.cpl Joystick properties
Main.cpl Mouse, Fonts, Keyboard, and Printers properties
Mlcfg32.cpl Microsoft Exchange or Windows Messaging properties
Mmsys.cpl Multimedia properties
Modem.cpl Modem properties
Netcpl.cpl or Ncpa.cpl Network properties
Odbccp32.cpl Data Sources (32-bit ODBC, included w/ Microsoft Office)
Password.cpl Password properties
Sticpl.cpl Scanners and Cameras properties
Sysdm.cpl System properties and Add New Hardware wizard
Themes.cpl Desktop Themes
TimeDate.cpl Date/Time properties
Wgpocpl.cpl Microsoft Mail Post Office



How to Start Single Sections of Control Panel With a Command

Control Panel Tool Command
---------------------------------------------------------------
Accessibility Options control access.cpl
Add New Hardware control sysdm.cpl add new hardware
Add/Remove Programs control appwiz.cpl
Date/Time Properties control timedate.cpl
Display Properties control desk.cpl
FindFast control findfast.cpl
Internet Properties control inetcpl.cpl
Joystick Properties control joy.cpl
Keyboard Properties control main.cpl keyboard
Microsoft Exchange control mlcfg32.cpl
(or Windows Messaging)
Microsoft Mail Post Office control wgpocpl.cpl
Modem Properties control modem.cpl
Mouse Properties control main.cpl
Multimedia Properties control mmsys.cpl
Network Properties control netcpl.cpl or ncpa.cpl
Password Properties control password.cpl
PC Card control main.cpl pc card (PCMCIA)
Power Management (Windows 95) control main.cpl power
Power Management (Windows 98) control powercfg.cpl
Printers Properties control main.cpl printers
Regional Settings control intl.cpl
Scanners and Cameras control sticpl.cpl
Sound Properties control mmsys.cpl sounds
System Properties control sysdm.cpl

Wednesday, September 28, 2011

PsTools with Powershell - Remote Execution

So I was tasked to remote execute an exe file on a server.

I wanted to use PowerShell to do the whole thing but ended up using PsTools due to lack of time to explore PowerShell Remote.

I ran into few issue. Basically when I was executing the exe file it wasn't displaying anything to the end user. After doing some research I found that exe was running but on the wrong session. For example I wanted to see the Test.exe GUI logged in as a remote user. But when I executed the psexec it ran the Test.exe in console session id 0. My session id was 2. You can see you session by going to taskmanager and go to users tab. There you will see the session id.

In order for me to see Test.exe GUI I had to specify the session on psexec. But problem was that every time I log into remote desktop I would get a new session id.

So I googled it and found the solution for identifying the session id.

So basically psexec runs a command query session and looks for session id of the user where the app should be displayed. i.e the variable $UserSession

=======================================================================
#Make sure you change the location of PsTools exe files below
#Change following variables for your setup
set-alias psexec 'C:\PsTools\PsExec.exe
$UsersSession = 'ijaved' #Username of the user you want the app to show
$UserAdmin = 'admin' #admin user on the remote pc
$AdminPass = 'test' #pass of admin user
$RemotePc = '\\192.168.1.128' #remote pc
$App ='C:\test.exe' # App to run

#No need to change anything below
$results = & psexec '$RemotePC' -u '$UserAdmin' -p '$AdminPass' query session
$id = $results | Select-String "$UsersSession\s+(\w+)" | Foreach {$_.Matches[0].Groups[1].Value}
$delay = ping 127.0.0.1 -n 2
$call =  & psexec '$RemotePC' -u '$UserAdmin' -p '$AdminPass' -i $id -d '$App'
#End Script
=======================================================================

I found a problem where one of the installer.exe won't run even after being called. It would come up and disappear. After trying to figure out something I got my break. I found that when psexec is executing the installer it is doing so from c:\windows\system32 folder and the installer for some stupid reason was looking for its file inside system32. Don't know why but that was the case.

So I made a batch file which called the installer from its own folder. Basically batch file did the following :-

cd \
cd "c:\installerfolder\"
c:\installerfolder\install.exe

Hope it helps anyone out there.

AutoLogin Win 7 - Shortcut for User Accounts Wizard on Win 7

From run write netplwiz to get User Accounts wizard.

  1. Press the Windows key + R on your keyboard to launch the “Run” dialog box.
  2. Type in netpliz or control userpasswords2
  3. Press Enter. The User Accounts window will display.
  4. Uncheck the option “Users must enter a user name and password to use this computer”
  5. Click “OK”
  6. You will then be prompted to enter the current password and confirm it.
  7. After doing so, you will no longer be prompted to enter your password upon login.

If  Checkbox is no there then

1. Press the Windows key + R on your keyboard to launch the “Run” dialog box.
2. Type regedit and hit enter to open the Registry Editor
3. Then browse to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon\
4. Set AutoAdminLogon = 1 (create it if doesn't exist its a string variable)
5. Set DefaultUserName = your username (create it if doesn't exist its a string variable)
6. Set DefaultPassword = your password (create it if doesn't exist its a string variable)

Monday, September 26, 2011

Disable & Enable Services for Mailbox

PowerShell commands to Disable mailbox level services for specificed users. Works well for Microsoft 365 Cloud.

Disable Access to Mailbox

#Set $member to username of the user you wan
$member | Set-CASMailbox -OWAEnabled $false -PopEnabled $false -ImapEnabled $false -MAPIEnabled $false -ActiveSyncEnabled $false -EwsEnabled $false
   
Get-CASMailbox $member | Select-Object Name, OWAEnabled, PopEnabled, ImapEnabled, MAPIEnabled, ActiveSyncEnabled, EwsEnabled

==========================

Enable Access to Mailbox

#Set $member to username of the user you wan
$member | Set-CASMailbox -OWAEnabled $true -PopEnabled $true -ImapEnabled $true -MAPIEnabled $true -ActiveSyncEnabled $true -EwsEnabled $true

Get-CASMailbox $member | Select-Object Name, OWAEnabled, PopEnabled, ImapEnabled, MAPIEnabled, ActiveSyncEnabled, EwsEnabled

Friday, September 23, 2011

Enable / Disable Microsoft 365 ActiveSync service for users.

I am currently working on Microsoft 365 cloud exchange. I want to try out different things which will enable and disable the services for users. I found the below post which stats how to enable and disable ActiveSync for users.

Windows Mobiles can be configured for Active Sync. Active Sync is enabled for all users by Default in Exchange 2007 & 2010.

For Security reasons its recommended to disabled all users and enable only for the required users

Below power shell enabled and disabled Active for all the users in the exchange Organization

get-Mailbox -resultsize unlimited | set-CASMailbox -ActiveSyncEnabled:$False

get-Mailbox -resultsize unlimited | set-CASMailbox -ActiveSyncEnabled:$True

Below powershell command to enable and disable active sync for given set of users in the text file

Get-content C:\users.txt | set-CASMailbox -ActiveSyncEnabled:$True

Get-content C:\users.txt| set-CASMailbox -ActiveSyncEnabled:$False

Source : http://powershell.com/cs/forums/p/2619/3506.aspx