Archivi categoria: powershell

Docker PowerShell commands and AutoHotKey

Commands

I’m doing quite a few exercies with Docker in the last fiew days. I start to notice that I need a toolkit of useful commands to cleanup all the containers or images that I move around.

I record here, for future memory, a collection of useful PowerShell script.

Bonus point: an AHK (AutoHotKey) script to be able to rapidly insert those commands with just a few chars!


# Maintenance commands or utils
docker container prune # Remove all stopped containers
docker volume prune # Remove all unused volumes
docker image prune # Remove unused images
docker system prune # All of the above, in this order: containers, volumes, images
docker ps -a -q | % { docker rm $_ } # Remove all containers
docker images -q | % { docker rmi $_ } # Remove all images
docker volume rm $(docker volume ls -f dangling=true -q) # Remove all volumes
https://gist.github.com/phenixita/6b3b65e58cf19ff38d9e4f44e6f79a54.js

How to create a self-signed certificate with PowerShell

When we develop web-application or experiment with some technologies it’s quite common to need a certificate. A certificate for real world scenarios can be expensive and it’s over-engineering for testing purposes. So with this simple PowerShell script (on Windows) we can create a self-signed certificate for development enviroments.

$cert = New-SelfSignedCertificate -DnsName mydemowebapp.net -CertStoreLocation cert:\LocalMachine\My

$pwd = ConvertTo-SecureString -String "MyPassword" -Force -AsPlainText

Export-PfxCertificate -Cert $cert -FilePath C:\temp\cert.pfx -Password $pwd

With these few line of codes we create and store a self-signed certificate in the Windows Certificate Store. With the last line (Export-PfxCertificate) we export the certificate for furher use for example to do some experiments with cloud providers.

Reference

GitHub Gist

Aggiornare un servizio di Windows via Powershell

Avete implementato un servizio di Windows e, come è consuetudine, avete scoperto un bug e lo avete risolto.

Ora bisogna aggiornare il servizio.

Siccome il mio codice ha spesso dei bug mi è capitato di dover ripetere più volte questo processo:

  • Compila la dll con la correzione;
  • Spegni il servizio nel server;
  • Sostituisci la dll;
  • Riavvia il servizio.

All’n-esima volta mi sono stufato e ho creato uno script powershell che recita:

Set-Service -Name MadLab.Router -ComputerName 125.0.0.254 -Status Stopped

Copy-Item -Path .\MadLab.Router.Servizio\bin\Release\* -Destination \\125.0.0.254\madlab-ame\MadLab.Router\ -Exclude *config*,*vshost*, *.pdb

Set-Service -Name MadLab.Router -ComputerName 125.0.0.254 -Status Running