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

Thursday, September 22, 2011

Microsoft 365 Cloud Remote Management

Alright so I been learning how to play with Microsoft 365. In particular management of Exchange server via “Exchange Management Console” and “Exchange Management Shell aka Powershell”.

I found very amazing tutorial on http://technet.microsoft.com/en-us/edge/Video/hh278971 by Naomi Alpern. I watched the whole thing since it was very interesting. But you if you really want to skip it to fun part then skip it to 20 minutes into to tutorial.

First of install "Exchange Management Tools" from Exchange 2010 Sp1 cd. I downloaded mine off microsoft. Extract the files and run the setup from run. Cmd into the folder where exchange 2010 sp1 setup file is located and write "setup /R:MT"

To configure Exchange Management Console do the following steps:-

·         From the “Exchange Management Console” right click on “Microsoft Exchange” and click on “Add Exchange Forest…”

·         You will be prompted with the below dialog box. In “Specify a friendly name for this Exchange forest”. Write whatever is easy to remember for you. I put mine as “Exchange Online”. After that make sure you select “Exchange Online” from the drop down menu of “Specify the FQDN or URL of the server running the Remote PowerShell instance:” Click “OK” and it will ask you for the username and password for your Microsoft 365 account.
·         After you put in your username and password it will refresh the EMC and if it accepts the credentials it will list your exchange forest in the list.

That was the "Exchange Management Console" connection settings. Now for "Exchange Management Shell (PowerShell)"

To connect through PowerShell use the following script
========================================
Set-ExecutionPolicy unrestricted
#Ask for credentials
$cred = Get-Credential
#Setup a powershell session to the Exhcange online server
$O365 = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic –AllowRedirection
#Import the cmdlets to your local computer
$importcmd = Import-PSSession $O365
=============================================

Update : Someone asked me to automate the username and password. So here's the script with username & password already added :-

=============================================
Set-ExecutionPolicy unrestricted
#Change to your username of microsoft 360 below
$userName = "username@domain.onmicrosoft.com"
#Change yourpasswordhere with your password
$passWord = ConvertTo-SecureString "yourpasswordhere" -Force -AsPlainText
$cred = New-Object System.Management.Automation.PSCredential($userName, $passWord)
$O365 = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic –AllowRedirection
$importcmd = Import-PSSession $O365
========================================
The above will connect you to your online cloud exchange server. You can test it out by running exchange commands. get-mailbox or anything else.

That pretty much sums it up. Not much different from your local exchange server. Do keep in mind there are few settings not visible on cloud exchange server since its running multiple clients setup (hosted exchange setup).

Windows Registry

*-*-*-*-*-*-*-*-*-*-*-*-*-*Warning*-*-*-*-*-*-*-*-*-*-*-*-*-*

Don’t ever change a value in the registry. Ever. We know we just told you to do that, but would you jump off a cliff if we told you to? Don’t ever change a value in the registry. Don’t even say the word registry. We know a guy once who said the word registry, and three days later he was hit by a bus. True story. As a matter of fact, you shouldn’t even have a registry on your computer. If you suspect that you do have a registry on your computer, please call us and a trained professional will be dispatched to your office to remove the registry immediately. If you accidentally touch the registry, wash your hands with soap and water and call a doctor. Do not swallow the registry or get it in your eyes!

Wednesday, September 21, 2011

Windows 7 Shortcut

Windows 7
The → symbol stands for the right arrow key, ← for the left arrow key, etc.


Win+ Maximize the current window
Win+ If the current window is maximized, restore it; if the current window is restored, minimize it
Win+ Dock the current window to the left half of the screen
*If it is already docked left, it is moved to the right half of the screen
*If it is already docked right, it is restored to its original size
Win+ Dock the current window to the right half of the screen
*If it is already docked right, it is moved to the left half of the screen
*If it is already docked left, it is restored to its original size
Win+Shift+ Move current window to the left monitor (with dual monitors)
Win+Shift+ Move current window to the right monitor (with dual monitors)
Win+Home Minimize all but the current window
Win+Space Peek at the desktop
Win+[Plus sign] Zoom in
Win+[Minus sign] Zoom out
Win+P Open the projection menu (generally used for laptops connected to projectors)
Alt+P In Explorer, show/hide the preview pane


Taskbar Modifiers (New in Windows 7)



Shift+Click Open a new instance of the program
Ctrl+Click Cycle between windows in a group
Middle Click Open a new instance of the program
Ctrl+Shift+Click Open a new instance of the program as Administrator
Shift+Right-Click Show window menu


Managing Windows



