17 Sep 2010

System.Windows.Forms.Timer is not Working in Windows Service

I’m really surprised to know that System.Windows.Forms.Timer is not working in Windows Service project type. The reason from Microsoft is just like this,

“Server-based timers use the thread pool internally, and the event handler runs in a thread taken from the pool. For this reason, conflicts might occur while the event handler is accessing shared variables and modifying controls and forms.”

For this reason, you can’t use the System.Windows.Forms.Timer to periodically run your custom code. Replace it to System.Timers.Timer will help you resolve the situation. I’m using Visual Studio 2010, so it’s very simple to change this code. After adding the ProjectInstaller to the project, then you see your Windows Service Class, and open it, so VS will open the designer for you.

Your windows service class 

After open, just add the System.Windows.Forms.Timer from the Toolbox window.

image

Right after that, open the (+) sign before your Windows Service class file in the Solution Explorer window, and open the “Designer.cs” (depending your project, it can be “Designer.vb”). In my project, the file is “ServerCommand.Designer.cs”, double click that file.

image

Change the content of “InitializeComponents” method, change the System.Windows.Forms.Timer to System.Timers.Timer. See the surrounding red background.

image

And, you’re done. You can add the Timer elapsed event. Double click your Windows Service class file. Double click your timer, and the “Elapsed” event should be added to the code.

image

And this is the example code for Elapsed event,

private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EvLog.WriteEntry("Ex Message", EventLogEntryType.Information);
try
{
System.IO.File.AppendAllText(@"D:\PIS-ServiceCommand " + DateTime.Now.ToString("dd-MMM-yyyy HH-mm-sss", System.Globalization.CultureInfo.CreateSpecificCulture("id-ID")) + ".txt", "sss");
} catch (Exception ex)
{
EvLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
}


Happy Coding…

7 Sep 2010

.NET SSH Connection using SharpSSH

Last week, I’ve been assigned to a new project. I must be able to parse the text output of a router, get the settings, and then doing something magic in .NET, either save it to Active Directory or something. Then, I found this .NET component, for free. You don’t have to pay for it, called SharpSSH. Well, I don’t care about how the DLL connect to the router and get the command output, but I do care of the result of the command output.

You can download the component in the website. The code are really simple, in this example is written in C#.NET. Oh… I almost forgot, you must add the Reference to Tamir.SharpSSH.DLL, and make sure that you add too the DiffieHellman.dll and Org.Mentalis.Security.dll. Actually we don’t really need those 2 DLLs, but Tamir.SharpSSH.dll does. So you must add the reference, or just include the DLL into the Debug folder in order to make your application runs well.

So here you go the code. First, add the “using” directive into top of your code.

using Tamir.SharpSsh;

And you’re ready to make the magic. First of all, there’s so many SSH class objects defined in the Tamir.SharpSSH.dll, but I choose SshShell. This is the code…

SshShell SSHConn = new SshShell("10.2.54.123", "dean");
SSHConn.Password = "deanp@w";
SSHConn.Connect();

Insert the IP, username, and the password, and Connect. Then you might want to look carefully before writing the code, you must first know the router output behaviour. In my environment, first login to the router, the prompt ended with the character like this “>”, and I must type “enable” and Enter, and then insert the password, and the ending prompt changing to “#”. This is important to make your code not “hanging” because of wrong character assigned to the SshShell object.

SSHConn.ExpectPattern = ">";
SSHConn.RemoveTerminalEmulationCharacters = true;
if (SSHConn.ShellOpened)
{
SSHConn.WriteLine("enable");
SSHConn.Expect(">");
SSHConn.WriteLine("deanp@w");
SSHConn.Expect("#");
SSHConn.WriteLine("term length 0");
SSHConn.Expect("#");
SSHConn.WriteLine("show run");
sOut = SSHConn.Expect("#");
SSHConn.WriteLine("exit");
SSHConn.Expect("#");
}

That’s what I’m talking about. After you’ve connected to the router, then the important thing is you must write your line of command to the router. To get the return and probably get the output to a string variable, you can assign variable like in my example, sOut. When you don’t want to get the return, just “apply” the command you’ve set for the router, just execute the “Expect” method, with the ending response of the router like “#” character.


After all, you must close the object using “Close” method, and you’re done.

31 Agu 2010

Simple LINQ Example on Sharepoint Items Query

Actually I don’t remember much about CAML that came up with Sharepoint. CAML is a basic XML based query to fetch some item based on criteria, just like query on SQL. But, It’s so hard to remember! I want to run code as fast as I can, and here’s the answer, LINQ. So, can we start and try this?

