Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Máy Chủ Windows

Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Hầu hết module PowerShell phổ biến được cài đặt online từ kho lưu trữ PowerShell Gallery chính thức (PSGallery) dùng lệnh Install-Module. Tuy nhiên, bạn không thể cài module PowerShell nếu máy tính ở mạng biệt lập hoặc truy cập trực tiếp PSRepository bị hạn chế. Tương tự khi bạn cố cài module PowerShell trên host Windows Server, thường chặn truy cập Internet trực tiếp. Trong bài này, chúng ta chỉ cách cài module PowerShell offline và import module từ máy tính từ xa (ví dụ khi dùng module quản lý SQLServer PoSh).

Lưu ý không có link tải trực tiếp module từ PowershellGallery.com. Chỉ có thể tải gói NuGet (file .nupkg) từ trang web. Bạn có thể cài module PowerShell từ file NUPKG, nhưng đây không phải cài đặt module hoàn chỉnh (vấn đề chính là dependency không được giải quyết).

Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Cách cài Module PowerShell trên máy tính Offline?

Trước hết, cài module PowerShell bạn cần trên máy tính có Internet.

PowerShell phiên bản 5.1 hoặc mới hơn phải cài trên máy tính:

$PSVersionTable.PSVersion

Đảm bảo module tồn tại trong PSGallery:

Find-Module –Name *SqlServer*| Select Name, Version, Repository
Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Tải module về thư mục local chỉ định trên máy tính:

Save-Module –Name SqlServer –Path C:\PS\
Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Copy thư mục sang máy tính khác bạn muốn cài module.

Xem module PowerShell lưu ở thư mục nào:

$env:PSModulePath -split ";"
Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Như bạn thấy, module PowerShell nằm ở một trong các đường dẫn sau:

  • C:\Users\username\Documents\WindowsPowerShell\Modules ($Home\Documents\PowerShell\Modules) – module ở thư mục này chỉ khả dụng cho user này (CurrentUser);
  • C:\Program Files\WindowsPowerShell\Modules ($Env:ProgramFiles\WindowsPowerShell\Modules) — đường dẫn dùng cài module cho tất cả user máy tính (-Scope AllUsers);
  • C:\Windows\system32\WindowsPowerShell\v1.0\Modules – thư mục mặc định cho module tích hợp sẵn.

Copy module vào C:\Program Files\WindowsPowerShell\Modules.

Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Đảm bảo module SQLServer hiện có sẵn:

Get-Module -Name SQLServer -ListAvailable
Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Bạn có thể lấy thư mục module như sau:

(Get-Module -ListAvailable SQLServer).path
Cài đặt Module PowerShell ở chế độ Offline (không có Internet)

Hiển thị danh sách lệnh có sẵn trong module:

Get-Command -Module SQLServer

Cùng cách đó, bạn có thể cài bất kỳ module nào. Tôi thường dùng cách này cài SQLServer, PSWindowsUpdate, hoặc PowerCLI cho VMWare module.

Vì lý do rõ ràng, không có ý nghĩa cài module AzureAD và Exchange Online PowerShell trên thiết bị offline.

Import Module PowerShell từ máy tính từ xa

Nếu không muốn cài module PowerShell trên tất cả máy tính, bạn có thể import bất kỳ module nào từ máy tính từ xa dùng PSRemoting:

$session = New-PSSession -ComputerName dub-sql1

Để hiển thị danh sách module cài trên máy tính từ xa:

Get-Module -PSSession $session –ListAvailable

Để import module PowerShell chỉ định về máy tính của bạn:

Import-Module -PSsession $session -Name SqlServer

Đừng quên đóng session khi xong:

Remove-PSSession $session

Cách thú vị khác để dùng local module PowerShell cài trên máy tính từ xa qua Implicit remoting.

Kết nối máy tính từ xa dùng Invoke-Command và import module PowerShell bạn muốn:
$session = New-PSSession -ComputerName dub-sql1
Invoke-Command {Import-Module SqlServer} -Session $session

Xuất cmdlet module từ phiên từ xa sang module local:

Export-PSSession -Session $s -CommandName *-Sql* -OutputModule RemoteSQLServer -AllowClobber

Lệnh tạo module PowerShell RemoteSQLServer mới trên máy tính của bạn (trong C:\Program Files\WindowsPowerShell\Modules). Các file cmdlet không được copy.

Đóng session:

Remove-PSSession $session

Sau đó để dùng cmdlet PowerShell từ module này, chỉ cần import vào phiên:

Import-Module RemoteSQLServer

Tất cả cmdlet MSSQL sẽ có sẵn mà không cần thiết lập kết nối tường minh đến máy tính từ xa. Thử truy vấn database MS SQL dùng Invoke-Sqlcmd. Tất cả lệnh MSSQL khả dụng cho đến khi bạn đóng console PowerShell hoặc gỡ module.