Alt+F4 Close the active window
Alt+Tab Switch to previous active window
Alt+Esc Cycle through all open windows
Win+Tab Flip 3D [more info]
Ctrl+Win+Tab Persistent Flip 3D
Win+T Cycle through applications on taskbar (showing its live preview)
Win+M Minimize all open windows
Win+Shift+M Undo all window minimization
Win+D Toggle showing the desktop
Win+ Maximize the current window
Win+ If the current window is maximized, restore it; if the current window is restored, minimize it
Win+ Dock the current window to the left half of the screen
*If it is already docked left, it is moved to the right half of the screen
*If it is already docked right, it is restored to its original size
Win+ Dock the current window to the right half of the screen
*If it is already docked right, it is moved to the left half of the screen
*If it is already docked left, it is restored to its original size
Win+Shift+ Move current window to the left monitor (with dual monitors)
Win+Shift+ Move current window to the right monitor (with dual monitors)
Win+Home Minimize all but the current window
Win+Space Peek at the desktop
Win+[Plus sign] Zoom in
Win+[Minus sign] Zoom out


Starting Programs



Win+1 Open the first program on your Quick Launch bar
Win+2 Open the second program on your Quick Launch bar
Win+n Open the nth program on your Quick Launch bar
Win+U Open the ease of access center
Win+F Open the search window
Win+X Open the Mobility Center
Win+E Open Explorer
Win+R Open the Run window [more info]
Win+B Move focus to notification tray (the right-most portion of the taskbar)
Win+P Open the projection menu (generally used for laptops connected to projectors)
Win+Pause Open the System Properties portion from the Control Panel
Ctrl+Shift+Esc Open Windows Task Manager


Logging In And Out


While the below shortcuts seem unwieldy because of their length, they're quite easy to remember once you try them out a few times.


Win, , Enter Shutdown
Win, →, →, R Restart
Win, →, →, S Sleep
Win, →, →, W Switch Users
Win+L Locks computer


Viewing Folders With Explorer



Alt+ Go back
Alt+ Go forward
Alt+ Go up a directory
Alt+D Move focus to address bar
Alt+D, Tab Move focus to search bar
Alt+Enter Open the Properties window of the current selection
Ctrl+Mousewheel Change the view type (extra large, small, list view, detail, etc.)
Alt+P Show/hide the preview pane

Tuesday, September 13, 2011

Useful Net commands - Don`t need admin rights to run these.

I just love using Net commands to get list of users or password expiry dates for users.

Run the following command to get all the data for that specific user on domain

  • net user username /domain

Result :-

C:\Users\ijaved>net user ijaved /domainThe request will be processed at a domain controller for domain test.local.
User name                    ijaved
Full Name                    i Javed
Comment
User's comment
Country code                 000 (System Default)
Account active               Yes
Account expires              Never
Password last set            04/07/2011 9:14:48 AM
Password expires             02/10/2011 9:14:48 AM
Password changeable          04/07/2011 9:14:48 AM
Password required            Yes
User may change password     Yes
Workstations allowed         All
Logon script
User profile
Home directory
Last logon                   13/09/2011 5:03:59 PM
Logon hours allowed          All
Local Group Memberships
Global Group memberships     *Employees            *Domain Users
The command completed successfully.

  • net group /domain
This will list all the groups in your domain.

  • net group "groupname" /domain
This will list users inside specified group name.

* I love these commands because they do not require admin rights. You can run them as normal users and still get the useful information you probably didn`t have access to :)

Additional Commands can be found at : http://www.petri.co.il/list_all_users_and_groups_in_domain.htm

Friday, September 9, 2011

Windows 7 / 2008 shortcut for settings

I hate clicking my way to network settings so found the shortcut.. from run write ncpa.cpl that will open network connections.

Also you can open firewall settings by opening firewall.cpl from run.

So here's the quick list :-

Network Connections = ncpa.cpl
Firewall Settings = firewall.cpl
Device Management = devmgmt.msc
Disk Management = diskmgmt.msc
Services = services.msc
Remote Desktop client = mstsc

 If you know any good ones please leave a comment and I will add it up in ths list :)

Export GAL details to CSV

CSVDE -f GalDump.csv -r objectCategory=person -l "displayName, title, telephoneNumber, department, mail"

Works on Windows 2008 R2 Server with AD. Might come in handy to make SharePoint Contact list by importing this csv.

It exports to the same folder you are in on cmd. Filename will be GalDump.csv

Source: http://smtp25.blogspot.com/2010/05/how-to-dumpexport-gal-to-csv-file.html