Mục đích của bài viết là tổng hợp các Powershell script thông dụng và cả không thông dụng nhưng rất hửu ích để can thiệp vào một số thành phần cài đặt của hệ thống. Hay chỉ là những cài đặt đơn giản nhưng bạn sẽ mất nhiều thời gian khi tao tác bằng tay.
I. Một số điều cần biết về powershell
PowerShell là gì? Để hiểu hơn về PowerShell, đầu tiên chúng ta cần biết Shell là gì. Dĩ nhiên nó là vở sò rồi :D. Và trong khoa học máy tính, shell cơ bản là một giao diện người dùng cho phép bạn qua đó có thể truy cập vào các dịch vụ khác nhau của hệ điều hành. Shell có thể là một/một tập các dòng lệnh hoặc nó có thể bao gồm một giao diện người dùng đồ họa (GUI). PowerShell hay đầy đủ là Windows PowerShell (gọi tắt PS), PS cũng là một shell được phát triển trên nền .NET framework; Nó bao gồm một Shell các dòng lệnh và ngôn ngữ script có cấu trúc. Bề nỗi của PS mà bạn có thể tương tác đó là phần Windows PowerShell ISE – cho phép bạn viết, quản lý các lệnh hay tập các lệnh/script và thực thi chúng.

Posershell ISE
Phiên bản đầu tiên của PS được phát hành vào tháng 11 năm 2006 cho Windows XP, Windows Server 2003 và Windows Vista. Phiên bản mới nhất của PS là Windows PowerShell 6.0.0 Beta 3. Phiên bản ổn định nhất là 5.1.x (cập nhật đến khi viết bài – Windows 10 v1703).
Trên Windows 10 Creators, Microsoft đã thay thế CMD bằng Powershell trong thanh truy cập nhanh. Đối với một quản trị viên máy tính, có thể đây là một sự thay đổi tích cực. Không ai phủ nhận được rằng Powershell làm được tất cả những gì CMD có thể làm và hơn thế nữa.
II. Tổng hợp các Powershell script
1) Lấy thông tin version của Powershell.
$PSVersionTable
2) Nối các đường dẫn và tập tin. Lệnh sau giống hàm Path.Combine trong C#.
$filmPath = Join-Path -Path "C:\Media" -ChildPath "Films\E chua 18.mp4"
Write-Output "Film: $($filmPath)"
$filmPath = Join-Path -Path "C:\Media\Films" -ChildPath "E chua 18.mp4"
Write-Output "Film: $($filmPath)"

Path.Combine trong Powershell
3) Lấy thư mục cha của thư mục hiện tại.
$mediaDir = Split-Path -Path "C:\Media\Films"
Write-Output "Film DIR: $($mediaDir)"
4) Cho phép chạy PS script (Enable powershell script).
Mặc định bạn chỉ có thể chạy các command của powershell trên cử sổ dòng lệnh hoặc trên Powershell ISE. Lỗi thường thấy là: .ps1 cannot be loaded because running scripts is disabled on this system.
Để có thể thực thi các tập tin script(*.ps1) của PS. Bạn cần chạy đoạn mã sau bằng cách lưu chúng thành một tập tin có đuôi .bat.
Echo Enable PS script
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /f /v "ExecutionPolicy" /t REG_SZ /d "Unrestricted"
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /f /v "Path" /t REG_SZ /d "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe"
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /f /v "ExecutionPolicy" /t REG_SZ /d "Unrestricted"
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /f /v "Path" /t REG_SZ /d "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe"
5) Lấy thư mục chứa PS script đang thực thi.
$workingScript = $PSScriptRoot
Write-Output "Script DIR: $($workingScript)"
6) Start/Stop/Get thông tin một Wondows service.
$serviceNAme = "WinRM" # Windows Remote Management (WS-Management)
# - Stop service
Stop-Service $serviceNAme
Get-Service $serviceNAme
Write-Output "The service is stopped"
# - Start service
Start-Service $serviceNAme
Get-Service $serviceNAme
Write-Output "The service is running"
7) Include nội dung của một file scipt khác vào script hiện tại.
function.ps1
Function Log-Out {
param($content)
Write-Host $content
}
main.ps1
$commonFunction = "C:\Users\phamt\Downloads\PSScript Studying\PSScript\function.ps1"
# Include another script
. "$($commonFunction)"
# Call API in function.ps1
Log-Out "This is log content"
8) Mở tất cả các Port trong Firewall (Hãy cẩn thận).
$inBoundTCPRule = "Allow All port TCP Inbound"
# Check "Allow All port TCP Inbound"
$rule = Get-NetFirewallRule -DisplayName $inBoundTCPRule -ErrorAction SilentlyContinue
if($rule -eq $null)
{
Log-Out "$($inBoundTCPRule) isn't existed. Create new rule."
New-NetFirewallRule -DisplayName $inBoundTCPRule -Name $inBoundTCPRule -Direction Inbound –LocalPort Any -Protocol TCP -Action Allow
}
$outBoundTCPRule = "Allow All port UDP Outbound"
# Check "Allow All port UDP Outbound"
$rule = Get-NetFirewallRule -DisplayName $outBoundTCPRule -ErrorAction SilentlyContinue
if($rule -eq $null)
{
Log-Out "$($outBoundTCPRule) isn't existed. Create new rule."
New-NetFirewallRule -DisplayName $outBoundTCPRule -Name $outBoundTCPRule -Direction Outbound –LocalPort Any -Protocol TCP -Action Allow
}
9) Cho phép remote thông qua powershell.
Enable-PSRemoting -Force
Nếu thực thi đoạn lệnh trên mà gặp lỗi như hình sau đây. Nhớ thay đổi Network type thành Domain hoặc Provate. Xem tại đây.
Đang cập nhật...
I’m not sure where you’re getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for fantastic information I was looking for this information for my mission.|
Hello. I see that you don’t update your site too often. I know that writing articles is time consuming and boring.
But did you know that there is a tool that allows you to create new posts using existing content
(from article directories or other blogs from your niche)?
And it does it very well. The new posts are unique and pass the copyscape
test. Search in google and try: miftolo’s tools
Some truly interesting details you have written.Helped me a lot, just what I was searching for : D.