You are on page 1of 44

MICROSOFT SWITZERLAND

1 2007

()

Windows PowerShell


, Windows PowerShell, Windows PowerShell,
, ,
.

Windows PowerShell,
, .
, , , ,
, .

Microsoft Windows
PowerShell. -, .
,
, .

Windows PowerShell
,
Windows PowerShell, Windows
PowerShell:

Windows PowerShell
Windows PowerShell

, ,
.
, Windows PowerShell 1.0,
Windows XP SP2.
PowerShell . -.


Windows PowerShell, :
www.microsoft.com/PowerShell.
-, .

Windows PowerShell
http://blogs.msdn.com/PowerShell/.
. .


ITPro http://blogs.technet.com/chITPro-DE.
- Windows PowerShell
/ 2007 ..
Windows PowerShell
http://blogs.technet.com/abeshkov/default.aspx,
http://xaegr.wordpress.com
http://www.itcommunity.ru/blogs/dmitrysotnikov/default.aspx.
PowerShell http://pwrshell.blogspot.com/.

...................................................................................................2
..............................................................2
Windows PowerShell......................................................................5
: ..............................................................7
Windows PowerShell.................................................8
...........................................................................................................8
TXT, CSV XML...................................................................9
....................................................................................................................10
if..................................................................11
HTML...........................................................................................................12
.............................................................................................................15
Get-Member.............................................16
..............................................................................................................18
..................................................................................................................19
..................................................................................................21
Windows PowerShell ..........................23
WMI.....................................................................................................................23
.NET XML......................................................................................25
COM-.................................................................................................26
......................................................................................29
- ............................................................30
Windows PowerShell .............................................33
Windows PowerShell...............................................................35
Windows PowerShell .........................................................................35
Windows PowerShell.............................................................................35
, ...............................................................................35
......................................................................................................36
Windows ...................................................................37
........................................................................................................37
.......................................................................................................37
CMD, WScript PowerShell? ?.................................................................37
Windows PowerShell 1.0.....................................................................................................38
.................................................................39
Windows PowerShell
Windows PowerShell
Windows XP , - Microsoft
http://www.microsoft.com/powershell.
.NET Framework 2.0, ,
. Windows PowerShell
, 1,5 ,
.
, Windows
Windows PowerShell.

Windows PowerShell Start,


, PowerShell
Windows.

Windows PowerShell
, Windows PowerShell
CMD Start.
:

1: CMD

2: Windows PowerShell
,
. , , Windows
PowerShell /,
CMD. :


(= )

(= )

.
( Copyright (c) 2006 Microsoft Corporation)
. , ,
Enter. ,
.

, ,
.
. , Windows PowerShell
, CMD; 100 ,
( cmdlets). CMD
, .
CMD ,
CMD .
.

Windows PowerShell :

Windows PowerShell (
), .
. :
Get-Help Windows PowerShell
-:
Get-Help -Detailed
Windows PowerShell .
Windows PowerShell. :
help ( Windows) man ( Unix)
, Get-Help.

.
help . ,
.

help man Windows PowerShell Get-


Help. :

Get-Help
Get-Help * Windows PowerShell
Get-Help
Get-Help -Detailed

help help:
Get-Help Get-Help -Detailed.
: TAB
. .

:
, Windows PowerShell -
. ,
. , Windows
PowerShell (
Windows PowerShell ,
). : |

Windows
PowerShell. Get-Help * | Get-Help -Detailed : Get-Help
* , Get-Help
-Detailed. ,
CTRL-C.

,
, . Windows PowerShell
Out-File, >.

. : Get-
Help * | Get-Help -Detailed | Out-File c:\Powershell-Help.txt
Get-Help * | Get-Help -Detailed > C:\PowerShell-Help.txt. ,
( C:\ ).


. - , Get-Help
. - , -
, Get-Help Sort*. Get-Help
Windows PowerShell.
, ,
Get-Help *. , *
. , ,
, , .

, (, Sort-Object),
Get-Help,
-Detailed, :

Get-Help Sort-Object -Detailed.

