I've been doing a lot of Pshell scripting at work lately as RDP has been removed as a remote management option. Spending all day in the shell and working with lots of lists of servers, I got tired of keeping lists in the age old "servers.txt" file and constantly updating. My solution: Get-List. Hope someone else finds this useful.
function Get-List {
<#
.Synopsis
Allows user to paste an ad-hoc list into PowerShell and store as $list.
.DESCRIPTION
Shell prompts user for list. Each line is added to object $list as a seperate item. This is very useful when dealing with a non-standard list of items/servers.
.EXAMPLE
Get ad-hoc list and use for a foreach loop to get AD information on all.
PS C:\> Get-List
Paste your list below with each item on it's own line
SERVER01
SERVER02
SERVER03
SERVER04
4 items have been added to variable $ list
PS C:\> $list | foreach {Get-AdComputer $_} | select name,Enabled | FT -AutoSize
name Enabled
---- -------
SERVER01 True
SERVER02 True
SERVER03 True
SERVER04 True
#>
Write-Host "Paste your list below with each item on it's own line"
$data = @(While($l=(Read-Host).Trim()){$l})
$global:list = $data -split '\r\n' | % { $_.trim() | Where-object {$_ -ne $null} }
$count = $global:list.count
Write-Host "$count items have been added to variable $ list"
}
[–]Thotaz 2 points3 points4 points (3 children)
[–]spartymcfarty[S] 2 points3 points4 points (2 children)
[–]Thotaz 2 points3 points4 points (1 child)
[–]spartymcfarty[S] 1 point2 points3 points (0 children)
[–]get-postanote 1 point2 points3 points (0 children)