Service_DisplayName = $service.DisplayName $custom_array += New-Object PSObject -Property = $service.Name # Setting up custom array utilizing a PSObject # Looping through all of the running services and adding them to the custom array $services = Get-Service | Select Name, Status, DisplayName, StartType | Where-Object # Getting Running services on the system and sending them to the services variable $custom_array = # Creating an empty array to populate data in In this example, we utilize the Stop-Service command inside of a Foreach loop in order to stop the multiple selected services. The values selected will be stored in the variable specified ( $multiple_output). All the values can be selected by hitting CTRL + A. Multiple values can be selected by holding CTRL while clicking on the desired values. We set the title for the Out-GridView window again, but this time we set the outputmode to Multiple which means that there will be an “OK” and “Cancel” button while allowing the user to select multiple values. The custom array is not entirely necessary for this simple example of stopping services but it could be useful with more complex scripts where you need to combine data from multiple sources. The custom array will then be piped to Out-GridView. The array will be populated with all of the Running services from the Foreach loop. The services variable will be populated from the Get-Service command and then piped to Where-Object which will only include services with a “Running” status. Method 3: Input from custom array, multiple outputĪn even slightly more advanced option will use a custom array to hold all of the data from the services variable. Selecting a single service and then clicking the “OK” button will cause the selected service to be stopped. Stop-Service $single_output.Name # Finally the service selected will be stopped Write-Host "Stopping service" $single_output.Name -BackgroundColor Cyan -ForegroundColor Black $single_output = $services | Out-GridView -Title "Select the service that you want to stop" -OutputMode Single # Sending the services variable to Out-GridView, the single service selected will be sent to single_output variable $services = Get-Service # Getting all services on the system and sending them to the services variable We have set a title for the Out-GridView window and set the outputmode to None which means that there is no “OK” or “Cancel” button and no data will be passed along to the next command. In the below example, we do a Get-Service command to get all the services on a given system, then we pipe those results to Out-GridView to be viewed. The most basic function of Out-GridView is to just simply display data to the user. Method 1: Input directly from command, display results only We will cover the methods that I utilize most frequently. There are several ways of getting data into Out-GridView, as well as multiple ways of outputting data from Out-GridView. Any additional instructions should be placed here as well. This is where you put your descriptive name for the Out-GridView window.None: Nothing will be able to be selected, use this if you just want a report.Multiple: This allows you to select multiple objects at a time use this if the next command can handle multiple values as input.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |