Powershell Cmdlet Powershell Powershell Windows Powershell
11
PowerShell
Cmdlet
PowerShell
PowerShell
Windows PowerShell PowerShell PowerShell Windows Server 2008 Windows Server 2008 Windows Server 2008 PowerShell Windows Server 2008
PowerShell 1.0 Windows Server 2008 PowerShell Windows PowerShell Windows XP Windows Server 2003 Windows Vista PowerShell http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx
PowerShell Shell Shell Shell
Shell CD
Shell
Windows Windows Shell Windows Shell Shell Windows Windows Shell Shell Shell 8 Windows Server 2008 Server Core Windows Shell Shell Graphical User Interface GUI Command Line Interface CLI Shell
Windows PowerShell Shell CD
Shell
PowerShell .NET Framework Windows Server 2008 Server Core PowerShell Windows Server 2008 .NET Framework
PowerShell PowerShell .Net Framework Windows PowerShell WMI ADSI CDO Windows PowerShell
PowerShell PowerShell
286 PowerShell
PowerShell
PowerShell
Windows Server 2008 PowerShell PowerShell PowerShell
PowerShell PowerShell
Exchange Server 2007 System Center :Operations Manager Virtual Machine Manager PowerShell PowerShell cmdlet Quest Software PowerShell PowerShell
PowerShell Windows Server 2008 Windows Server 2008 PowerShell PowerShell
PowerShell
System Center System Center https://www.microsoft.com/china/systemcenter/
Quest Software Windows Quest software http://www.quest.com/
287 11-1
11-1
Windows PowerShell PowerShell 11-2
11-2 Windows PowerShell
288 PowerShell
Windows PowerShell PowerShell PowerShell 11-3
11-3 PowerShell
PowerShell Administrator Windows PowerShell PowerShell 11-4
11-4 PowerShell
PowerShell powerShell PowerShell powerShell PowerShell telnet telnet powerShell PowerShell
289
PowerShell exit PowerShell
PowerShell PowerShell PowerShell Windows cmd.exe PowerShell PowerShell Windows Reg.exe Netsh PowerShell dir cd
PowerShell cmdlet command-let cmdlet Shell
cmdlet - - cmdlet Get-Process cmdlet get process cmdlet cmdlet cmdlet NTFS cacls acls aclquery PowerShell Get-Acl PowerShell cmdlet netsh cmdlet PowerShell
PowerShell 129 cmdlet cmdlet PowerShell cmdlet
PowerShell cmdlet PowerShell cmdlet cmdlet cmdlet cmdlet Get-Command cmdlet PowerShell
290 PowerShell
Get-Command 11-5
11-5 cmdlet
PowerShell cmdlet cmdlet
cmdlet cmdlet - cmdlet
cmdlet 11-6
11-6 cmdlet
-Name Test* Test cmdlet
cmdlet cmdlet -? cmdlet -? cmdlet cmdlet 11-7 cmdlet cmdlet -?
291
11-7 cmdlet
cmdlet cmdlet 11-1 PowerShell cmdlet cmdlet
表 11-1 PowerShell 内置的常用 cmdlet
cmdlet Compare-Object Copy-Item Copy-ItemProperty ForEach-Object Format-List Format-Table Format-Wide Get-Acl Get-Alias Get-ChildItem Get-Command cmdlet Windows PowerShell Get-Content Get-Credential Get-Date Get-EventLog Get-ExecutionPolicy Get-Help Windows PowerShell cmdlet Get-Item Get-ItemProperty
292 PowerShell
续表
cmdlet Get-Location Get-Member Get-Process Get-PSDrive Windows PowerShell Get-Service Get-WmiObject WMI Import-Alias Move-Item Move-ItemProperty New-Item New-ItemProperty New-Object .NET COM New-Variable Out-File Out-Host Read-Host Remove-Item Remove-ItemProperty Rename-Item Windows PowerShell Rename-ItemProperty Resolve-Path Restart-Service Set-Alias Windows PowerShell cmdlet Set-Content Set-ExecutionPolicy Set-Item Set-ItemProperty Set-Location Set-Service Set-Variable Sort-Object Start-Service Stop-Process Stop-Service Where-Object
cmdlet cmdlet
293 PowerShell cmdlet PowerShell
Tab cmdlet PowerShell
Get-Command –CommandType “cmdlet”
get-co Tab PowerShell Get-Command -c Tab PowerShell -CommandType Tab 33 17
cmdlet Tab PowerShell cmdlet get-c Tab PowerShell Get-ChildItem Get-Command Get-Content Get-Credential Get-Culture Get-C
Tab cmdlet
'C:\Program Files\Microsoft SQL Server\90\EULA\License_SSEE_CHS.txt'
c:\pro[tab 键]\m [tab 键]\9[tab 键]\e[tab 键]\l[tab 键]
PowerShell PowerShell
1
Get-Command -Na "test*"
2
Get-Command -Name "test*"
Get-Command -Na -Name -N
294 PowerShell
-Noun
PowerShell
11.2.1 使用管道连接 cmdlet
cmdlet
cmdlet PowerShell
PowerShell cmdlet cmdlet cmdlet cmdlet PowerShell | cmdlet cmdlet Get-Process Stop-Process cmdlet |
Get-Process -ProcessName "Notepad" | Stop-Process
Get-Process cmdlet Notepad Stop-Process cmdlet cmdlet Notepad Stop-Process
cmdlet cmdlet 3 cmdlet cmdlet cmdlet
Get-ChildItem C:\Users | ForEach-Object {$_.Name + " " + $_.Mode} | Out-File C:\tmp\Users.txt
C:\Users Users.txt
11.2.2 面向对象的命令界面
Windows UNIX Shell
295 | PowerShell
Shell PowerShell PowerShell cmdlet
PowerShell Shell PowerShell Shell
PowerShell .NET Framework Shell
PowerShell PS
Shell cmdlet PowerShell
cmdlet cmdlet Get-Member Get-Member 11-8
11-8
296 PowerShell
Get-Process "Notepad" Notepad Get-Member Get-Member
Get-Member .NET Framework System.Diagnostics.Process MSDN http://msdn.microsoft.com 11-9
11-9 MSDN
PowerShell
cmdlet PowerShell
$p = Get-Process -Name “Notepad”
PowerShell $ Get-Process cmdlet Notepad $p
$p.Name
297
$p.Kill()
Kill() PowerShell 11-10
11-10
C#
cmdlet cmdlet $_ cmdlet
Get-ChildItem | Foreach-Object {$_.FullName} Get-ChildItem Foreach-Object cmdlet {} {} $_ .FullName 11-11
11-11
298 PowerShell
11.2.3 格式化输出结果
cmdlet Shell PowerShell cmdlet cmdlet cmdlet cmdlet
PowerShell cmdlet 11-2
表 11-2 格式输出 cmdlet
格式 cmdlet 功能 Format-List Format-Custom Format-Table Format-Wide
Get-Process
Get-Process –Name “Notepad” | Format-List
Get-Process cmdlet Notepad Format-List cmdlet
Get-Process -Name “Notepad” | Format-List Name, ID, CPU
11-12
11-12
Format-Table cmdlet 11-13
299
11-13
11.2.4 使用 PowerShell 别名
PowerShell dir cd cat grep PowerShell PowerShell PowerShell cmdlet dir Get-ChildItem cmdlet PowerShell dir rm pwd Shell Windows UNIX Shell PowerShell PowerShell 11-3 Get-Alias cmdlet
表 11-3 PowerShell 中内置的别名
cat dir Mount Rm cd echo Move Rmdir chdir erase Popd Sleep cls history Pushd Tee copy kill Pwd Type del lp Clear write diff ls Ren
cmdlet cmdlet PowerShell Set-Alias PowerShell nsvc Get-Service cmdlet 11-14
nsvc alias:nsvc PowerShell
remove-item alias: nsvc
300 PowerShell
11-14 PowerShell
PowerShell PowerShell PowerShell PowerShell PowerShell
11.2.5 使用函数
cmdlet cmdlet PowerShell PowerShell PowerShell
function <函数名> {语句… }
hosts hosts
Get-Content C:\Windows\system32\drivers\etc\hosts
11-15
11-15
301 11.2.6 PowerShell 驱动器
PowerShell
PowerShell cd Set-Location dir Get-Childitem cmdlet Tab PowerShell Item cmdlet Get-Item Get-Childitem New-Item Remove-Item Set-Item Move-Item PowerShell
Get-PSDrive cmdlet PowerShell 11-16
11-16
HKEY_CURRENT_USER HKCU HKCU cd HKCU: PowerShell cd dir 11-17
11-17
302 PowerShell
cd env: PowerShell 11-18
11-18
PowerShell
11.2.7 获取帮助信息
PowerShell PowerShell cmdlet -? cmdlet cmdlet Get-Help PowerShell
Get-Help PowerShell cmdlet Get-Content
Get-Help Get-Content
-Detailed -Full 11-19
11-19
303 Get-Help -Example -Parameters cmdlet Get-Content
Get-Help Get-Content -Parameters *
PowerShell -Parameters * F* F Get-Help cmdlet Get-Help about_wildcard
Get-Help cmdlet PowerShell
Get-Help about_*
PowerShell
PowerShell
$strComputer = "." $colItems = get-wmiobject -class "Win32_Product" -namespace "root\CIMV2" ` -computername $strComputer foreach ($objItem in $colItems) { write-host "Caption: " $objItem.Caption write-host "Description: " $objItem.Description write-host "Identifying Number: " $objItem.IdentifyingNumber write-host "Installation Date: " $objItem.InstallDate write-host "Installation Date 2: " $objItem.InstallDate2 write-host "Installation Location: " $objItem.InstallLocation write-host "Installation State: " $objItem.InstallState write-host "Name: " $objItem.Name write-host "Package Cache: " $objItem.PackageCache write-host "SKU Number: " $objItem.SKUNumber write-host "Vendor: " $objItem.Vendor write-host "Version: " $objItem.Version write-host }
304 PowerShell
PowerShell .ps1
C:\PSCode\Demo.ps1
:
.\Demo.ps1
.\ PowerShell
PowerShell PowerShell 11-20
11-20
PowerShell Restricted cmdlet Get-ExecutionPolicy
PowerShell Set-ExecutionPolicy cmdlet 11-4
表 11-4 PowerShell 的脚本执行策略
Restricted AllSigned RemoteSigned Unrestricted
PowerShell
305 Set-ExecutionPolicy Unrestricted
Get-Help about_signing
PowerShell PowerShell
1 cmdlet
Out-Host -Paging
2 PowerShell
PowerShell cmdlet Quest Software PowerShell Commands for Active Directory http://www.quest.com/powerShell/activeroles-server.aspx cmdlet PowerShell .NET Framework
3 PowerShell PowerShell
PowerShell PowerShell PowerGui http://powergui.org/index.jspa PowerShell Script Editor PowerShell
cmdlet
PowerShell Windows
306 PowerShell
PowerShell
Windows PowerShell
http://www.microsoft.com/windowsserver2003/technologies/management/powerShell/defa ult.mspx Windows PowerShell
http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx Windows PowerShell
http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true
307