The script executes a commands stored in a variable (array). The output is conveniently collected and stored in a log file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[string]$log = "c:\temp\log.txt" [array]$commands = @( 'if (!([System.Environment]::OSVersion.Version).Major -ge 6) {"win xp"} else {"win 7 or higher"}'; 'gps | select id,@{Name="WorkingSet(MB)";Expression={"{0:N1} M" -f($_.WorkingSet/1mb)}},name,company | sort name | ft'; 'netstat -noa -p TCP | findstr /i close' ) foreach ($command in $commands) { $command = [scriptblock]::Create($command) [array]$output = & $command $output | fl | Out-File -FilePath $log -Encoding UTF8 -Append } |