De Wiki inetshell
Saltar a: navegación, buscar

Red / Network[editar]

Visualizar puertos con NAT[editar]

https://social.msdn.microsoft.com/Forums/en-US/1312f171-032e-4693-8db1-da9a66bc8d1f/cannot-successfully-expose-ports-to-container-host?forum=windowscontainers

Get-NetNatStaticMapping

Ejecutar comando en variable[editar]

https://stackoverflow.com/questions/3592851/executing-a-command-stored-in-a-variable-from-powershell

$Cmd = "echo 'hola mundo!'"
Invoke-Expression $Cmd

Crear funciones[editar]

http://www.computerperformance.co.uk/powershell/powershell_functions.htm

Function Unzip-File{
	Param ($InFile, $OutPath)
	If(Test-path $OutPath) {Remove-item $OutPath}
	Add-Type -assembly "system.io.compression.filesystem"
	[io.compression.zipfile]::CreateFromDirectory($InFile, $OutPath)
}

Comprimir en Zip usando librerias nativas[editar]

https://blogs.technet.microsoft.com/heyscriptingguy/2015/03/09/use-powershell-to-create-zip-archive-of-folder/

$source = "C:\fso"
$destination = "C:\fso1\FSO_Backup.zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 

Descomprimir archivos Zip usando powershell[editar]

https://github.com/teddymacn/windows-dockerfile-lab/blob/master/unzip/Dockerfile

powershell -Command "expand-archive -Path 'c:\ODTwithODAC121012.zip' -DestinationPath 'c:\ODTwithODAC121012'"

Incluyendo archivos[editar]

Try{
  . ("$PSScriptRoot\config.ps1")
}
Catch{
  Write-Host "Error reading config file, check if config.ps1 exist in directory" -ForegroundColor Red
  exit 1
}

Borrar archivos en subdirectorios[editar]

Get-ChildItem -Path 'C:\Users\ramrod\Desktop\Firefly' *.avi -recurse | foreach { Remove-Item -Path $_.FullName }

Conectividad[editar]

Acceder a archivos web[editar]

https://superuser.com/questions/344927/powershell-equivalent-of-curl

(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html")

Procesamiento de texto[editar]

Buscar texto y ejecutar si existe[editar]

https://social.technet.microsoft.com/Forums/en-US/8de39fbf-556e-4f8b-8173-cb25452913ad/selectstring-boolean?forum=winserverpowershell

if(Get-ChildItem "Cert:\LocalMachine\ca" | Select-String -pattern "CN=Virtual" -quiet){
      # process if found
}else{
     # process if not found
}

Reemplazar texto[editar]

https://www.kittell.net/code/powershell-unix-sed-equivalent-change-text-file/

PS C:\Windows\system32> "A B C D".replace(' ','')
ABCD

Argumentos[editar]

https://stackoverflow.com/questions/2157554/how-to-handle-command-line-arguments-in-powershell

param (
  [string]$server = "http://defaultserver",
  [Parameter(Mandatory=$true)][string]$username,
  [string]$password = $( Read-Host "Input password, please" )
)


Tareas programadas[editar]

Al arranque[editar]

https://devblogs.microsoft.com/scripting/use-powershell-to-create-job-that-runs-at-startup/

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:30
Register-ScheduledJob -Trigger $trigger -FilePath C:\jenkins_slave.ps1 -Name GetBatteryStatus