Posts Tagged PowerShell

Quản trị SharePoint 2010 với Windows PowerShell

View English version

Một tính năng mới được chào đón trong SharePoint 2010 là khả năng hỗ trợ Windows PowerShell. Giờ đây, các nhà quản trị đã có một công cụ mạnh mẽ để tự động hóa các tác vụ hằng ngày bằng cách viết các đoạn script có khả năng tái sử dụng. Mặc dù điều này nghe có vẻ hấp dẫn, nó cũng ít nhiều gây bỡ ngỡ cho những người vốn đã quen sử dụng stsadm.exe trong SharePoint 2007 trước đây. Tuy nhiên, bạn có thể yên tâm rằng những lợi ích mà nó đem lại hoàn toàn vượt xa những rắc rối, và một khi đã biết PowerShell, bạn sẽ bị nó lôi cuốn ngay! Bài viết sau đây xin trình bày những nét căn bản về Windows PowerShell, nhằm giúp cho mọi người dễ dàng tiếp cận công cụ mới đầy uy lực này.

1. Cửa sổ console Windows PowerShell

Điều đầu tiên mà ta cần làm là xác định vị trí mở Windows PowerShell. Trên một máy Windows 7, PowerShell đã được cài đặt mặc định và có thể truy xuất từ đường dẫn sau:

Start\All Programs\Accessories\Windows PowerShell\Windows PowerShell

Bạn cũng có thể sử dụng tổ hợp phím tắt Windows + R và gõ ”powershell.exe” vào hộp thoại Run.

Cửa sổ Windows PowerShell sẽ hiện lên với hình dáng tương tự như một cửa sổ CMD truyền thống trước đây.

2. Các lệnh cơ bản

Các lệnh DOS phổ biến (như cd, dir, cls…) đều có thể sử dụng được trong Windows PowerShell. Hãy thử gõ một vài lệnh và xem kết quả:

Ngoài ra, Windows PowerShell cung cấp một thư viện với nhiều lệnh mới hữu dụng. Có thể kể đến một số lệnh thường gặp như:

+ Get-Help: hiển thị thông tin trợ giúp về cách sử dụng một lệnh nào đó
Vd: Get-Help dir

+ Set-Content: ghi nội dung ra thiết bị (chẳng hạn như một tập tin). Nếu tập tin đó đã có, nội dung của nó sẽ bị ghi đè.
Vd: Set-Content –path C:\document.txt –value “Welcome to Windows PowerShell!”

+ Get-Content: lấy nội dung theo đường dẫn xác định.
Vd: Get-Content C:\document.txt

+ Get-Process: trả về các tiến trình đang chạy trên máy cục bộ hoặc một máy tính từ xa.

Thú vị là, Windows PowerShell cũng có thể đóng vai trò của một chiếc máy tính đơn giản. Để minh họa, hãy thử nhập:

(2+2+3*4)/2

và xem kết quả:

Tuy nhiên, sức mạnh thực sự của PowerShell nằm ở khả năng nối các câu lệnh lại thành một chuỗi liên tục, một khái niệm được gọi là ”pipelining”. Mọi câu lệnh đều trả về một đối tượng hoặc một tập hợp các đối tượng. Nói một cách dễ hiểu thì pipelining cho phép ta lấy kết quả từ một câu lệnh và truyền nó vào làm tham số cho câu lệnh tiếp theo. Bạn có thể tạo một chuỗi lệnh với toán tử | như sau:

Get-ChildItem | Where-Object {$_.Name -like "d*"}

Trong ví dụ trên, lệnh Get-ChildItem trả về một danh sách các tập tin trong thư mục hiện tại. Danh sách này sau đó được truyền làm tham số cho lệnh Where-Object, lệnh này lọc các tập tin trong danh sách và chỉ trả về những tập tin nào có tên bắt đầu bằng chữ ”d”.

3. SharePoint snap-in cho PowerShell

Windows PowerShell cung cấp khả năng mở rộng bằng cách cho phép các bên thứ ba xây dựng các thư viện riêng của họ theo dạng snap-in. Vì vậy không ngạc nhiên là, SharePoint 2010 cũng đi kèm với một snap-in có tên Microsoft.SharePoint.PowerShell nhằm hỗ trợ các chức năng chuyên biệt. SharePoint 2010 thậm chí còn cung cấp một phiên bản tùy biến của Windows PowerShell console được biết đến dưới cái tên SharePoint 2010 Management Shell. Khác biệt chính nằm ở chỗ SharePoint 2010 Management Shell sẽ tự động nạp snap-in Microsoft.SharePoint.PowerShell lúc khởi động trong khi phiên bản Windows PowerShell chuẩn thì không. Dù gì đi nữa, tốt nhất là bạn nên luôn luôn nạp snap-in này một cách tường minh trước khi sử dụng.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

Lưu ý dòng lệnh trên có một tham số -ErrorAction với giá trị ”SilentlyContinue”. Tham số này chỉ thị cho PowerShell bỏ qua thông báo lỗi trong trường hợp snap-in đã được load trước đó. Sau khi xong thủ tục này, bạn đã sẵn sàng để quản trị SharePoint thông qua Windows PowerShell. Hãy nghịch một tí nào:

+ Để tạo một web application mới:
New-SPWebApplication -Name <name> -Port <port> -HostHeader <hostHeader> -URL <url> -ApplicationPool <appPoolName> -ApplicationPoolAccount <appPoolAccount>

+ Để tạo mới một site collection:
New-SPSite -URL <url> -Name <name> -OwnerAlias <owner> -Template <template>

+ Để xóa một site collection:
Remove-SPSite -Identity <identity> -Confirm:<true|false>

+ Để hiển thị danh sách các web application trên server:
Get-SPWebApplication

Phù! Thế là ta đã lướt qua những kiến thức căn bản để làm việc với Windows PowerShell. Còn rất nhiều những tài nguyên quý giá về PowerShell có thể dễ dàng tìm thấy trên Internet. Vậy nên hãy bắt đầu khám phá ngay hôm nay và bạn sẽ thấy rằng chẳng khó khăn gì để trở thành một chuyên gia PowerShell thực thụ. Chúc bạn thành công!

,

Để lại phản hồi

Windows PowerShell for SharePoint 2010 administration

Xem bản tiếng Việt

A welcome new feature in SharePoint 2010 is its support for Windows PowerShell. Administrators now have an effective way to simplify their day-to-day tasks by writing script that is fully automated and reusable. But as compelling as that may sound, it also imposes a learning curve on SharePoint 2007 professionals who have been familiar with using the stsadm tool. However, the advantages still outweigh the troubles, and once you get to know it, you’ll start to appreciate it! This article tries to present you with the fundamentals of working with Windows PowerShell. Now, let’s get started!

1. The Windows PowerShell console

One of the first thing that we need to do is to locate the Windows PowerShell console. On a Windows 7 machine, PowerShell is installed by default and can be accessed from the following path:

Start\All Programs\Accessories\Windows PowerShell\Windows PowerShell

or you can use the shortcut Windows + R and type “powershell.exe” into the run dialog.

The Windows PowerShell console will be displayed with a similar appearance to that of a traditional CMD window.

2. Basic commands

Popular DOS commands (such as cd, dir, cls…) that can be used in a CMD window are also available in Windows PowerShell. So just type in some of them and see the result:

Besides, the built-in Windows PowerShell library also provides a lot of new commands that you may find helpful. To list just a few:

+ Get-Help: display help for a specific command.
Ex: Get-Help dir

+ Set-Content: writes the content to an item (such as a file). If that item already exists, its content will be replaced.
Ex: Set-Content –path C:\document.txt –value “Welcome to Windows PowerShell!”

+ Get-Content: gets the content of the item at the specified location.
Ex: Get-Content C:\document.txt

+ Get-Process: gets the processes that are running on the local computer or a remote computer.

Interestingly, Windows PowerShell can also perform the role of a simple calculator. For instance, try to enter

(2+2+3*4)/2

and see the output:

However, its true power lies in the ability to chain command, a concept called pipelining. Every command returns an object or a collection of objects. Simply speaking, pipelining allows you to take the results of one command and pass it to another one. You create a pipeline by typing a sequence of command separated by the | character.

Get-ChildItem | Where-Object {$_.Name -like "d*"}

Here, the Get-ChildItem command returns a list of files inside the current folder. This list is then passed as input to the Where-Object command which, as its name implies, filters items in the list and return only those whose names begin with “d”.

3. The SharePoint PowerShell snap-in

Windows PowerShell provides extensibility by allowing third parties to plug in their own command library. Unsuprisingly, SharePoint 2010 also comes with a snap-in named Microsoft.SharePoint.PowerShell to support custom functions. It even provides a specialized version of the Windows PowerShell console known as the SharePoint 2010 Management Shell. The main difference is that the SharePoint 2010 Management Shell automatically loads the Microsoft.SharePoint.PowerShell snap-in while the standard Windows PowerShell console does not. Whatever, the best practice here is that you should always load the snap-in explicitly before use.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

Note that this command has a parameter -ErrorAction which is set to “SilentlyContinue”. This tells the command to ignore the error in case the snap-in is already loaded. Now, you’re ready to play with SharePoint using Windows PowerShell. Let’s have some fun:

+ To create a new web application:
New-SPWebApplication -Name <name> -Port <port> -HostHeader <hostHeader> -URL <url> -ApplicationPool <appPoolName> -ApplicationPoolAccount <appPoolAccount>

+ To create a new site collection:
New-SPSite -URL <url> -Name <name> -OwnerAlias <owner> -Template <template>

+ To delete a site collection:
Remove-SPSite -Identity <identity> -Confirm:<true|false>

+ To display a list of web applications on the server:
Get-SPWebApplication

Done! We now have a basic understanding of working with Windows PowerShell. And there’s a lot more resources on the Internet to deepen your knowledge. So start to explore them and you’ll find that it’s not difficult at all to become a Windows PowerShell expert. Good luck!

,

2 phản hồi

Follow

Get every new post delivered to your Inbox.