我们将实验Windows PowerShell的功能,这是一系列的实验。
可参阅《Windows PowerShell应用手册》,机械工业出版社 Lee Holmes著,赵松德 王英群译。
PowerShell笔记 19:02 2011-4-18
1、PS支持常规的Windows命令行:ipconfig notepad……
2、PS命令行可通过Tab键补全: get-pr<tab> -N<tab> lsass
3、PS支持通配符参数:gps l*s
4、PS支持直接调用.Net Framework 类库
PS C:\> [System.Console]::WriteLine("{0},{1}",5%2.2,5/2)
0.6,2.5
PS C:\> [DateTime]::Now
2011年4月18日 19:09:54
PS C:\> calc
PS C:\> $process = Get-Process calc
PS C:\> $process.kill()
5、可组合的命令:
PS C:\> Get-Process |
>> Where-Object { $_.Handles -ge 500 } |
>> Sort-Object Handles |
>> Format-Table Handles, Name, Description -Auto
>>
Handles Name Description
------- ---- -----------
531 explorer Windows Explorer
564 csrss
967 powershell PowerShell.EXE
1076 System
1568 svchost Generic Host Process for Win32 Services
6、若要查找与给定的通配符匹配的所有命令,用Get-Command名伶。
PS C:\> Get-Command *process*
CommandType Name Definition
----------- ---- ----------
Cmdlet Get-Process Get-Process [[-Name] <String[]>] [-Verbose] [-De...
Application qprocess.exe C:\WINDOWS\system32\qprocess.exe
Cmdlet Stop-Process Stop-Process [-Id] <Int32[]> [-PassThru] [-Verbo...
7、若要查找一个命令可以做什么,用 Get-Help 获得帮助
Get_Help Get-Process
8、无处不在的脚本:计算系统的句柄总数,分析网页
PS C:\> $count = 0
PS C:\> foreach($process in Get-Process) { $count += $process.Handles }
PS C:\> $count
10149
PS C:\> $webClient = New-Object System.Net.WebClient
PS C:\> $content = $webClient.DownloadString("http://www.google.com/")
PS C:\> $content.Substring(0,1000)
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At line:1 char:19
+ $content.Substring( <<<< 0,1000)
PS C:\> $content
<HTML><frameset border='0' frameSpacing='0' rows='100%,0' frameBorder='0'>
<frame id ='frm123' name='frm123' src='http://219.146.13.122/main.do?param=ABaW1zaT00NjAwMzY3NjA2MDUwNDImdXJsPXd3dy5nb2
9nbGUuY29tLyYyPS0xOTM5OTIyMjcy&ref=1'><frame id ='frmOLD' name='frmOLD' src='http://118.213.88.152/tongji.html'></frame
set></HTML>
PS C:\>
PS C:\> sqlcmd -S .\sqlexpress -E
1> use northwind
2> go
Changed database context to 'Northwind'.
1> select top 1 firstname,lastname,title from employees
2> go
firstname lastname title
---------- -------------------- ------------------------------
Nancy Davolio Sales Representative
(1 rows affected)
9、PS管理WMI(Windows Management Instrumentation)
PS C:\> Get-WmiObject win32_bios
SMBIOSBIOSVersion : R0051V1
Manufacturer : Phoenix Technologies LTD
Name : Ver 1.00PARTTBLL
SerialNumber : 28240893-9003733
Version : Sony - 20051201
10、PS使用活动目录(Active Directory),注意WinNT的大小写
PS C:\> [ADsI] "WinNT://./AdministratOR" | Format-List *
…………
11、PS导航功能
PS C:\WINDOWS> Set-Location c:\
PS C:\> Get-ChildItem
……
PS C:\> Set-Location HKCU:\Software\Microsoft\Windows\
PS HKCU:\Software\Microsoft\Windows> Get-ChildItem
Hive: Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows
SKC VC Name Property
--- -- ---- --------
27 0 CurrentVersion {}
3 1 Shell {BagMRU Size}
4 1 ShellNoRoam {BagMRU Size}
PS HKCU:\Software\Microsoft\Windows> Set-Location cert:\CurrentUser\Root
PS cert:\CurrentUser\Root> Get-ChildItem
Directory: Microsoft.PowerShell.Security\Certificate::CurrentUser\Root
Thumbprint Subject
---------- -------
CDD4EEAE6000AC7F40C3802C171E30148030C072 CN=Microsoft Root Certificate Authority, DC=microsoft, DC=com
A43489159A520F0D93D032CCAF37E7FE20A8B419 CN=Microsoft Root Authority, OU=Microsoft Corporation, OU=Copyright (c) 19...