Tag Archives: Status

Write-Progress

NAME
    Write-Progress

SYNOPSIS
    Displays a progress bar within a Windows PowerShell command window.

SYNTAX
    Write-Progress [-Activity] <string> [-Status] <string> [[-Id] <int>] [-Completed] [-CurrentOperation <string>] [-ParentId <int>] [-PercentComplete <int>] [-SecondsRemaining <int>] [-SourceId <int>] [<CommonParameters>]

DESCRIPTION
    The Write-Progress cmdlet displays a progress bar in a Windows PowerShell command window that depicts the status of a running command or script. You can select the indicators that the bar reflects and the text that appears above and below the progress bar.

PARAMETERS
    -Activity <string>
        Specifies the first line of text in the heading above the status bar. This text describes the activity whose progress is being reported.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Completed [<SwitchParameter>]
        Indicates whether the progress bar is visible. If this parameter is omitted, Write-Progress displays progress information.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -CurrentOperation <string>
        Specifies the line of text below the progress bar. This text describes the operation that is currently taking place.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Id <int>
        Specifies an ID that distinguishes each progress bar from the others. Use this parameter when you are creating more than one progress bar in a single command. If the progress bars do not have different IDs, they are superimposed instead of being displayed in a series.

        Required?                    false
        Position?                    3
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ParentId <int>
        Identifies the parent activity of the current activity. Use the value -1 if the current activity has no parent activity.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -PercentComplete <int>
        Specifies the percentage of the activity that is completed. Use the value -1 if the percentage complete is unknown or not applicable.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SecondsRemaining <int>
        Specifies the projected number of seconds remaining until the activity is completed. Use the value -1 if the number of seconds remaining is unknown or not applicable.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceId <int>
        Identifies the source of the record.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Status <string>
        Specifies the second line of text in the heading above the status bar. This text describes current state of the activity.

        Required?                    true
        Position?                    2
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    None
        Write-Progress does not generate any output.

NOTES

        If the progress bar does not appear, check the value of the $ProgressPreference Variable. If the value is set to SilentlyContinue, the progress bar is not displayed. For more information about Windows PowerShell preferences, see about_preference_variables.

        The parameters of the cmdlet correspond to the properties of the ProgressRecord class (System.Management.Automation.ProgressRecord). For more information, see the ProgressRecord topic in the Windows PowerShell Software Development Kit (SDK).

    ————————– EXAMPLE 1 ————————–

    C:\PS>for ($i = 1; $i -lt 101; $i++ )
    {for ($j=0;$j -lt 10000;$j++) {} Write-Progress -Activity “Search in Progress” -Status “% Complete:” -PercentComplete $i;}

    Description
    ———–
    This command displays the progress of two nested For loops. The first loop counts to 100. For each increment of that loop, the second loop counts to 10,000.
    The Write-Progress command includes a status bar heading (“activity”), a status line, and the Variable $i (the counter in the For loop), which indicates the relative completeness of the task.

    ————————– EXAMPLE 2 ————————–

    C:\PS>for($i = 1; $i -lt 101; $i++ ) {Write-Progress -Activity Updating -Status progress-> -PercentComplete $i -CurrentOperation OuterLoop} for($i = 1; $i -lt 101; $i++ ) {Write-Progress -Activity Updating -Status progress -PercentComplete $i -Id 1 -CurrentOperation InnerLoop}

    Updating
     progress ->
     [oooooooooooooooooo                                                 ]

     OutsideLoop

    Updating
     progress
     [oooooooooooooooooo                                                    ]

     InnerLoop

    Description
    ———–
    This example displays the progress of two nested For loops, each of which is represented by a progress bar.

    The Write-Progress command for the second progress bar includes the Id parameter that distinguishes it from the first progress bar. Without the Id parameter, the progress bars would be superimposed on each other instead of being displayed one below the other.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$events = Get-Eventlog -logname system

    C:\PS> $events | ForEach-Object -begin {clear-host;$i=0;$out=””} `
    -process {if($_.message -like “*bios*”) {$out=$out + $_.Message};
    $i = $i+1;`
    Write-Progress -Activity “Searching Events” `
    -Status “Progress:” -PercentComplete ($i/$events.count*100)} `
    -end {$out}

    Description
    ———–
    This command displays the progress of a command to find the string “bios” in the System event log.

    In the first line of the command, the Get-EventLog cmdlet gets the events in the System log and stores them in the $events Variable.

    In the second line, the events are piped to the ForEach-Object cmdlet. Before processing begins, the Clear-Host cmdlet is used to clear the screen, the $i counter Variable is set to zero, and the $out output Variable is set to the empty string.

    In the third line, which is the Process script block of the ForEach-Object cmdlet, the cmdlet searches the message property of each incoming object for “bios”. If the string is found, the message is added to $out.

    In the fourth line, the $i counter Variable is incremented to record that another event has been examined.

    The fifth line uses the Write-Progress cmdlet with values for the Activity and Status text fields that create the first and second lines of the progress bar heading, respectively. The PercentComplete parameter value is calculated by dividing the number of events that have been processed ($i) by the total number of events retrieved ($events.count) and then multiplying that result by 100.

    In the last line, the End parameter of the ForEach-Object cmdlet is used to display the messages that are stored in the $out Variable.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113428
    Write-Verbose
    Write-Error
    Write-Host
    Write-Debug
    Write-Output
    Write-Warning

