SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


notespowershell
Differences

This shows you the differences between two versions of the page.

Link to this comparison view

notespowershell [2013/05/14 20:20] (current)
sjoerd created
Line 1: Line 1:
 += Notes powershell training =
  
 += cvs geen header =
 +
 +<​code>​
 +export-csv filename -notype
 +</​code>​
 +
 += hernoemen property =
 +<​code>​
 +@{label="​computername";​ expression={$_.name}}
 +</​code>​
 += simpele commandos =
 +<code powershell>​
 +PS D:​\adminshf>​ Get-Module
 +
 +ModuleType Name                      ExportedCommands
 +---------- ----                      ----------------
 +Manifest ​  ​DataONTAP ​                ​{Clear-NaFcpPortNameAlias,​ Get-NcVolLanguage,​ Get-NaFlashDevice,​ Set-NaF...
 +
 +
 +PS D:​\adminshf>​ get-psdrive
 +
 +Name           Used (GB)     Free (GB) Provider ​     Root                                               ​CurrentLocation
 +----           ​--------- ​    ​--------- -------- ​     ----                                               ​---------------
 +A                                      FileSystem ​   A:\
 +Alias                                  Alias
 +C                  13.91         15.99 FileSystem ​   C:\                                                 ​Users\adminshf
 +cert                                   ​Certificate ​  \
 +D                   ​3.02 ​         6.97 FileSystem ​   D:\                                                       ​adminshf
 +E                                      FileSystem ​   E:\
 +Env                                    Environment
 +Function ​                              ​Function
 +HKCU                                   ​Registry ​     HKEY_CURRENT_USER
 +HKLM                                   ​Registry ​     HKEY_LOCAL_MACHINE
 +I                   ​4.07 ​         3.93 FileSystem ​   I:\
 +Variable ​                              ​Variable
 +WSMan                                  WSMan
 +</​code>​
 +
 += functie met help =
 +<code powershell>​
 +
 +
 +Function Optellen
 +{
 +param ($x, $y)
 +$Ans = $x + $y
 +Write-Host “Het antwoord is $Ans”
 +
 +<#  ​
 +.SYNOPSIS  ​
 +    Een zeer nuttige funktie
 +.DESCRIPTION  ​
 +    Dit script telt 2 waardes bij elkaar op
 +.NOTES  ​
 +    File Name  : optellen.ps1
 +    Author ​    : Roel Slob 
 +    Requires ​  : PowerShell V2 CTP3  ​
 +.LINK  ​
 +   ​http://​ThePowershellGuru.org
 +.EXAMPLE
 +   ​Optellen 1 2
 +#>
 +
 +
 +}
 +
 +met params ​
 +$var = $(throw "enter variable"​),​
 +
 +kan je de variable vereist maken met een foutmelding (enter variabele):
 +
 +{
 +param (
 + $x = $(throw "geef variable op"​), ​
 + $y)
 +$Ans = $x + $y
 +Write-Host “Het antwoord is $Ans”
 +
 +<#  ​
 +.SYNOPSIS  ​
 +    Een zeer nuttige funktie
 +.DESCRIPTION  ​
 +    Dit script telt 2 waardes bij elkaar op
 +.NOTES  ​
 +    File Name  : optellen.ps1
 +    Author ​    : Roel Slob 
 +    Requires ​  : PowerShell V2 CTP3  ​
 +.LINK  ​
 +   ​http://​ThePowershellGuru.org
 +.EXAMPLE
 +   ​Optellen 1 2
 +#>
 +
 +
 +}
 +
 +</​code>​
 +
 +uitvoeren geeft dan
 +<code powershell>​
 +PS C:​\Users\adminshf>​ optellen
 +geef variable op
 +At C:​\Users\adminshf\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:​6 char:14
 ++  $x = $(throw <<<< ​ "geef variable op"),
 +    + CategoryInfo ​         : OperationStopped:​ (geef variable op:String) [], RuntimeException
 +    + FullyQualifiedErrorId : geef variable op
 +
 +PS C:​\Users\adminshf>​ optellen 1 2
 +Het antwoord is 3
 +PS C:​\Users\adminshf>​ optellen 1
 +Het antwoord is 1
 +PS C:​\Users\adminshf>​ get-help optellen
 +
 +NAME
 +    Optellen
 +
 +SYNOPSIS
 +    Een zeer nuttige funktie
 +
 +
 +SYNTAX
 +    Optellen [[-x] <​Object>​] [[-y] <​Object>​] [<​CommonParameters>​]
 +
 +
 +DESCRIPTION
 +    Dit script telt 2 waardes bij elkaar op
 +
 +
 +RELATED LINKS
 +     ​http://​ThePowershellGuru.org
 +
 +REMARKS
 +    To see the examples, type: "​get-help Optellen -examples"​.
 +    For more information,​ type: "​get-help Optellen -detailed"​.
 +    For technical information,​ type: "​get-help Optellen -full"​.
 +</​code>​
 +
 += global variable =
 +kan ook in funcvties worden gezet
 +<code powershell>​
 +PS C:​\Users\adminshf>​ $global:a = 12
 +</​code>​
 +
 +Geldt dan ook voor omhoog, in plaats van alleen naar beneden
 +
 +of voor in een script ​
 +<code powershell>​
 +PS C:​\Users\adminshf>​ $script:a = 12
 +</​code>​
 +
 += see last error =
 +
 +<code powershell>​
 +PS C:​\Users\adminshf>​ $error[0]
 +The term '​inquire'​ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe
 +lling of the name, or if a path was included, verify that the path is correct and try again.
 +At line:1 char:27
 ++ $debugpreference = inquire <<<<​
 +    + CategoryInfo ​         : ObjectNotFound:​ (inquire:​String) [], CommandNotFoundException
 +    + FullyQualifiedErrorId : CommandNotFoundException
 +</​code> ​   ​
 += foutmeldingen geven =
 +<code powershell>​
 +PS C:​\Users\adminshf>​ get-help about_preference_variables > notepad
 +PS C:​\Users\adminshf>​ get-help about_preference_variables > c:​\temp\var.txt
 +PS C:​\Users\adminshf>​ $debugpreference
 +SilentlyContinue
 +PS C:​\Users\adminshf>​ $debugpreference = "​inquire"​
 +PS C:​\Users\adminshf>​ $debugpreference
 +Inquire
 +</​code>​
 +
 +
 +let op
 +<​code>​
 +$ErrorActionPreference
 +</​code>​
 +
 += profile =
 +<code powershell>​
 +PS C:​\Users\adminshf>​ $host
 +
 +
 +Name             : ConsoleHost
 +Version ​         : 2.0
 +InstanceId ​      : 2a2054e0-7fa3-4c3a-9ffd-5a33917a61fb
 +UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
 +CurrentCulture ​  : en-US
 +CurrentUICulture : en-US
 +PrivateData ​     : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
 +IsRunspacePushed : False
 +Runspace ​        : System.Management.Automation.Runspaces.LocalRunspace
 +
 +
 +
 +PS C:​\Users\adminshf>​ $host.privatedata
 +
 +
 +ErrorForegroundColor ​   : Red
 +ErrorBackgroundColor ​   : Black
 +WarningForegroundColor ​ : Yellow
 +WarningBackgroundColor ​ : Black
 +DebugForegroundColor ​   : Yellow
 +DebugBackgroundColor ​   : Black
 +VerboseForegroundColor ​ : Yellow
 +VerboseBackgroundColor ​ : Black
 +ProgressForegroundColor : Yellow
 +ProgressBackgroundColor : DarkCyan
 +
 +
 +
 +PS C:​\Users\adminshf>​ $host.privatedata.errorforegroundcolor
 +Red
 +PS C:​\Users\adminshf>​ $host.privatedata.errorforegroundcolor = yellow
 +The term '​yellow'​ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
 +ling of the name, or if a path was included, verify that the path is correct and try again.
 +At line:1 char:48
 ++ $host.privatedata.errorforegroundcolor = yellow <<<<​
 +    + CategoryInfo ​         : ObjectNotFound:​ (yellow:​String) [], CommandNotFoundException
 +    + FullyQualifiedErrorId : CommandNotFoundException
 +
 +PS C:​\Users\adminshf>​ $host.privatedata.errorforegroundcolor = "​yellow"​
 +PS C:​\Users\adminshf>​ $host.privatedata.errorforegroundcolor = yellow
 +The term '​yellow'​ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
 +ling of the name, or if a path was included, verify that the path is correct and try again.
 +At line:1 char:48
 ++ $host.privatedata.errorforegroundcolor = yellow <<<<​
 +    + CategoryInfo ​         : ObjectNotFound:​ (yellow:​String) [], CommandNotFoundException
 +    + FullyQualifiedErrorId : CommandNotFoundException
 +</​code>​
 +
 +De regel <​code>​$host.privatedata.errorforegroundcolor = "​yellow"</​code>​ kan je opnemen in het $profile om het standaard te laten werken.
 +
 +copy C:​\Windows\System32\WindowsPowerShell\v1.0\Examples profile.ps1 naar dezelfde directory als de powershell executable.  ​
 +
 += calender=
 +http://​mow001.blogspot.com/​2006/​10/​powershell-calendar-function-gui.html ​
 +
 += haakjes =
 +<code powershell>​
 +PS C:​\Users\adminshf>​ get-host
 +
 +
 +Name             : ConsoleHost
 +Version ​         : 2.0
 +InstanceId ​      : ee2323b0-c492-48e2-9daa-015f85180053
 +UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
 +CurrentCulture ​  : en-US
 +CurrentUICulture : en-US
 +PrivateData ​     : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
 +IsRunspacePushed : False
 +Runspace ​        : System.Management.Automation.Runspaces.LocalRunspace
 +
 +
 +
 +PS C:​\Users\adminshf>​ (get-host).name
 +ConsoleHost
 +</​code>​
 +
 += Cmdlets vinden=
 +<code powershell>​
 +PS C:​\Users\adminshf>​ get-command -noun *event*
 +
 +CommandType ​    ​Name ​                                               Definition
 +----------- ​    ​---- ​                                               ----------
 +Cmdlet ​         Clear-EventLog ​                                     Clear-EventLog [-LogName] <​String[]>​ [[-Computer.
 +Cmdlet ​         Get-Event ​                                          ​Get-Event [[-SourceIdentifier] <​String>​] [-Verbo.
 +Cmdlet ​         Get-EventLog ​                                       Get-EventLog [-LogName] <​String>​ [[-InstanceId] .
 +Cmdlet ​         Get-EventSubscriber ​                                ​Get-EventSubscriber [[-SourceIdentifier] <String.
 +Cmdlet ​         Get-WinEvent ​                                       Get-WinEvent [[-LogName] <​String[]>​] [-MaxEvents.
 +Cmdlet ​         Limit-EventLog ​                                     Limit-EventLog [-LogName] <​String[]>​ [-ComputerN.
 +Cmdlet ​         New-Event ​                                          ​New-Event [-SourceIdentifier] <​String>​ [[-Sender.
 +Cmdlet ​         New-EventLog ​                                       New-EventLog [-LogName] <​String>​ [-Source] <Stri.
 +Cmdlet ​         Register-EngineEvent ​                               Register-EngineEvent [-SourceIdentifier] <String.
 +Cmdlet ​         Register-ObjectEvent ​                               Register-ObjectEvent [-InputObject] <​PSObject>​ [.
 +Cmdlet ​         Register-WmiEvent ​                                  ​Register-WmiEvent [-Class] <​String>​ [[-SourceIde.
 +Cmdlet ​         Remove-Event ​                                       Remove-Event [-SourceIdentifier] <​String>​ [-Verb.
 +Cmdlet ​         Remove-EventLog ​                                    ​Remove-EventLog [-LogName] <​String[]>​ [[-Compute.
 +Cmdlet ​         Show-EventLog ​                                      ​Show-EventLog [[-ComputerName] <​String>​] [-Verbo.
 +Cmdlet ​         Unregister-Event ​                                   Unregister-Event [-SourceIdentifier] <​String>​ [-.
 +Cmdlet ​         Wait-Event ​                                         Wait-Event [[-SourceIdentifier] <​String>​] [-Time.
 +Cmdlet ​         Write-EventLog ​                                     Write-EventLog [-LogName] <​String>​ [-Source] <St.
 +</​code>​
 +
 += get-member =
 +<code powershell>​
 +PS C:​\Users\adminshf>​ $a | get-member
 +
 +
 +   ​TypeName:​ System.Int32
 +
 +Name        MemberType Definition
 +----        ---------- ----------
 +CompareTo ​  ​Method ​    int CompareTo(System.Object value), int CompareTo(int value)
 +Equals ​     Method ​    bool Equals(System.Object obj), bool Equals(int obj)
 +GetHashCode Method ​    int GetHashCode()
 +GetType ​    ​Method ​    type GetType()
 +GetTypeCode Method ​    ​System.TypeCode GetTypeCode()
 +ToString ​   Method ​    ​string ToString(), string ToString(string format), string ToString(System.IFormatProvider pro...
 +</​code>​
 +
 +
 +<code powershell>​
 +PS C:​\Users\adminshf>​ Get-EventLog -logname system -newest 5
 +
 +   Index Time          EntryType ​  ​Source ​                ​InstanceID Message
 +   ----- ----          --------- ​  ​------ ​                ​---------- -------
 +    5270 May 07 13:51  Information Service Control M...   ​1073748860 The Software Protection service entered the stopped state.
 +    5269 May 07 13:46  Information Service Control M...   ​1073748860 The Portable Device Enumerator Service service entered the stopped state.
 +    5268 May 07 13:44  Information Service Control M...   ​1073748860 The Software Protection service entered the running state.
 +    5267 May 07 13:44  Information Service Control M...   ​1073748860 The Portable Device Enumerator Service service entered the running state.
 +    5266 May 07 13:44  Information Microsoft-Windows... ​        1503 The Group Policy settings for the user were processed successfully. New settings from 1 Group Policy objects were detec
 +</​code> ​   ​
 +
 += get-maximum =
 +<code powershell>​
 +PS C:​\Users\adminshf>​ get-process | Measure-Object -maximum -property id
 +
 +
 +Count    : 118
 +Average ​ :
 +Sum      :
 +Maximum ​ : 6120
 +Minimum ​ :
 +Property : Id
 +
 +
 +
 +PS C:​\Users\adminshf>​ (get-process | Measure-Object -maximum -property id).maximum
 +6120
 +PS C:​\Users\adminshf>​ (get-process | Measure-Object -maximum -property id).maximum | get-member
 +
 +
 +   ​TypeName:​ System.Double
 +
 +Name        MemberType Definition
 +----        ---------- ----------
 +CompareTo ​  ​Method ​    int CompareTo(System.Object value), int CompareTo(double value)
 +Equals ​     Method ​    bool Equals(System.Object obj), bool Equals(double obj)
 +GetHashCode Method ​    int GetHashCode()
 +GetType ​    ​Method ​    type GetType()
 +GetTypeCode Method ​    ​System.TypeCode GetTypeCode()
 +ToString ​   Method ​    ​string ToString(), string ToString(string format), string ToString(System.IFormatProvider provider), string ToString(string format, System.IFormatProvider provider)
 +
 +</​code>​
 +
 +
 +{{tag>​notes scripts}}
notespowershell.txt · Last modified: 2013/05/14 20:20 by sjoerd