一、概述
Cmdlets 用於服務器的管理方面主要體現在4個方面:服務、日志、進程、服務器管理器。
1、服務
• Get-Service。查看某個服務的屬性。
• New-Service。創建一個新的服務。
• Restart-Service。重啟一個已有的服務。
• Resume-Service。使一個暫停的服務繼續運行。
• Set-Service。配置某個服務的屬性。
• Start-Service。啟動一個已停止的服務。
• Stop-Service。停止一個正在運行的服務。
• Suspend-Service。掛起一個服務。
2、日志
• Get-EventLog。顯示某個事件日志里的事件。
• Clear-EventLog。刪除某個事件日志里的所有記錄。
• Limit-EventLog。設置事件日志的區間和文件大小限制。
• New-EventLog。在運行Windows Server的計算機上創建一個新的事件日志和事件源。
• Remove-EventLog。刪除一個自定義的事件日志,並將此事件日志的所有的事件源刪除注冊。
• Show-EventLog。顯示某台計算機的事件日志。
• Write-EventLog。允許你寫事件到某個事件日志。
3、進程
• Get-Process。獲得某個進程的信息。
• Start-Process。啟動某個進程。
• Stop-Process。停止某個進程。
• Wait-Process。在接受輸入之前等待某個進程停止。
• Debug-Process。附加一個debugger 到某個或某些正在運行的進程上。
4、服務器管理器
• Get-WindowsFeature
• Install-WindowsFeature
• Remove-WindowsFeatre
更多操作,參考官網 http://technet.microsoft.com/zh-cn/library/dd315367.aspx
二、示例1:為服務器“添加功能”
1、加載ServerManager模塊
Windows Server 2008 R2 的 ServerManager 模塊位於PowerShell安裝路徑下的Modules文件夾。
Windows PowerShell
版權所有 (C) 2009 Microsoft Corporation。保留所有權利。
PS C:\Users\Administrator> Import-Module ServerManager
PS C:\Users\Administrator>
注:僅對此進程有效。下次進入PowerShell 時,需要重新加載。
2、瀏覽現有的角色、角色服務和功能
PS C:\Users\Administrator> Get-WindowsFeature
Display Name Name
------------ ----
[ ] Active Directory Rights Management Services ADRMS
[ ] Active Directory 權限管理服務器 ADRMS-Server
[ ] 聯合身份驗證支持 ADRMS-Identity
[ ] Active Directory 聯合身份驗證服務 AD-Federation-Services
[ ] 聯合身份驗證服務 ADFS-Federation
[ ] 聯合身份驗證服務代理 ADFS-Proxy
[ ] AD FS Web 代理 ADFS-Web-Agents
[ ] 聲明感知代理 ADFS-Claims
[ ] 基於 Windows 令牌的代理 ADFS-Windows-Token
。。。
3、添加功能
本例中,添加“Windows Server Backup 功能”。如果在圖形界面中,很容易操作。
在PowerShell中,運行以下命令:
PS C:\Users\Administrator> Add-WindowsFeature Backup
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Windows Server Backup}
4、確認 Windows Server Backup 功能已經安裝
------------ ----
[X] Windows Server Backup Backup
5、刪除 Windows Server Backup 功能
PS C:\Users\Administrator> Remove-WindowsFeature Backup
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Windows Server Backup}
三、示例2:管理IIS
1、檢查可用的模塊
在 Windows Server 2008 R2 通過“服務器管理器”中“添加角色”安裝了 IIS7.5 之后。運行以下命令,檢查WebAdministration模塊是否已經安裝。
PS C:\Users\Administrator> Get-Module -ListAvailable
ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest ADRMS {}
Manifest AppLocker {}
Manifest BestPractices {}
Manifest BitsTransfer {}
Manifest PSDiagnostics {}
Manifest ServerManager {}
Manifest TroubleshootingPack {}
Manifest WebAdministration {}
注:Windows Server 2008 安裝了 IIS7.0 之后,需要下載安裝 PowerShell Snap-In For IIS7.0 。http://www.iis.net/downloads/microsoft/powershell
2、加載Web管理模塊
PS C:\Users\Administrator> Import-Module WebAdministration
3、查看可用的命令
PS C:\Users\Administrator> Get-Command -pssnapin WebAdministration
CommandType Name Definition
----------- ---- ----------
Cmdlet Add-WebConfiguration Add-WebConfiguration [-Filter] <String[]> [[-PSP...
Cmdlet Add-WebConfigurationLock Add-WebConfigurationLock [-Filter] <String[]> [[...
Cmdlet Add-WebConfigurationProperty Add-WebConfigurationProperty [-Filter] <String[]...
Cmdlet Backup-WebConfiguration Backup-WebConfiguration [-Name] <String> [-Verbo...
Alias Begin-WebCommitDelay Start-WebCommitDelay
Cmdlet Clear-WebConfiguration Clear-WebConfiguration [-Filter] <String[]> [[-P...
Cmdlet Clear-WebRequestTracingSettings Clear-WebRequestTracingSettings [[-Name] <String...
Cmdlet ConvertTo-WebApplication ConvertTo-WebApplication [[-PSPath] <String[]>] ...
。。。
4、操作 IIS
加載了WebAdministration 模塊之后,PowerShell 環境建立了一個“IIS:\”命名空間。進入這個命名空間,並查看IIS的信息。可以用New-Item 創建文件夾、網站、應用池、虛擬目錄,或者用 Remove-Item 進行刪除。
PS C:\Users\Administrator> IIS:
PS IIS:\> dir
Name
----
AppPools
Sites
SslBindings
四、故障排除
1、“在此系統中禁止執行腳本”
PS C:\Users\Administrator> Get-Module -ListAvailable | Import-Module
Import-Module : 無法加載文件 C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDiagnostics\PSDiagnostics.psm1,因為在此系統中禁止執行腳本。有關詳細信息,請參閱 "get-help about_signing"。
所在位置 行:1 字符: 42
+ Get-Module -ListAvailable | Import-Module <<<<
+ CategoryInfo : NotSpecified: (:) [Import-Module], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.ImportModuleCommand
【原因】權限不足
【對策】當前的權限一共有4種:受限的(Restricted)、僅本地運行(RemoteSigned)、不受限的(Unrestricted)、所有(AllSigned)。建議改為RemoteSigned 。
PS C:\Users\Administrator> Get-ExecutionPolicy
Restricted
PS C:\Users\Administrator> Set-ExecutionPolicy RemoteSigned
執行策略更改
執行策略可以防止您執行不信任的腳本。更改執行策略可能會使您面臨 about_Execution_Policies
幫助主題中所述的安全風險。是否要更改執行策略?
[Y] 是(Y) [N] 否(N) [S] 掛起(S) [?] 幫助 (默認值為“Y”): y
PS C:\Users\Administrator> Import-Module WebAdministration
如果在ps1腳本中不允許交互,則需要強制修改權限。例如
PS C:\Users\Administrator> Set-ExecutionPolicy Remotesigned -Force
四、ServerManageCmd
1、概述
PS C:\Users\Administrator> servermanagercmd
Servermanagercmd.exe 已被棄用,不保證在將來版本的 Windows 中支持它。建議使用可用於服務器管理器的 Windows PowerShell cmdlet。
用法:
ServerManagerCmd.exe
安裝和刪除角色、角色服務和功能。也顯示所有可用的角色、角色服務和功能列表,並顯示在此計算機上安裝了其中哪些內容。有關可以使用此工具指定的角色、角色服務和功能的詳細信息,請參閱服務器管理器的“幫助”。
-query [<query.xml>] [-logPath <log.txt>]
-install <名稱>
[-resultPath <result.xml> [-restart] | -whatIf] [-logPath <log.txt>]
[-allSubFeatures]
-remove <名稱>
[-resultPath <result.xml> [-restart] | -whatIf] [-logPath <log.txt>]
。。。
實際上,ServerManageCmd 是一個命令提示符下面即可運行的命令。在 Windows Server 2008 操作系統有很多的“粉絲”。而在 Windows Server 2008 R2 操作系統,官方推薦為:棄用 ServerManageCmd ,改用 PowerShell cmdlet 。
2、示例
以安裝SNMP服務為例。
C:\Users\Administrator>ServerManagerCmd.exe -install SNMP-service
........
開始安裝...
[Installation] 成功: [SNMP 服務] SNMP 服務。
<100/100>
成功: 安裝成功。
效果等同於圖形界面中“添加功能”。