Set-Service

NAME
    Set-Service

SYNOPSIS
    Starts, stops, and suspends a service, and changes its properties.

SYNTAX
    Set-Service [-Name] <string> [-Description <string>] [-DisplayName <string>] [-PassThru] [-StartupType {Automatic | Manual | Disabled}] [-Status <string>] [-ComputerName <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

    Set-Service [-Description <string>] [-DisplayName <string>] [-InputObject <ServiceController>] [-PassThru] [-StartupType {Automatic | Manual | Disabled}] [-Status <string>] [-ComputerName <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Set-Service cmdlet changes the properties of a local or remote service, including the status, description, display name, and start mode. You can use this cmdlet to start, stop, or suspend (pause) a service. To identify the service, enter its service name or submit a service object, or pipe a service name or service object to Set-Service.

PARAMETERS
    -ComputerName <string[]>
        Specifies one or more computers. The default is the local computer.

        Type the NetBIOS name, an IP address, or a fully qualified domain name of a remote computer. To specify the local computer, type the computer name, a dot (.), or “localhost”.

        This parameter does not rely on Windows PowerShell remoting. You can use the ComputerName parameter of Set-Service even if your computer is not configured to run remote commands.

        Required?                    false
        Position?                    named
        Default value                local computer
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Description <string>
        Specifies a new description for the service.

        The service description appears in Services in Computer Management. Description is not a property of the ServiceController object that Get-Service gets. To see the service description, use Get-WmiObject to get a Win32_Service object that represents the service.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -DisplayName <string>
        Specifies a new display name for the service.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <ServiceController>
        Specifies a ServiceController object that represents the service to be changed. Enter a Variable that contains the object, or type a command or expression that gets the object, such as a Get-Service command. You can also pipe a service object to Set-Service.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Name <string>
        Specifies the service name of the service to be changed. Wildcards are not permitted. You can also pipe a service name to Set-Service.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -PassThru [<SwitchParameter>]
        Returns objects that represent the services that were changed. By default, this cmdlet does not generate any output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -StartupType <ServiceStartMode>
        Changes the start mode of the service. Valid values for StartupType are:

        — Automatic: Start when the system starts.
        — Manual: Starts only when started by a user or program.
        — Disabled: Cannot be started.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Status <string>
        Starts, stops, or suspends (pauses) the services. Valid values are:

        — Running: Starts the service.
        — Stopped: Stops the service.
        — Paused: Suspends the service.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.ServiceProcess.ServiceController, System.String
        You can pipe a service object or a string that contains a service name to Set-Service.

OUTPUTS
    None
        This cmdlet does not return any objects.

NOTES

        To use Set-Service on Windows Vista and later versions of Windows, start Windows PowerShell with the “Run as administrator” option.

        Set-Service can control services only when the current user has permission to do so. If a command does not work correctly, you might not have the required permissions.

        To find the service names and display names of the services on your system, type “Get-Service“. The service names appear in the Name column and the display names appear in the DisplayName column.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Set-Service -Name lanmanworkstation -DisplayName “LanMan Workstation”

    Description
    ———–
    This command changes the display name of the lanmanworkstation service to “LanMan Workstation”. (The default is “Workstation”.)

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-WmiObject win32_service -filter “name = ‘SysmonLog'”

    ExitCode : 0
    Name     : SysmonLog
    ProcessId : 0
    StartMode : Manual
    State     : Stopped
    Status    : OK

    C:\PS> Set-Service sysmonlog -StartupType automatic

    C:\PS> Get-WmiObject win32_service -filter “name = ‘SysmonLog'”

    ExitCode : 0
    Name     : SysmonLog
    ProcessId : 0
    StartMode : Auto
    State     : Stopped
    Status    : OK

    C:\PS> Get-WmiObject win32_service | Format-Table Name, StartMode -auto

    Name                                 StartMode
    —-                                 ———
    AdtAgent                             Auto
    Alerter                             Disabled
    ALG                                 Manual
    AppMgmt                             Manual
    …

    Description
    ———–
    These commands get the startup type of the Performance Logs and Alerts (SysmonLog) service, set the start mode to automatic, and then display the result of the change.

    These commands use the Get-WmiObject cmdlet to get the Win32_Service object for the service, because the ServiceController object that Get-Service returns does not include the start mode.

    The first command uses the Get-WmiObject cmdlet to get the Windows Management Instrumentation (WMI) object that represents the SysmonLog service. The default output of this command displays the start mode of the service.

    The second command uses Set-Service to change the start mode to automatic. Then, the first command is repeated to display the change.

    The final command displays the start mode of all services on the computer.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Set-Service -Name Schedule -ComputerName S1 -Description “Configures and schedules tasks.”

    C:\PS> Get-WmiObject win32_service -ComputerName s1 | Where-Object {$_.Name -eq “Schedule”} | Format-List Name, Description

    Description
    ———–
    These commands change the description of the Task Scheduler service on the S1 remote computer and then display the result.

    These commands use the Get-WmiObject cmdlet to get the Win32_Service object for the service, because the ServiceController object that Get-Service returns does not include the service description.

    The first command uses a Set-Service command to change the description. It identifies the service by using the service name of the service, “Schedule”.

    The second command uses the Get-WmiObject cmdlet to get an instance of the WMI Win32_Service that represents the Task Scheduler service. The first element in the command gets all instances of the Win32_service class.

    The pipeline operator (|) passes the result to the Where-Object cmdlet, which selects instances with a value of “Schedule” in the Name property.

    Another pipeline operator sends the result to the Format-List cmdlet, which formats the output as a list with only the Name and Description properties.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Set-Service winrm -Status Running -PassThru -ComputerName Server02

    Description
    ———–
    This command starts the WinRM service on the Server02 computer. The command uses the Status parameter to specify the desired status (“running”) and the PassThru parameter to direct Set-Service to return an object that represents the WinRM service.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Get-Service schedule -ComputerName S1, S2 | Set-Service -Status paused

    Description
    ———–
    This command suspends the Schedule service on the S1 and S2 remote computers. It uses the Get-Service cmdlet to get the service. A pipeline operator (|) sends the service to the Set-Service cmdlet, which changes its status to “Paused”.

    ————————– EXAMPLE 6 ————————–

    C:\PS>$s = Get-Service schedule

    C:\PS> Set-Service -inputobject $s -Status stopped

    Description
    ———–
    These commands stop the Schedule service on the local computer.

    The first command uses the Get-Service cmdlet to get the Schedule service. The command saves the service in the $s Variable.

    The second command uses the Set-Service cmdlet to change the status of the Schedule service to “Stopped”. It uses the InputObject parameter to submit the service stored in the $s Variable, and it uses the Status parameter to specify the desired status.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113399
    Get-Service
    Start-Service
    Stop-Service
    Restart-Service
    Resume-Service
    Suspend-Service
    New-Service