De Linuxmemo.
[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
(Get-FileHash -Path ${repbase}${repdiffusion}\${nomfichier} -Algorithm MD5).hash
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
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
Get-NetAdapter
Get-NetIPConfiguration
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
Set-NetIPInterface -InterfaceIndex 12 -Dhcp {Enabled/Disabled}
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 8.8.8.8
Get-DnsClientServerAddress -InterfaceIndex 12
Get-NetNeighbor
Get-NetRoute
Get-NetTCPConnection
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)