OK, before we start, we must ensure that we have .NET Framework 3.5, coz that’s the framework to do the LINQ, and import System.Core.dll to your project. and add the imports below.

using System.Collections;
using System.Collections.Generic;
using System.Linq;

LINQ gives you so many solution to get queries easily, and you don’t need to remember CAML or whatever you’ve called. You can get raw IEnumerable<SPListItem> from the LINQ, but if you want to iterate through query result, you must use foreach.

IEnumerable<SPListItem> LiColl = from SPListItem Li in Lhr.Items where Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days <= 0 &&
Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days >= (_DayIndex * -1) select Li;

Another way, you can get items, using IList<SPListItem> and you can iterate with any iterator.

IList<SPListItem> LiColl2 = (from SPListItem Li in Lhr.Items where Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days <= 0 && Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days >= (_DayIndex * -1) select Li).ToList();

And the other way, you can get items, and returning array of SPListItem.

SPListItem[] LiColl3 = (from SPListItem Li in Lhr.Items where Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days <= 0 && Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days >= (_DayIndex * -1) select Li).ToArray();

Rename Sharepoint Site URL using STSADM

Sometimes you faced a little problem with Sharepoint, and what if the problem is you do not like the existing Site Collection URL? For example, http://intern.contoso.com/sites/HRArea and you want to change it to http://intern.contoso.com/sites/HR ? Try this STSADM Commands…

The method is involving backup, delete and restore command in STSADM, so be careful to do this, and trying not to make any mistakes. First time, you must confirm the old URL and the new URL. Maybe you would like to try my another solution using STSADM, so you don’t need to write the full path of STSADM, coz it so annoying.

STSADM –o backup –url <the old url> –overwrite –filename <the backup filename>

So, the example would be like this, STSADM –o backup –url http://intern.contoso.com/sites/HRArea –overwrite –filename D:\BackupHR.dat. This will make backup for the old URL, including contents.

STSADM –o deletesite –url <the old url>

Make sure that the first command completed successfully, so you can do this command, to delete the old site URL. The example is STSADM –o deletesite –url http://intern.contoso.com/sites/HRArea.

STSADM –o restore –url <the new url> –filename <the backup filename>

And the next command is to restore the backed up site. So the site will run with the new URL. And you’re done….

28 Jul 2010

Easier STSADM Tips by Adding PATH into the Environment Variables

Sometimes you don’t realize that you’re wasting your every seconds just for typing and finding where’s STSADM located. Everytime you remote on a Sharepoint Server, and needs to call STSADM, you type this before calling STSADM…

image

What a waste time…. eh..! You can try to add the path of Sharepoint 12 Folder, to Environment Variables. Here’s how….

image

Click Advanced system settings…

image

Go to tab Advanced and then Environment Variables…

image

in the System Variables, find PATH, and add the 12 BIN sharepoint folder, usually at “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\”, and don’t forget to add “;” delimiters (without quotesBig Grin).

image

Missing Site Actions Menu after Sharepoint Backup or Restore

A couple weeks ago, I ran into a problem, it’s so annoying and stupid. I have a scenario that I must copy an entire Site Collection and restore it on another server. After I tried to backup all of site collection and then restore it on another server, all seems good and nothing’s error found. But when I hitting the front page, and click Site Action, some of menus are missing!

After a few days, I found that both of the source and destination server, are locked by default when you try to backup or restore by using STSADM. and you can check the site whether it’s locked or not by using this command.

image

and this command does unlock the site…

image

And, voila……. the Site Actions menu shown up again….!!!

Merging Two Virtual Hard Disks with Virtual Server 2005

You can merge two Virtual Hard Disk (VHD) into one VHD by using Virtual Server 2005. But as far as I know, you can only merge VHD with Differencing VHD type. What is that? Differencing VHD actually two VHD, with one as the parent which holds the most common and shares the same configuration with the children VHD. And the other one is the child, which holds the changes in every events.

By merging this two VHD, you can increase a better performance for your Virtual Machine. You can try to merge this two hard disk by accessing Virtual Server Administration WebSite, and click Inspect under Virtual Disks.

image

After that, choose the Full Path of the Differencing VHD to Inspect, and click Inspect.

image

Click on the “Merge virtual hard disks”…

image

You can choose whether to merge with the parent virtual disks, or just create a new merged VHD, is up to your choice. And then click Merge.

image

And that’s how to do it…

10 Mar 2010

CPUZ, aplikasi informasi hardware akurat!

