Wednesday, April 22, 2009
Oracle buys Sun !!
I did not use Java technology a lot, I admire it, but I cannot consider myself as a Java developer. However, having Java technology in our world and it's competition with Microsoft's .net framework is a healthy thing. It gives us options.
Some Java developers were worried after news about the deal has spread. I think that Oracle + Java has been and will stay a good choice to build enterprise applications. Maybe the future will carry news about better and better RIA applications built with Java platform.
What I really care about and made me worried is the future of MySQL, which is the most popular open source database that has a free community edition.
How is this related to MySQL? Here is the story:
MySQL has a pluggable database engine architecture. So it has many database engines to choose from, and you deal with them all using the same SQL dialect.
One famous engine is the InnoDB engine, which is transactional and high performance database engine that was widely used by MySQL users. InnoDB was developed by Innobase, a Finnish company.
Oracle acquired Innobase, which caused worries about the future of the transactional engine. Sun bought MySQL. And MySQL released the Falcon storage engine.
Now, what is the future of MySQL under the control of Oracle, the database giant? The problem is not only that Oracle may kill MySQL gradually (I think it won't do it directly). But is that MySQL engineers started to leave Sun after Oracle's deal. (more about this in this article).
So. Big fishes eat small fishes. It's the turn of open source advocates and MySQL users to have their word.
Saturday, April 18, 2009
Calling a PowerShell script in a path with a white space from command line
First, how to call a script from PowerShell console when the script file path contains white space? because executing this:
PS C:\> c:\new folder\myscript.ps1 param1will give this error:
The term 'c:\new' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
Putting the path between quotations like this:
PS C:\> "c:\new folder\myscript.ps1" param1Will lead to:
Unexpected token 'param1' in expression or statement.
And the solution is to use the Invoke Operator "&", which is used to run script blocks
PS C:\> & 'c:\new folder\myscript.ps1' param1So farm so good. Now coming to the next part which is calling this from command line.
Executing a PowerShell script from command line is as easy as:
C:\Documents and Settings\Hesham>powershell c:\MyScript.ps1 param1This is fine as long as the script path has no spaces. For example, executing:
C:\Documents and Settings\Hesham>powershell c:\new folder\MyScript.ps1 param1Again gives:
The term 'c:\new' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
With the help of PowerShell -?, here is a solution:
C:\Documents and Settings\Hesham>powershell -command "& {&'c:\new folder\MyScript.ps1' param1}"Tada!!
Saturday, March 7, 2009
Why should you learn PowerShell?
It's amazing capabilities open many possibilities for you and it can be used in several scenarios:
- For system administrators: quick and easy way to deal with the system in a consistent way. You'll have the power of many built-in commands, the .net CLR. Using it, you can perform several tasks like managing the file system and permissions, monitor even log, work with Active Directory. And so much more.
- As a Developer: you can use PowerShell commands to automate some systems like exchange server from your application. Most Microsoft server products support or will support PowerShell as a programmable interface to automate the product. You can also use it to check environment issues if you have a production or testing environment issues that you suspect it's root cause to be environment related.
- As a tester: PowerShell can be used for testing automation. With its easy to use commands and the simple syntax, I believe it's very suitable for this purpose. Have a look at: Why Should I Test With PowerShell?
If you want to get a high level image about what PowerShell can do for you and the flexibility it provides, you can watch this video by Jeffrey Snover the architect of PowerShell:
Monday, February 2, 2009
How to know Active Directory attribute names
I stumbled upon a nice msdn page that has the Mappings for the Active Directory Users and Computers Snap-in. It has has links to object type specific UI labels to attribute names.
For example, the User Object User Interface Mapping page shows that the Office UI label maps to physicalDeliveryOfficeName. How could you guess it?
Friday, January 30, 2009
Working with Active Directory using PowerShell
Working with Active Directory is one of the important administrative tasks. VBSctipt was the most used language for administartors to automate repetitive tasks.
Now, windows PowerShell is the future, so it's important to know how to use it to work with Active Directory.
I'll provide a simple example that should clarify some concepts. In this scenario, it's required to set the email attribute of all users under a certain OU (Organaizational Unit) in the format: sAMAccountname@domainname.com and output the results to a text file.
PowerShell 1.0 does not have specific built in Cmdlets to handle active directory objects. But it has a basic support for [ADSI]. This will not limit us as we still can use the .net class library easly in PowerShell.
Here is how the code works:
- First we decclare a variable that holds the output file path:
$filePath = "c:\MyFile.txt" - Then, create the root directory entry which represents the OU that we need to modify users under it. Note the LDAP: it tells: get the OU named "MyOU" from the domain "win2008.demo"
$rootOU=[ADSI]"LDAP://ou=MyOU,dc=win2008,dc=demo" - We need to get all users under this OU, so we create a .net directory searcher instance using New-Object Cmdlet
$searcher= New-Object System.DirectoryServices.DirectorySearcher - Setting the root of the search to the OU and the filter to find users only. and start to find all objects that match the filter:
$searcher.searchroot=$rootOU
$searcher.Filter = "objectclass=user"
$res=$searcher.FindAll() - Initializing the output file by writing the string "Emails"
"Emails:" Out-File -FilePath $filePath - Iterating on the results:
foreach($u in $res) - Getting the user object and setting the mail attribute, and committing:
$user = $u.GetDirectoryEntry()
$name=$user.sAMAccountname
$user.mail="$name@win2008.demo"
$user.SetInfo() - Appending the mail to the output file (note the append parameter):
$user.mail Out-File -FilePath $filePath -append
c:\filename.ps1
note that you need to execute Set-ExecutionPolicy RemoteSigned first.
And here is the complete code listing, note that no error checking or handling is included for simplicity.
#Set-ExecutionPolicy RemoteSigned
$filePath = "c:\MyFile.txt"
$rootOU=[ADSI]"LDAP://ou=MyOU,dc=win2008,dc=demo"
$searcher= New-Object System.DirectoryServices.DirectorySearcher
$searcher.searchroot=$rootOU
$searcher.Filter = "objectclass=user"
$res=$searcher.FindAll()
"Emails:" Out-File -FilePath $filePath
foreach($u in $res)
{
$user = $u.GetDirectoryEntry()
$name=$user.sAMAccountname
$user.mail="$name@win2008.demo"
$user.SetInfo()
$user.mail Out-File -FilePath $filePath -append
$user.psbase.Dispose()
}
$rootOU.psbase.Dispose()
$res.Dispose()
$searcher.Dispose()
Thursday, January 1, 2009
Articles I read in 2008
2007 list can be fond here
- Sanity Checks for Your ASP.NET Web Site
- ICallback and JSON-Based JavaScript Serialization
- .NET Tip: Creating a Thread-Safe Singleton
- Interviewing thread on altdotnet
- 5 Tips for Becoming a Better Data Modeler
- When GETDATE() is not a constant
- When a Function is indeed a Constant
- SQL Server 2008 Launch - The Day After
- Exporting data to a remote server with SQL Express
- Creating objects - Perf implications
- Reading Code Is Hard
- The First Rule of Programming: It's Always Your Fault
- Updated SQL Injection
- Write Readable Code By Making Its Intentions Clear
- Short, concise and readable code - invert your logic and stop nesting already!
- Invert your logic part 2: For loops and while loops
- The HierarchyID Datatype in SQL Server 2008
- Column-Store Databases and DW Appliances: How to Make the Right Choice
- 4 Simple Rules for Handling SQL NULLs
- Standard Query Operators with LINQ
- Which to use: "<>" or "!="?
- 1753, datetime, and you
- Analysts: Upgrading to Vista SP1 on Intel Chips? Proceed with Caution
- More about sparse columns and column_sets
- Entity-Attribute-Value model
- Gotcha! SQL Aggregate Functions and NULL
- An editor's top six pet peeves
- Reviewing Article Submissions
- The Case of the Skewed Totals
- The "Numbers" or "Tally" Table: What it is and how it replaces a loop.
- Simplify SQL Server 2005 queries with a Dates table
- Name Value Pair - Part II
- Google and the Wisdom of Clouds
- IFileOperation in Windows Vista
- Is a Temporary Table Really Necessary?
- Implementing Table Inheritance in SQL Server
- Implementing Table Interfaces
- AOP Using Spring.NET - Part 1
- Five things you should never tell your boss
- Five things you should always tell your boss
- Correlated Joins Using "Apply"
- Introduction to JavaServer Faces
- How to solve Windows system crashes in minutes
- The 10 Commandments of Web Design
- 10 of the Biggest Platform Development Mistakes
- Programmer Competency Matrix
- Calculating Age
- Return Query Text Along With sp_who2 Using Dynamic Management Views
- Log Shipping vs. Replication
- The New ETL Paradigm
- Bookmark Lookups
- The Great Debate: Security by Obscurity
- Security by Obscurity?
- Outer joins and ON clauses vs. WHERE clauses
- Digging into IDisposable
- Warning: SharePoint can create chaos if not used properly
- Tough Choices: How Making Decisions Tires Your Brain
- Linus Torvalds, Geek of the Week
- Deployment Pipelines: Revolutionizing Release Management
- 7 Ways to Improve Your Software Release Management
- Quality Doesn’t Just Happen
- Four Qualities of a Top Performing IT Department
- Dynamically generating typed objects in .NET
- IDictionary options - performance test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable
- Software Tool design: The Three Rs - Part 1: The Three ‘R’s – Research, Research & Research
- JSON and other data serialization languages
- Five Things Linus Torvalds Has Learned About Managing Software Projects
- Is unit testing doomed?
- Microsoft worried over .NET fragmentation
- How do Convert IP Address to Country Name
- ASP.NET gets no Respect
- Google App Engine: Getting Data Out Ain't Simple. Yet.
- Top 10 Performance Improvements in IIS 7.0
- The Agile System Development Lifecycle (SDLC)
- 7 Agile Leadership Lessons for the Suits
- Has Agile Peaked? Let's look at the numbers
- Agile Adoption Rate Survey: February 2008
- Local Temporary Tables and Table Variables
- Design pattern – Inversion of control and Dependency injection
- Design Patterns: Dependency Injection
- Management Studio Improvements in SQL Server 2008
- Changing Good Programmers into Great Programmers
- Ten career-damaging behaviours to avoid
- Threat Modeling: Uncover Security Design Flaws Using The STRIDE Approach
- Cloud computing is a trap, warns GNU founder Richard Stallman
- Speeding up the Performance of Table Counts in SQL Server 2005
- Does Your Database Contain Sensitive Data?
- Brad's Sure DBA Checklist
- Using Covering Indexes to Improve Query Performance
- Why This SQL Server DBA is Learning Powershell
- Five old-fashioned Web concepts that need to die
- How Cloud Computing Works
- At Your Service: Versioning Options
- Moving Large Table to Different File Group
- SQL Server 2008 SSMS Enhancements - Debugging Support
- Engineering 7: A view from the bottom
- Everything You Know About CSS Is Wrong
- Microsoft to unveil tools to push identity platform into the cloud
- PDC: Anders Gets Dynamic on Future of C#
- Microsoft Delivers Oslo Components
- Microsoft Launches Windows Azure for the Cloud
- Get Rid of the Performance Review!
- Ten Tech Interview Errors
- Architecting .NET Web Applications for Scale & Performance (A Practical Guide)
- The inside view of Microsoft's cloud strategy
- Patterns in Practice: Cohesion And Coupling
- Super Sizing Columns in SQL Server
- Once Thought Safe, WPA Wi-Fi Encryption Is Cracked
- Microsoft: 'Geneva' Will Help Change Access Paradigm
- Security Quiz: Test Your Security IQ
- Agile SDL: Streamline Security Practices For Agile Development
- Test Run: Group Determination In Software Testing
- Service Station: Authorization In WCF-Based Services
- SQL Server 2008 Top 10 List for Developers
- Invoking Stored Procedures through a One Line Native .NET Method Call
- Restore Transaction Logs for Point in Time Recovery
- New Features in Visual Studio 2010 and the .NET Framework 4.0
- Notes from Microsoft PDC 2008
- Test-Driven Development of T-SQL Code
- Implementing Change Data Capture in Microsoft® SQL Server 2008
- Auto generated SQL Server keys - uniqueidentifier or IDENTITY
- Normalizing-Denormalized Tables
- Selecting Rows Randomly from a Large Table
- Is It Worth Upgrading to SQL Server 2008
- A journey into Expressions
- Using SOAP Faults
- AnemicDomainModel
- Inversion of Control Containers and the Dependency Injection pattern
- Five Strategies Microsoft Got Right
- Filtered Indexes in SQL Server 2008
- .NET Garbage Collector PopQuiz - Followup
- Cheat Sheet – patterns & practices Security Engineering
- Does Google Have a Secret OS?
- Power and Deception of CTEs
- SQL Server Data Warehouse Cribsheet
- ASP.NET MVC 101
- How To Avoid Msg 106
- It's Time to Get Good at Functional Programming
- Ten Commandments of Egoless Programming
- Inside VSTS: Code Metrics
- 5 Reasons for Software Developers to Do Code Reviews (Even If You Think They're a Waste of Time)
- The A-Z of Programming Languages: F#
- KBSoft IP Locator
- 10 Useful Techniques To Improve Your User Interface Designs
- Validate Business Objects Declaratively
Friday, December 5, 2008
Handling errors when calling PowerShell using C#
First, we should know about the types of errors that need to be handled:
- Exceptions due to error in syntax of the command or script.
- Errors generated from commands themselves due to logical errors or invalid parameters.
Several exceptions can be thrown. The base for PowerShell exceptions is the System.Management.Automation.RuntimeException class. Exceptions can originate from bad syntax or calling invalid commands.
Handling this kind of errors follows the common exception handling in .net as this example shows:
try
{
using (RunspaceInvoke invoke = new RunspaceInvoke())
{
result = invoke.Invoke("dir " + "c:\\" + " -recurse -Filter *.exe");
}
}
catch (System.Management.Automation.RuntimeException ex)
{
Console.WriteLine(ex.Message);
//Specific handling for PowerShell errors
}
catch (Exception ex)
{
//general handling and logging
}
Command Errors:
These errors that commands return. For example, when you run this command in PowerShell while you don't have a Q drive:
dir Q:You'll get this error:
Get-ChildItem : Cannot find drive. A drive with name 'q' does not exist.Which makes sense. But how to check for this kind of errors?
At line:1 char:4
The Invoke method of the RunspaceInvoke class has an overload that accepts an IList as the 3rd parameter. This out parameter will contain a list of errors that has occurred.
IList errors;
using (RunspaceInvoke invoke = new RunspaceInvoke())
{
result = invoke.Invoke("dir " + "Q:\\" + " -recurse -Filter *.exe", null, out errors);
}
if (errors.Count > 0)
{
PSObject error = errors[0] as PSObject;
if (error != null)
{
ErrorRecord record = error.BaseObject as ErrorRecord;
Console.WriteLine(record.Exception.Message);
Console.WriteLine(record.FullyQualifiedErrorId);
}
return;
}
In the above code, I cast the error to PSObject and
get its BaseObject and cast it to ErrorRecord which contains the error information.An interesting part here, is that you can check the FullyQualifiedErrorId property which is a string to distinguish errors and create logic to handle specific errors. ErrorDetails property can also be checked. But be careful because it can be null.
I hope this post can make your programming with PowerShell easier.