Ir al contenido principal

Encrypting PowerShell Scripts

Sometimes, you may want to hide the code of your PowerShell script in order to protect passwords contained within the code. One way to safely encrypt PowerShell script is by converting it into a secure string. You must first create a sample script you would like to encrypt and save it as $home\original.ps1.

Next, use the following function to encrypt it into a file called secure.bin:

function Encrypt-Script($path, $destination) {
  $script = Get-Content $path | Out-String
  $secure = ConvertTo-SecureString $script -asPlainText -force
  $export = $secure | ConvertFrom-SecureString
  Set-Content $destination $export
  "Script '$path' has been encrypted as '$destination'"}
 
Encrypt-Script $home\original.ps1 $home\secure.bin

When you now look at secure.bin, all content is safely encrypted:



Get-Content $home\secure.bin

To execute the script, you need to decrypt it. Here is the second part, which reads in an encrypted script and executes it:



function Execute-EncryptedScript($path) {
  trap { "Decryption failed"; break }
  $raw = Get-Content $path
  $secure = ConvertTo-SecureString $raw
  $helper = New-Object system.Management.Automation.PSCredential("test", $secure)
  $plain = $helper.GetNetworkCredential().Password
  Invoke-Expression $plain
}
 
Execute-EncryptedScript $home\secure.bin

This approach allows you to use your personal identity as secret key. As a result, the person who encrypted the script is the only one who can decrypt and execute it- a great way to keep personal scripts secret.



Fuente: http://powershell.com/cs/blogs/tips/archive/2009/04/08/encrypting-powershell-scripts.aspx

Comentarios

Entradas populares de este blog

Get SharePoint Online Site and SubSites permission using PowerShell

The below PowerShell script retrieves the following for the given SharePoint Online Site All the Sub-site's URL Security group attached with each Sub-site with their permission level Prerequisites: This PowerShell script uses the latest version of SharePoint Online PnP Module. Download the installer from https://github.com/SharePoint/PnP-PowerShell/releases  Install-Module SharePointPnPPowerShellOnline  Install-Module - Name ' SharePointPnP.PowerShell.Commands.Files.Recurse ' function  connect - site( $webs , $creds ){    Connect - PNPonline  - Url  $webs   - Credentials  $cred     }    function  get - sitepermission( $web , $cred ){    $rec =@()    connect - site  - webs  $web   - creds  $cred     if ( $web   - eq  $parentsitename )  {  #Write-Host "Parent site permission" $web   $Pgroups =G...

Conexión desde casa a una VPN sin perder salida a internet

Solución, asumiendo que estas en Windows: Panel de Control, Conexiones de Red. Clic derecho en la VPN, dale a propiedades. Anda a la pestaña de "Funciones de Red" y selecciona Protocolo Internet TCP/IP y clic en el botón "Propiedades". Ahora hazle clic al botón "Opciones Avanzadas..."En la pestaña "General", desmarca la opción que dice "Usar la puerta de enlace predeterminada en la red remota". Dale a aceptar a todas las ventanitas de opción, y ahora conéctate a la VPN nuevamente. Con eso deberías entrar a la VPN sin perder la conexión local de tu red e internet.

Event ID 8031 The uri endpoint information may be stale

An exception occurred while updating addresses for connected app {6783ce5e-c88h-4021-8d5b-12614875cbfa_b79f19ab-1d40-4824-9911-3466cf8b070a}. The uri endpoint information may be stale. System.InvalidOperationException: The requested application could not be found.    at Microsoft.SharePoint.SPTopologyWebServiceApplicationProxy.ProcessCommonExceptions(Uri endpointAddress, String operationName, Exception ex, SPServiceLoadBalancerContext context)    at Microsoft.SharePoint.SPTopologyWebServiceApplicationProxy.ExecuteOnChannel(String operationName, CodeBlock codeBlock)    at Microsoft.SharePoint.SPTopologyWebServiceApplicationProxy.GetEndPoints(Guid serviceId)    at Microsoft.SharePoint.SPConnectedServiceApplicationAddressesRefreshJob.Execute(Guid targetInstanceId) After de-commissioning some SharePoint servers, you might notice the above error on other WFEs /Application server’s event viewer . It appears that the SharePoint still has a reference...