Beberapa aplikasi digunakan untuk tools kita membantu mengetahui segala sesuatu, dan membantu kita mempermudah pekerjaan. Salah satunya adalah mengetahui dengan persis tipe, clock processor, jumlah memori, berapa besar memori di setiap slot, dan lain-lainnya. 

Adalah CPUZ versi 1.53, adalah aplikasi untuk menampilkan detail dari hardware komputer bersangkutan. Baik clockspeed processor, codename dari processor, instruksi yang didukung, arsitekturnya apakah 32bit atau 64bit. Cara pakai gak terlalu sulit kok, tinggal jalankan aja. Kamu bisa download di sini.

8 Okt 2009

Pendahuluan Install Sharepoint


Topik :: Pendahuluan Instalasi Sharepoint
Spesifik :: Pendahuluan
Tools :: -
Tujuan :: Memahami instalasi Sharepoint di Windows Vista untuk Developing.
Pada saat sharepoint masih versi 2003, alias Sharepoint Server 2003 dan versi free-nya Windows Sharepoint Services 2.0, environment untuk developing masih menggunakan teknologi virtualisasi untuk melakukan developing. Karena Sharepoint memang tidak didesain untuk di-install di Windows XP atau Windows Vista sekalipun. Sharepoint memang untuk Server.

Tapi selalu ada banyak cara untuk melakukan instalasi Sharepoint di atas Windows non-Server. Bamboo Solutions menyediakan installer free untuk melakukan Instalasi Sharepoint di atas Windows Vista. Pasti dengan beberapa requirement tertentu seperti yang dimiliki oleh server. Kamu bisa lakukan langkah-langkahnya di Blog Bamboo Solutions.

Beberapa hal yang harus diperhatikan dan diperlukan di komputer Vista kamu, yaitu ada 4 poin.



  1. Install dulu SQL Server 2005 (either SQL Server 2005 Express atau yang standard / berbayar)
  2. Hanya bisa memilih Advanced Setup pada saat instalasi
  3. Mengaktifkan fitur-fitur yang ada di IIS (penjelasan ada di webBlog Bamboo Solutions tadi)
  4. Melakukan instalasi Visual Studio 2005 Professional
  5. Melakukan instalasi Visual Studio Extension WSS versi 1.1 (untuk Visual Studio 2005)


SQL Server diperlukan sebelum kita melakukan instalasi WSS 3.0 atau MOSS 2007 di Windows Vista. Kamu bisa memakai SQL Server 2005 versi berbayar dari Microsoft, ato pake versi Free SQL Server 2005. Yang jelas, pada saat instalasi SQL Server 2005 tersebut, DIWAJIBKAN memilih Advanced Setup mode untuk memilih menggunakan Mixed Mode authentication. Dengan cara ini, jika Windows Authentication mode itu fail, kamu masih bisa menggunakan SQL Authentication, dan sebaliknya. Hanya itu saja yang kritis di dalam instalasi SQL Server 2005. Ini untuk future enhancements.

Nah, pada saat instalasi MOSS 2007 atau WSS 3.0, kita musti pilih Advanced Setup > Web Front End. Hindari Instalasi Stand Alone, karena semua component terinstall sudah dipacking dan dikonfigurasikan, nantinya akan menyulitkan konfigurasi. Kelemahannya, kamu gak bisa lagi menambah Server utk ditambahkan dalam Server Farm. Nanti akan aku bahas di posting berikutnya tentang instalasi.

17 Sep 2009

Sebuah Misteri dari Sharepoint System Account



Topik :: Tools for System Engineer
Spesifik :: Windows Sharepoint Services
Tools :: None
Tujuan :: Mengetahui apa itu System Account
Sharepoint System Account? Hmm, User ini ada di Windows Sharepoint Services. By Default, kalo kita login menggunakan User yang digunakan sebagai Sharepoint System Account Administrator saat instalasi Sharepoint, maka saat kita login ke situs manapun, username yang tertampil, bukan lagi Username yang bersangkutan.

Let's say aku pake user RADIT\raditz, dimana property Name dari user itu adalah Radityo Ardi. Pada saat login di salah satu situs under Sharepoint, akan tertulis seperti di
 gambar, System Account.

Pada saat aku melakukan coding untuk retrieve gambar si user ini, error terjadi, bahwa user ini tidak ketemu. Tapi bagaimana bisa berubah menjadi System Account dengan login name SHAREPOINT\System?

Here's it how. Pada saat Sharepoint menerima login user, Sharepoint akan melakukan cek, apakah login-nya sama dengan Identity yang digunakan di Application Pool. Jika sama, maka System Account-lah yang muncul.