Recettes PowerShell

De Linuxmemo.

Sommaire

Exécuter une commande stocké dans une variable

Invoke-Command -ComputerName $Ip -Credential $login -ScriptBlock { Param( $LocalVariable ); Invoke-Expression -Command $LocalVariable } -ArgumentList $commande

Tester l’exécution administrateur

function Test-Administrator  
{  
   $user = [Security.Principal.WindowsIdentity]::GetCurrent();
   (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  
}
Test-Administrator
False

MD5sum

(Get-FileHash -Path ${repbase}${repdiffusion}\${nomfichier} -Algorithm MD5).hash

ZIP / UNZIP

compress-archive -path 'c:\wwwroot\logs' -destinationpath '.\logs.zip' -compressionlevel optimal
compress-archive -path “c:\wwwroot\logs\latest” -destinationpath “c:\wwwroot\logs\logs.zip” -update -compressionlevel optimal
compress-archive test.txt test.zip
expand-archive -path 'c:\users\john\desktop\iislogs.zip' -destinationpath '.\unzipped'

Infos matériel

(Get-WmiObject -Class Win32_ComputerSystem).Manufacturer
(Get-WmiObject -Class Win32_ComputerSystem).Model
(Get-WmiObject win32_computersystem).SystemSKUNumber

Uptime

function Get-Uptime {
  $os = Get-WmiObject win32_operatingsystem
  $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
  $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes" 
  Write-Output $Display
}

Configuration réseau

  • info
Get-NetAdapter
Get-NetIPConfiguration
  • config
New-NetIPAddress –InterfaceIndex 12 –IPAddress 192.168.1.20 –PrefixLength 24 –DefaultGateway 192.168.1.254
ou
Set-NetIPAddress –InterfaceIndex 12 –IPAddress 192.168.1.20 –PrefixLength 24 –DefaultGateway 192.168.1.254
  • activer/désactiver DHCP
Set-NetIPInterface -InterfaceIndex 12 -Dhcp {Enabled/Disabled}
  • ajout DNS
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 8.8.8.8
Get-DnsClientServerAddress -InterfaceIndex 12
  • arp
Get-NetNeighbor
  • route
Get-NetRoute
  • netstat
Get-NetTCPConnection
Outils personnels