Benutzer-Werkzeuge

Webseiten-Werkzeuge


lehrgaenge:windows_server_2008_r2:9102_powershell_fuer_automatische_windows_administration

Windows Server R2

Dozent: Herr Frank Röder roeder@iteache-online.de
23.04. - 27.04.2012

1. Tag

Den Windows Editor aus der PowerShell starten um damit die Konfigurationsdatei profile.ps1 zu erstellen.

PS C:\Windows\system32> notepad $env:USERPROFILE\documents\windowspowershell\profile.ps1

Eine Logdatei erstellen die die Eingaben der PowerShell mitprotokoliert.

PS C:\Windows\system32> Start-Transcript -Append -Path ("$env:USERPROFILE\documents\"+(((Get-Date).DayOfWeek).tostring())+".txt")

Das PowerShell Fenster leeren (wie cls)

PS C:\Windows\system32> Clear-Host

Ein Snapin integriern

PS C:\Users\Admin> Add-PSSnapin pseventing

Hilfe erhalten

PS C:\Users\Admin> get-help

Die Befehle die sich mit eventlog befassen finden

PS C:\> Get-Command *eventlog*
PS C:\> Get-Command *event* -CommandType cmdlet

Hilfe bekommen

PS C:\> Get-Help Get-EventLog
PS C:\> Get-Help Get-EventLog -Detailed
PS C:\> Get-Help Get-EventLog -Full
PS C:\> Get-Help Get-EventLog -Examples
PS C:\> Get-Help Get-EventLog -Parameter logname
PS C:\> Get-Help Get-EventLog -Parameter *
PS C:\> Get-Help about_*
PS C:\> Get-Help about_Comparison_operators

Ausgabe Seitenweise

PS C:\> Get-Help about_Comparison_operators | Out-Host -Paging

Eine Befehl nicht wirklich ausführen, nur prüfen was wäre wenn.

PS C:\>  Get-Process pow* | Stop-Process -whatif
WhatIf: Ausführen des Vorgangs "Stop-Process" für das Ziel "powershell (2224)".
PS C:\> Get-Date | Get-Member
PS C:\> (Get-Date).addhours(-24)

Sonntag, 22. April 2012 14:35:20

Die Ereignisse der letzten 24 Std. von den beiden PC holen.

PS C:\Users\Administrator> Get-EventLog system -After (Get-Date).addhours(-24) -ComputerName DC1-PSH,CL1-PSH

2. Tag

PS C:\Users\Administrator> Get-Command *service* -CommandType cmdlet
PS C:\Users\Administrator> $Host.UI.RawUI
PS C:\Users\Administrator> notepad
PS C:\Users\Administrator> $meinnotepad = Start-Process notepad
PS C:\Users\Administrator> $meinnotepad
PS C:\Users\Administrator> $meinnotepad = Start-Process notepad -PassThru
PS C:\Users\Administrator> $meinnotepad

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                      
-------  ------    -----      ----- -----   ------     -- -----------                                                                      
     57       7     1604       5496    79     0,11   2052 notepad 
PS C:\Users\Administrator> New-Alias np notepad.exe
PS C:\Users\Administrator> np
PS C:\Users\Administrator> Export-Alias $env:USERPROFILE\documents\alias.txt
PS C:\Users\Administrator> np  $env:USERPROFILE\documents\alias.txt  
PS C:\Users\Administrator> dir function:

CommandType     Name                                                          Definition                                                   
-----------     ----                                                          ----------                                                   
Function        A:                                                            Set-Location A:                                              
Function        B:                                                            Set-Location B:                                              
Function        BinIchAdmin                                                   ...                                                          
Function        C:                                                            Set-Location C:                                              
Function        cd..                                                          Set-Location ..                                              
Function        cd\                                                           Set-Location \                                               
Function        Clear-Host                                                    $space = New-Object System.Management.Automation.Host.Buff...
Function        D:                                                            Set-Location D:                                              
Function        Disable-PSRemoting                                            ...                                                          
Function        E:                                                            Set-Location E:                                              
Function        F:                                                            Set-Location F:                                              
Function        G:                                                            Set-Location G:                                              
Function        Get-Verb                                                      ...                                                          
Function        H:                                                            Set-Location H:                                              
Function        help                                                          ...                                                          
Function        I:                                                            Set-Location I:                                              
Function        ImportSystemModules                                           ...                                                          
Function        J:                                                            Set-Location J:                                              
Function        K:                                                            Set-Location K:                                              
Function        L:                                                            Set-Location L:                                              
Function        M:                                                            Set-Location M:                                              
Function        mkdir                                                         ...                                                          
Function        more                                                          param([string[]]$paths)...                                   
Function        N:                                                            Set-Location N:                                              
Function        O:                                                            Set-Location O:                                              
Function        P:                                                            Set-Location P:                                              
Function        prompt                                                        $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } el...
Function        Q:                                                            Set-Location Q:                                              
Function        R:                                                            Set-Location R:                                              
Function        S:                                                            Set-Location S:                                              
Function        T:                                                            Set-Location T:                                              
Function        TabExpansion                                                  ...                                                          
Function        U:                                                            Set-Location U:                                              
Function        V:                                                            Set-Location V:                                              
Function        W:                                                            Set-Location W:                                              
Function        X:                                                            Set-Location X:                                              
Function        Y:                                                            Set-Location Y:                                              
Function        Z:                                                            Set-Location Z:  
PS C:\Users\Administrator> function sayhello {write-host Hallo $args[0] -foregroundcolor "red"}
PS C:\Users\Administrator> sayhello Timo
Hallo Timo  
PS C:\Users\Administrator> "Ein String mit mit `"doppelten`" Anführungszeichen"
Ein String mit mit "doppelten" Anführungszeichen
PS C:\Users\Administrator> write-host "Hallo" write-host "Teilnehmer"
Hallo write-host Teilnehmer
PS C:\Users\Administrator> write-host "Hallo"; write-host "Teilnehmer"
Hallo
Teilnehmer
PS C:\Users\Administrator> Get-PSProvider

Name                 Capabilities                                                Drives                                                    
----                 ------------                                                ------                                                    
WSMan                Credentials                                                 {WSMan}                                                   
Alias                ShouldProcess                                               {Alias}                                                   
Environment          ShouldProcess                                               {Env}                                                     
FileSystem           Filter, ShouldProcess                                       {C, E, A, D}                                              
Function             ShouldProcess                                               {Function}                                                
Registry             ShouldProcess, Transactions                                 {HKLM, HKCU}                                              
Variable             ShouldProcess                                               {Variable}                                                
Certificate          ShouldProcess                                               {cert}
PS C:\Users\Administrator> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root                                                                   CurrentLocation
----           ---------     --------- --------      ----                                                                   ---------------
A                                      FileSystem    A:\                                                                                   
Alias                                  Alias                                                                                               
C                  10,36        116,54 FileSystem    C:\                                                                Users\Administrator
cert                                   Certificate   \                                                                                     
D                    ,27               FileSystem    D:\                                                                                   
E                    ,07          7,93 FileSystem    E:\                                                                                   
Env                                    Environment                                                                                         
Function                               Function                                                                                            
HKCU                                   Registry      HKEY_CURRENT_USER                                                                     
HKLM                                   Registry      HKEY_LOCAL_MACHINE                                                                    
Variable                               Variable                                                                                            
WSMan                                  WSMan   
PS C:\windows> Get-ChildItem $env:windir\*.log -Recurse


    Verzeichnis: C:\Windows


Mode                LastWriteTime     Length Name                                                                                          
----                -------------     ------ ----                                                                                          
-a---        16.05.2011     10:20       1774 DtcInstall.log                                                                                
-a---        24.04.2012     07:41      14299 setupact.log                                                                                  
-a---        14.07.2009     06:51          0 setuperr.log                                                                                  
-a---        16.05.2011     10:20       1313 TSSysprep.log                                                                                 
-a---        16.05.2011     10:20       5460 vmgcoinstall.log                                                                              
-a---        24.04.2012     11:13     224777 WindowsUpdate.log                                                                             
PS C:\windows> Get-ChildItem $env:windir\ *.log -Recurse
PS C:\windows> Get-ChildItem C:\Users\ -Include *.jpg, *.bmp -Recurse
PS C:\windows> Get-ChildItem hklm:


    Hive: HKEY_LOCAL_MACHINE


SKC  VC Name                           Property                                                                                            
---  -- ----                           --------                                                                                            
  2   0 BCD00000000                    {}                                                                                                  
  4   0 HARDWARE                       {}                                                                                                  
  1   0 SAM                            {}                                                                                                  
Get-ChildItem : Der angeforderte Registrierungszugriff ist unzulässig.
Bei Zeile:1 Zeichen:14
+ Get-ChildItem <<<<  hklm:
    + CategoryInfo          : PermissionDenied: (HKEY_LOCAL_MACHINE\SECURITY:String) [Get-ChildItem], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetChildItemCommand
 
 10   0 SOFTWARE                       {}                                                                                                  
  8   0 SYSTEM                         {}  
PS C:\windows> Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\services
PS C:\windows> Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\services\Spooler
PS C:\windows> Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | ft displayname,publisher
PS C:\windows> New-ItemProperty -Path HKCU:\Software\Testschlüssel -Name DWORD -Value 0x1234 -PropertyType DWORD 
PS C:\windows> New-ItemProperty -Path HKCU:\Software\Testschlüssel -Name MULTISTRING -Value eins,zwei,drei -PropertyType MULTISTRING 
PS C:\windows> Set-ItemProperty -Path HKCU:\Software\Testschlüssel -Name MULTISTRING -Value vier,fünf,sechs -PropertyType MULTISTRING

Registry ändern

Aus der Registry auslesen von welchen Diensten der Druckerspooler abhängig ist und dann eine Abhängigkeit vom Systemdienst eintragen. Einmal auslesen und dann in eine Variable schreiben

PS C:\windows> (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\Spooler).DependOnService
RPCSS
http
PS C:\windows> $Service = (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\Spooler).DependOnService;

Erste Lösung:

PS C:\windows> $Service = (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\Spooler).DependOnService;Set-ItemProperty -Path  HKLM:\SYSTEM\CurrentControlSet\services\Spooler -Name DependOnService -Value $Service[0],$Service[1],LanmanServer
PS C:\windows> (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\Spooler).DependOnService
RPCSS
http
LanmanServer

oder etwas kürzer

PS C:\windows> $Service = (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\Spooler).DependOnService;Set-ItemProperty -Path  HKLM:\SYSTEM\CurrentControlSet\services\Spooler -Name DependOnService -Value ($Service+="LanmanServer")

oder ohne den Zwischenschritt einer Variablen

PS C:\windows> Set-ItemProperty -Path  HKLM:\SYSTEM\CurrentControlSet\services\Spooler -Name DependOnService -Value ((Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\Spooler).DependOnService+="LanmanServer")

3. Tag

4. Tag

5. Tag

lehrgaenge/windows_server_2008_r2/9102_powershell_fuer_automatische_windows_administration.txt · Zuletzt geändert: von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki