1、概述shell
Cmdlets 用於服務器的管理方面主要體如今4個方面:服務、日誌、進程、服務器管理器。api
一、服務安全
• Get-Service。查看某個服務的屬性。服務器
• New-Service。建立一個新的服務。工具
• Restart-Service。重啓一個已有的服務。網站
• Resume-Service。使一個暫停的服務繼續運行。spa
• Set-Service。配置某個服務的屬性。操作系統
• Start-Service。啓動一個已中止的服務。.net
• Stop-Service。中止一個正在運行的服務。debug
• Suspend-Service。掛起一個服務。
二、日誌
• Get-EventLog。顯示某個事件日誌裏的事件。
• Clear-EventLog。刪除某個事件日誌裏的全部記錄。
• Limit-EventLog。設置事件日誌的區間和文件大小限制。
• New-EventLog。在運行Windows Server的計算機上建立一個新的事件日誌和事件源。
• Remove-EventLog。刪除一個自定義的事件日誌,並將此事件日誌的全部的事件源刪除註冊。
• Show-EventLog。顯示某臺計算機的事件日誌。
• Write-EventLog。容許你寫事件到某個事件日誌。
三、進程
• Get-Process。得到某個進程的信息。
• Start-Process。啓動某個進程。
• Stop-Process。中止某個進程。
• Wait-Process。在接受輸入以前等待某個進程中止。
• Debug-Process。附加一個debugger 到某個或某些正在運行的進程上。
四、服務器管理器
• Get-WindowsFeature
• Install-WindowsFeature
• Remove-WindowsFeatre
更多操做,參考官網 http://technet.microsoft.com/zh-cn/library/dd315367.aspx
2、示例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 時,須要從新加載。
二、瀏覽現有的角色、角色服務和功能
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}
四、確認 Windows Server Backup 功能已經安裝
五、刪除 Windows Server Backup 功能
PS C:\Users\Administrator> Remove-WindowsFeature Backup
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Windows Server Backup}
3、示例2:管理IIS
一、檢查可用的模塊
在 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
二、加載Web管理模塊
PS C:\Users\Administrator> Import-Module WebAdministration
三、查看可用的命令
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[]>] ...
。。。
四、操做 IIS
加載了WebAdministration 模塊以後,PowerShell 環境創建了一個「IIS:\」命名空間。進入這個命名空間,並查看IIS的信息。能夠用New-Item 建立文件夾、網站、應用池、虛擬目錄,或者用 Remove-Item 進行刪除。
PS C:\Users\Administrator> IIS:
PS IIS:\> dir
Name
----
AppPools
Sites
SslBindings
4、故障排除
一、「在此係統中禁止執行腳本」
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
4、ServerManageCmd
一、概述
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 。
二、示例
以安裝SNMP服務爲例。
C:\Users\Administrator>ServerManagerCmd.exe -install SNMP-service
........
開始安裝...
[Installation] 成功: [SNMP 服務] SNMP 服務。
<100/100>
成功: 安裝成功。
效果等同於圖形界面中「添加功能」。