.
Windows PowerShell
,
. -
, .
,
MSDN http://msdn.microsoft.com
http://www.microsoft.com/switzerland/msdn/ru/default.mspx.
process. ,
, , (Task
Manager). process, MSDN
.


Get-Process .
.
: Sort-Object. Sort-Object
( ),
-Descending. , ,
(CPU).

A1:
. ,
: Get-Process, Sort-Object (|).

: CPU Sort-Object, ,
. -.

,
. Select-Object. Select-Object
( , Get-Help),
-First x -Last y, x
y , Select-Object -First 5. Select-
Object , ,
.

A2: 10 .
A1 Select-Object.
, ,
. .

: -First, -Last.

.
, ,
. ,
. ,
PowerShell $.
A2 ,
10 .
, .
:
$a = get-process | sort-object CPU -de

, .
, PowerShell .
.

A3: $P A2.

:
, Home
, . ,
$P.

3: $P

TXT, CSV XML


Windows PowerShell
. ,
. Out-Host. Windows
PowerShell ,
, .
Out-Host; Get-Help Out*.

: Out-File .
>, Windows
PowerShell. ,
CSV XML. Out-Host
. Export-CSV Export-CliXML,
. , ,
.
Import-CSV Import-CliXML.

A4: $P A3
A4.txt. $P CSV
A4.CSV, XML A4.XML.

: > |,
, Out-File, Export-CSV .. ,
(Notepad).

, .
, , . Write-Host
, -Foregroundcolor -Backgroundcolor.
, ?

Write-Host "Red on blue" -Foregroundcolor red -Backgroundcolor blue

. Get-Help Write-Host -Detailed


. :
Write-Warning "error".
.
, . ,
.
.

, . ,
, , , MSDN. ,
, Control Panel / Administrative Tools / Services.
, , (started
stopped), .
Get-Service.

A5: .

: ,
, get-service status
Sort-Object.

.
Write-Host. , Get-Service | Write-Host -Foregroundcolor
red , . Write-Host
,
. Write-Host ,
. .
, . ,
.
ForEach-Object,
.
$_, PowerShell.
, : $_.-. :

Get-Process | ForEach-Object { Write-Host $_.ProcessName $_.CPU}

Get-Process ,
,
(|), Write-Host
. , :
,
.

A6: .
ForEach,
.
Write-Host .

A7: ,
. ,
.

: A6
-Foregroundcolor -Backgroundcolor.

4:

if
.
, . , ,
If. , ,
, :

If () { }

Elseif (2) { }

Else { }

Elseif , .
{} ,
. Windows PowerShell }
.

Windows PowerShell .
- : -eq
equals, . :

-Eq
-Match
-Ne
-Notmatch
-Gt -Ge /
-Lt -Le /

PowerShell.
, :
stopped , running .

A8: .
stopped
running.

: Sort-Object, .
Foreach, , Write-
Host, If. ,
$_.status; stopped running.

: If (), {}.
Elseif.
ForEach! >>,
, Return .

, Sort-Object.
, .

Foreach-Object ForEach.
, , .
Foreach-Object ForEach.

5:

HTML
A8 .
. ,
CSV XML. ,
: HTML. Convertto-Html.
, , ,
.
, , , -.
- ,
Convertto-Html.

A9: Get-Service HTML.


Convertto-Html, .
: , , CTRL-C.

A10: , , ,
.\A10.html. .

: Invoke-Item .\a10.html -
PowerShell.
A10.html. , .

Convertto-Html ,
. Convertto-Html , ..
| Convertto-Html -Property name, status.

A11: A10: -
.
.

: 4 :
, , HTML, .
Convertto-Html HTML, ,
HTML. Windows PowerShell, PowerShell
. , , ,
Windows PowerShell:

Get-Service | ConvertTo-Html -Property name,status | Foreach {


If ($_ -Like "*<td>Running</td>*") {$_ -Replace "<tr>", "<tr
bgcolor=green>"}
else {$_ -Replace "<tr>", "<tr bgcolor=red>"}} > .\get-service.html

, . ,
, Write-Host, HTML
: HTML
bgcolor green bgcolor red . HTML,
, , .
6: HTML HTML

.
, .
,
. (, 40).
, -.
, ,
.

Windows PowerShell - .
, dir ls. cd ,
: cd .., cd..!

Windows PowerShell .
, . , Windows PowerShell
. Get-
Psdrive , Windows PowerShell
. (:).

Windows PowerShell. (cd) HKLM:.


cd software. dir. ?
ENV:. ls,
Unix. CERT: ,
Get-Childitem.

, Windows PowerShell
. dir, ls Get-Childitem .
, , .
Windows PowerShell,
Get-Childitem. . -
, , dir cd,
. ,
, (Size)
(Date last accessed) .
, Get-ItemProperty.
. ,
, Windows PowerShell.

, Windows PowerShell
, .
New-Psdrive.

, . ,
:

New-PSdrive -Name FK -Psprovider FileSystem -Root c:\

cd FK:, ,
. , Remove-PSDrive FK
.

Windows
PowerShell.
: cd fk: ( PowerShell, ,
: Set-Location fk:).
Get-Childitem. : Get-Childitem * -Exclude
*.tmp, *.temp

B1: ,
temp tmp.

: , .

7: TMP

, Windows PowerShell
. Get-Alias | Sort-Object
Definition .
,
, . , Get-Childitem *
-Exclude *.tmp | Select-Object name, length ls * -ex *.tmp |
select n*, le*

B2: (), .

: ,
.

Get-Member
Get-Member,
. Get-Member
. Get-Member,
.

B3: Get-
Member. .
: Get-Member
.
Group-Object .
. Get-Service |
Group-Object status , ( ) .
, :

8: Group-Object

B4: . ,
.

: , .
count.

Get-Member
, Measure-Object. ,
Measure-Object,
. ,
:

Get-Childitem | Measure-Object length -Average -Sum -Maximum


-Minimum

, .
,
Windows PowerShell.

B5: TMP.
.

: ().
Get-Member
( ? Windows PowerShell ,
!). ,
(), Total.
ForEach Status? , : object.status.
total, status. .


Windows PowerShell .
Remove-Item .
Get-Childitem.
. ,
.
B6: TMP Remove-Item !

,
. Where-
Object. if, ,
. .
Get-Service | Where-Object {$_.status -Eq "stopped}
.

B7: 2 . 2
2000000 .

: .
(length -Gt 2000000).
, .
($_.fullname). Remove-Item. ,
, .

, 2 2000000 ( ,
). 2MB, Windows
PowerShell .
512KB + 512KB.
, .


.
,
. 1
: New-Item. , , ,
directory . Test :

New-Item .\test -Type Directory

, : Get-
Service | Sort-Object status ,

Get-Service | Sort-Object status -Unique


. . ,
.

B8:
.

: Extension.
-Unique.
,

(.extension).
,

1
, FAT NTFS, -
. New-Item, New-Object.
.
\. ,
(".\New+$_.Extension). (directory)
.

Move-Item.
,
, , Move-Item .\test.txt .\

B9: .

: Get-Childitem
, . (
).

(
type -Notmatch "d). ,
, . :
.
.

9:

10:

, . ,
. Get-Childitem -Recurse .
TXT, (Notepad).

B10: , ,
, FinalOutput.txt.

B11: ,
: , read-only
Word. .doc
. , , IsReadOnly,
0 ( ).

: : ,
, .
Windows PowerShell ACL, .
Get-Acl Set-Acl,
. .
ACL .

11:


, Export-Csv Export-CliXML.
$P, , , Windows
PowerShell :

$p = Get-Process

CSV CliXML:

$p | Export-CSV .\test.csv

$p | Export-CliXML .\test.xml

$p1 = Import-Csv .\test.csv

$p2 = Import-CliXML .\test.xml

C1: ,
.

: Measure-Object, .
$p, $p1 $p2.
C2: , ,
, .
$p, $p1 $p2. ?
? ?

. CSV
Windows PowerShell . ,
, , 8,0
, 800. XML
. .. , , 8,0 + 800 808,0
Measure-Object , .
, . XML
.

Windows PowerShell
Windows PowerShell ;
, WMI, .NET COM.
, .
.
, .
, .

WMI
, , WMI Windows Scripting Host WSH
VBScript. , , , ,
WMI. Windows PowerShell.

WMI Windows PowerShell


Get-Wmiobject. , WMI. Windows
PowerShell :

Get-Wmiobject -Class win32_computersystem

.
VBScript , Windows PowerShell ,
.

Get-Wmiobject ,
WMI
WMI, , .. -Class
win32_computersystem

12: WMI WIN32_COMPUTERSYSTEM

, , .
Windows PowerShell Get-Member.
D1: (User name) .
: WMI
.

WMI, , .
, .

, ,
. WMI,
, , CIM Studio.
.
.
WMI:

Get-Wmiobject -Class win32_desktop -Computername .

,
. (server1,
server4.mycompany.ch, ..).

BIOS :

Get-Wmiobject -Class win32_bios

, -Computername .
.

Get-Wmiobject -Class win32_quickfixengineering :

Get-Wmiobject -Class win32_quickfixengineering -Property hotfixid |

Select-Object -Property hotfixid

13: KB
WMI , Windows
PowerShell, Put ,
. Get-Member,
. ,
(set), (get). Visible
COM , ,
.

.NET XML
.NET XML ,
WMI. . - ,
RSS , URL,
RSS.
.
, ,
. Windows PowerShell Windows PowerShell in Action
(Bruce Payette), Windows PowerShell
.

([xml](new-object net.webclient).DownloadString(

http://blogs.msdn.com/powershell/rss.aspx)).rss.channel.item |
Format-Table title,link

, , .
WMI. [XML].
new-object.
. (net.webclient), Windows PowerShell
, .NET Webclient.

DownloadString(url),
URL- ( -).

: ,
, ; , RSS
, , , RSS. URL-
, ,
RSS.

format-table
. ,
(title), URL- (link) RSS.

, 4.
.NET Windows
PowerShell. Windows WinForms.

Windows PowerShell .
, .
.NET .NET Microsoft MSDN,
.NET.
COM-
COM-
. Excel. Excel,
Internet Explorer.

COM.
, .

, Windows PowerShell, COM.


, ,
. , COM .
COM-, WMI,
. :
new-object.

$a = new-object -comobject excel.application

, COM- .
Excel, Excel.Application. ,
COM-, .
Microsoft MSDN COM.
Excel ,
- Visio
, Windows
PowerShell. MS Press ,
COM- Visio.
Excel.

Excel .
COM, COM. Windows PowerShell
, , . ,
$a | Get-Member. Get-Member
COM-. , Excel,
, , . Workbooks.Add()
.
. .
:

$b = $a.Workbooks.Add()

, Excel Error:
0x80028018 (-2147647512) Description: Old Format or Invalid Type
Library. 320369 Microsoft.
, Excel (, English (US)),
Windows (, English (UK))).
Excel. Microsoft
.
English (US)
Windows PowerShell. Excel
.

, , . :

$c = $b.Worksheets.Item(1)
- , ,
Excel. , :

$c.Cells.Item(1,1) = "Windows PowerShell rocks!"

. Excel . ? , Excel
, .

$a.Visible = $True

Visible Excel $true. Excel


.

, Visible, () Excel
. , , ,
:

$b.SaveAs(".\Test.xls")

: , Excel,
Excel. XLS, .

, . ,
Excel. , Excel
? SaveAs
? COM-,
COM,
Windows PowerShell.

Get-Member .
Windows PowerShell,
, . Excel,
:

$a.Quit()

, :

D2: Excel.
: Excel
. Excel,
$i.
$c.Cells.Item($i,1).

$i ,
$i = $i + 1. ,
;. Excel XLS, , ,
, , Excel.

, Excel .
Windows PowerShell running stopped,
.
14: Excel 2007.
Excel 2007 , .

Excel, ,
Internet Explorer. -.
RSS
: RSS
-.
.

, Excel, new-object:

$ie = New-Object -Comobject InternetExplorer.application

, Excel,
:

$ie.Visible = $True

, Internet Explorer,
Get-Member:

$ie | Get-Member

, -:

$ie.Navigate(http://www.microsoft.com/powershell)
, RSS.
. ,
, .
;
Windows PowerShell.
.



. Windows PowerShell
. WMI, .NET COM
. Windows PowerShell ,
. Get-Eventlog.

Get-Eventlog -List .
,
Where-Object. ,
1:

Get-Eventlog -List | Where-Object {$_.logdisplayname -eq "System"}

,
, .

, ,
Get-Eventlog, .
, , CTRL-C.
20 , -newest 20.
, 20 .

Get-Eventlog system -Newest 3

Get-Eventlog system -Newest 3 | Format-List

Windows PowerShell,
.

E1: Windows PowerShell.


ID , .
ID 403.

: ,
" ".

E2: 15 ID
.

: , .

1
Windows XP .
, .

-

A1

get-process | sort-object CPU

A2a

get-process | sort-object CPU -descending | select-object -first 10

get-process | sort-object CPU | select-object -last 10

A3

$P = get-process | sort-object CPU -descending | select-object -first 10

A4

$P > .\A4.txt

$P | export-csv .\a4.csv

$P | export-CliXML .\a4.xml

A5

get-service | sort-object status

A6

get-service | foreach-object{ write-host $_.name $_.status}

A7

get-service | foreach-object{ write-host -f yellow -b red $_.name $_.status}

-f -foregroundcolor, -b -backgroundcolor

A8

get-service | foreach-object{

if ($_.status -eq "stopped") {write-host -f green $_.name $_.status}`

else{ write-host -f red $_.name $_.status}}

A9

get-service | convertto-html
A10

get-service | convertto-html > .\a10.html

A11

get-service | sort-object status | convertto-html name, status > .\a10.html

B1

get-childitem * -exclude *.tmp | select-object name, length

B2

get-childitem * -exclude *.tmp | select-object name, length | sort-object length, name

B3

get-childitem | get-member

B4

get-childitem | group-object extension | sort-object count

B5

(get-childitem .\*.tmp | measure-object length -sum).sum

B6

remove-item .\*.tmp

B7

get-childitem | where-object {$_.length -gt 2000000}

| foreach-object {remove-item $_.fullname}

B8

get-childitem | select-object extension | sort-object extension -unique |

foreach-object {new-item (".\New" + $_.extension) -type directory}

B9

get-childitem | where-object {$_.mode -notmatch "d"} |

foreach-object {$b= ".\New" + $_.extension; move-item $_.fullname $b}

B10

get-childitem -recurse > .\finaloutput.txt


B11

get-childitem *.doc | foreach-object {$_.Isreadonly = 0}

C1

$p | measure-object CPU -min -max -average

C2

$p | sort-object CPU -Descending | Select-Object -first 5

D1

(get-wmiobject -class win32_computersystem).username

D2

$a = new-object -comobject excel.application

$a.Visible = $True

$b = $a.Workbooks.Add()

$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "Service Name"

$c.Cells.Item(1,2) = "Service Status"

$i = 2

get-service | foreach-object{ $c.cells.item($i,1) = $_.name

$c.cells.item($i,2) = $_.status; $i=$i+1}

$b.SaveAs("C:\Users\frankoch\Downloads\Test.xls")

$a.Quit()

E1

get-eventlog "Windows PowerShell" | group-object eventid | sort-object name

get-eventlog "Windows PowerShell" | where-object {$_.eventid -eq 403}

E2

get-eventlog system -newest 15 | sort-object eventid -descending




Windows PowerShell
Windows
PowerShell. PowerShell, ,
Windows PowerShell.
, >
( ).
Windows PowerShell in Action , Windows
PowerShell. ,
.

1:

"Hello world":

"Hello world"

:
Windows PowerShell
.

2:

Windir; ,
Error, :

dir $env:windir\*.log | Select-String -List error | Format-Table path,linenumber -Autosize

:
, ..
.

3: RSS

-, RSS, RSS
URL-:

([xml](New-Object net.webclient).DownloadString(

"http://blogs.msdn.com/powershell/rss.aspx")).rss.channel.item | Format-Table title,link

:
-
.
4:

WinForm :

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$form = New-Object Windows.Forms.Form

$form.Text = "My First Form"

$button = New-Object Windows.Forms.Button

$button.text="Push Me!"

$button.Dock="fill"

$button.add_click({$form.close()})

$form.controls.add($button)

$form.Add_Shown({$form.Activate()})

$form.ShowDialog()

:
(
Windows PowerShell).

WINDOWS POWERSHELL
Windows PowerShell

Windows PowerShell
Microsoft
. command.com, ,
MS DOS,
. cmd.exe, Windows NT,
.
Unix, Bash, Microsoft, ,
.

Microsoft . Windows PowerShell (


Monad Shell, MSH),
Windows,
. Windows PowerShell ,
- , bash.

Windows PowerShell
Windows PowerShell Windows,
.
, ,
. ,
, Windows PowerShell ,
.NET Framework 2.0.
Windows .

Windows PowerShell ( cmdlet).


,
. ,

. Windows PowerShell ,

.

, Windows PowerShell
. Windows PowerShell
, .

,
PowerShell -.
, ,
( ).
, ,
. ,
, .

PowerShell C++ C#.


.
, .

, , Windows PowerShell,
, .
, .
, ,
.
.

- PowerShell
Unix (/)
, . ,
:

, ,
100 . Linux
(ps -A). .
, .
, ID ,
.
,
.

,
, ,
. , , ,
, .

PowerShell .
get-process, .
, .

, .
.


Windows PowerShell - ,
. :

. Windows PowerShell .NET.


. , .
. ,
.NET, C#.

. Windows
PowerShell
. ,
Windows PowerShell.
: , ,
(-), , get-help, get-process start-service. Windows PowerShell

. , , Get ,
Set , Format ,
Out .

Windows
. ,
, . Windows
PowerShell Windows
Windows, , (Notepad)
(Calculator). , Cmd.exe,
. ,
dir, ls cd, Windows PowerShell,
- .


, Windows PowerShell
. ,
.
,
(|) .


,
,
. , , .

, Windows PowerShell
. ,
. Windows PowerShell scripts - .ps1;
.

,
. Windows PowerShell
( ), ,
, .
, Windows PowerShell
, , ,
.bat, .cmd .vbs.

CMD, WScript PowerShell? ?


Windows XP :
CMD, Windows Scripting Host VB Jscript, Windows
PowerShell. ,
, . Windows, Vista Longhorn
Server, , .
, , .
, .
, Windows PowerShell,
.
, , ,
, 60- 70-
.

Windows PowerShell 1.0


Windows PowerShell 1.0, ,
.
, Windows PowerShell
.NET Framework 2.0.
.

, , ,
Windows PowerShell. Windows PowerShell
, , ,
.NET, WMI ADSI; .
, WMI .NET.
, ,
-, 2006 .
, WMI .NET.



Windows Scripting Host (WSH), Windows 2000, Microsoft
.
, .

, ,
, VBScript, .
Windows PowerShell , .

, Windows PowerShell
.

.
,
. , Windows PowerShell (PS1)
(Notepad). ,

. , Windows
PowerShell ,
%Path%.
, ,
Windows PowerShell.

,
Windows PowerShell. : get-
executionpolicy set-executionpolicy. get-executionpolicy
. :


Restricted (, )
Allsigned ( )
RemoteSigned ( ) ,

Unrestricted ( )

, ,

set-executionpolicy remotesigned

Microsoft ,
.
Windows PowerShell.

.
, ,
.

Windows PowerShell,
. , , ,
Windows PowerShell in Action.
!

, ,
, Microsoft MSDN Windows PowerShell.
, .

,
. ,
: frankoch@microsoft.com