Recettes PowerShell

De Linuxmemo.

(Différences entre les versions)
(MD5sum)
(Éditer un fichier)
 
(10 versions intermédiaires masquées)
Ligne 22 : Ligne 22 :
  expand-archive -path 'c:\users\john\desktop\iislogs.zip' -destinationpath '.\unzipped'
  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
 +
==Random==
 +
Get-Random
 +
==Longueur du path courant==
 +
pwd | Measure-Object -Character
 +
==Éditer un fichier==
 +
$newfile = (Get-Content file.txt | Foreach-Object {$_ -replace ${stringToReplace}, ${replacementString}} )
 +
$newfile | Out-File file.txt
 +
==Compression / décompression (zip)==

Version actuelle en date du 22 janvier 2021 à 10:09

Sommaire

[modifier] Exécuter une commande stocké dans une variable

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

[modifier] 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

[modifier] MD5sum

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

[modifier] 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'

[modifier] Infos matériel

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

[modifier] 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
}

[modifier] 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

[modifier] Random

Get-Random

[modifier] Longueur du path courant

pwd | Measure-Object -Character

[modifier] Éditer un fichier

$newfile = (Get-Content file.txt | Foreach-Object {$_ -replace ${stringToReplace}, ${replacementString}} )
$newfile | Out-File file.txt

[modifier] Compression / décompression (zip)

Outils personnels