Advertisement
lincruste

PowerShell snippets

Nov 5th, 2018
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////////////
  2. Get-Process |
  3. Select-Object Name,
  4.               ID,
  5.               @{n='VirtualMemory(MB)';e={$PSItem.VM/ 1MB}},@{n='PagedMemory(MB)';e={$PSItem.PM/ 1MB}}
  6. ///////////////////
  7. Get-Volume | Where-Object -Filter { $PSItem.HealthStatus -eq 'Healthy' -or $PSItem.SizeRemaining -gt 100MB }
  8. ///////////////////
  9. Get-Volume | Where-Object -Filter { $_.HealthStatus -eq 'Healthy' -or $_.SizeRemaining -gt 100MB }
  10.  
  11. ///////////////////
  12.  
  13. Get-Service -ComputerName "localhost", "WIN-THODEPOGFK1"| Format-Table -Property MachineName, Status, Name, DisplayName -auto
  14.  
  15. ///////////////////
  16.  
  17. function WLog
  18. {
  19.     param([string]$Log, [bool]$bligne=$false)
  20.  
  21.     if ($bligne)
  22.     {
  23.         Add-Content -Path ($Destination +'Log.log') -Value '---------------------------------------------------'
  24.         Write-Host '---------------------------------------------------'
  25.         if ($bDebug)
  26.         {
  27.             Read-Host $Log
  28.         }
  29.     }
  30.     [string]$Dat = Get-Date
  31.     $log2 = $Dat + ' -> ' + $Log
  32.     Add-Content -Path ($Destination +'Log.log') -Value $Log2
  33.     Write-Host $Log2
  34. }
  35.  
  36. --------------------------------------------------------------------------------------
  37.  
  38. Try
  39. {
  40.     $Result = Copy-Item -Path 'C:\Test\toto.txt' D:\Perso -ErrorAction Stop
  41. }
  42. catch
  43. {
  44.     write-host $Error[0]
  45. }
  46.  
  47. Write-Host 'Fin'
  48.  
  49. --------------------------------------------------------------------------------------
  50.  
  51. function Copy-File {
  52.     param( [string]$from, [string]$to)
  53.     $ffile = [io.file]::OpenRead($from)
  54.     $tofile = [io.file]::OpenWrite($to)
  55.     Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0
  56.     try {
  57.         [byte[]]$buff = new-object byte[] 4096
  58.         [int]$total = [int]$count = 0
  59.         do {
  60.             $count = $ffile.Read($buff, 0, $buff.Length)
  61.             $tofile.Write($buff, 0, $count)
  62.             $total += $count
  63.             if ($total % 1mb -eq 0) {
  64.                 Write-Progress -Activity "Copying file" -status "$from -> $to" `
  65.                    -PercentComplete ([int]($total/$ffile.Length* 100))
  66.             }
  67.         } while ($count -gt 0)
  68.     }
  69.     finally {
  70.         $ffile.Dispose()
  71.         $tofile.Dispose()
  72.         Write-Progress -Activity "Copying file" -Status "Ready" -Completed
  73.     }
  74. }
  75.  
  76. --------------------------------------------------------------------------------------
  77.  
  78. ///////////////////
  79.  
  80. 'Date: 02/09/2013' –match '^Date:\s(?<date>(?<jour>\d{2})/>(?<mois>\d{2})/>(?<annee>\d{4}))$'
  81.  
  82. [string[]]$Jours='Lu','Ma','Me','Je','Ve','Sa','Di'
  83. $Jours[0]
  84. $Jours[-1]
  85. $jours.Length
  86. $jours+='Dredi'
  87. $Jours[-1]
  88.  
  89.  
  90. If($res|Where {$_ -match 'IPv4[ \.]+:[ ]+(\d+\.\d+\.\d+\.\d+)'}) {
  91.     $Matches[1]
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement