Advertisement
adziavitski

Examination powershell scripting task

Aug 5th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #PowerShell Version
  2. #$PSVersionTable
  3. #Name                           Value                                                                                                                                                                                                                
  4. #----                           -----                                                                                                                                                                                                                
  5. #PSVersion                      4.0                                                                                                                                                                                                                  
  6. #WSManStackVersion              3.0                                                                                                                                                                                                                  
  7. #SerializationVersion           1.1.0.1                                                                                                                                                                                                              
  8. #CLRVersion                     4.0.30319.34209                                                                                                                                                                                                      
  9. #BuildVersion                   6.3.9600.17400                                                                                                                                                                                                        
  10. #PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                                                                  
  11. #PSRemotingProtocolVersion      2.2          
  12.  
  13.  
  14. #Раздел с входными параметрами
  15. param(
  16. [string]$t
  17. )
  18.  
  19. #Определяем глобальные переменные
  20. $fl_content_array=Get-Content .\GTS_LINUX-MULTI-001A.results #
  21. [array]$buf_array = @()
  22. [array]$output = @()
  23.  
  24.  
  25. #Функция разбора файла, ищем нужные метки, записываем нужные строки в массив, после чего передаём массив нужных строк во вне функции.
  26. function fl_to_arry {
  27. $fl_content_counter=0
  28. $fl_to_str_mark="false"
  29. $fl_to_str_start_num="null"
  30. [array]$str_array=@()
  31. $str_array_indx=0
  32.  
  33.     $fl_content_array| ForEach-Object -Process {
  34.         if ($_ -contains "`[MEASURED_VALUE`]")
  35.         {
  36.             $fl_to_str_mark="true"
  37.             $fl_to_str_start_num=$fl_content_counter
  38.         }
  39.  
  40.         if ($_ -contains "`[MEASURED_VALUE_EOF`]")
  41.         {
  42.             $fl_to_str_mark="false"
  43.             $fl_to_str_start_num=$fl_content_counter
  44.         }
  45.    
  46.         if ($fl_to_str_mark -eq "true" -and $fl_to_str_start_num -ne $fl_content_counter)
  47.         {
  48.             $str_array += $_
  49.             $str_array_indx++
  50.         }
  51.    
  52.  
  53.         $fl_content_counter++
  54.     }
  55.  
  56.  
  57.  return $str_array
  58. }
  59.  
  60. #Функция разбора переданного fl_to_arry массива строк. Проверяем подстроки на метки директорий, линков, файлов терминалов, обычных файлов и дёргаем то что нужно на запись в массив на вывод.
  61. function array_to_output ([string]$ato_type, [array]$ato_str_array)
  62. {
  63. [array]$ato_unm_grp = New-Object 'object[,]' 4,5
  64.  
  65.     if ($ato_type -match "d")
  66.     {
  67.        
  68.         $ato_str_array| ForEach-Object -Process {
  69.             if ($_ -match "dr")
  70.             {
  71.                 $ato_str="$_" -split ";"
  72.                 $usrname=$ato_str[1]
  73.                 $groupname=$ato_str[2]
  74.                 $ato_unm_grp += ,@($usrname,$groupname)
  75.             }
  76.         }
  77.     }
  78.  
  79.     elseif ($ato_type -match "f")
  80.     {
  81.         $ato_str_array| ForEach-Object -Process {
  82.             if ($_ -match "-r" -and $_ -notmatch "cr" -and $_ -notmatch "lr")
  83.             {
  84.                 $ato_str="$_" -split ";"
  85.                 $usrname=$ato_str[1]
  86.                 $groupname=$ato_str[2]
  87.                 $ato_unm_grp += ,@($usrname,$groupname)
  88.             }        
  89.         }
  90.    
  91.     }
  92.  
  93.     else
  94.     {
  95.         $ato_str_array| ForEach-Object -Process {
  96.            $ato_str="$_" -split ";"
  97.            $usrname=$ato_str[1]
  98.            $groupname=$ato_str[2]
  99.            $ato_unm_grp += ,@($usrname,$groupname)
  100.         }
  101.     }
  102.     $out=$ato_unm_grp | sort-object | Get-Unique -AsString
  103.     return $out
  104. }
  105.  
  106. #Блок вызова функций и обработки их вывода.
  107.  
  108. $buf_array=fl_to_arry
  109. $output=array_to_output $t $buf_array
  110.  
  111.  
  112. if ($t -match "d")
  113. {
  114.     Write-Host "Output Filtered by directory"
  115.     $output| foreach -Process {
  116.          Write-Host $_
  117.     }
  118. }
  119. elseif ($t -match "f")
  120. {
  121.     Write-Host "Output Filtered by file"
  122.     $output| foreach -Process {
  123.          Write-Host $_
  124.          }
  125. }
  126. else
  127. {
  128.     $t="a"
  129.     Write-Host "Output not filtered"
  130.     $output| foreach -Process {
  131.          Write-Host $_
  132.          }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement