diff --git a/Encrypt-Script.ps1 b/Encrypt-Script.ps1 new file mode 100644 index 0000000..ba559d4 --- /dev/null +++ b/Encrypt-Script.ps1 @@ -0,0 +1,43 @@ +<# + .EXAMPLE + .\Encrypt-Script.ps1 -Path "C:\scripts" -ScriptName 'script.ps1' -Credential (Get-credential) +#> + +param ( + [Parameter(Mandatory)] + [String]$Path, + [Parameter(Mandatory)] + [String]$ScriptName, + [Parameter(Mandatory)] + [System.Management.Automation.PSCredential]$Credential +) + +$scriptPath = "$Path\$ScriptName" +$DestinationSctiptPath = $Path + "\" + [System.IO.Path]::GetFileNameWithoutExtension($ScriptName) + ".bin" + +$testPath = Test-Path -Path $Path +if ($testPath -eq $false) { + Write-Error "Path '$path' does not exists" + break +} +else { + $testFilePath = Test-Path -Path $scriptPath + if ($testFilePath -eq $false) { + Write-Error "Path '$scriptPath' does not exists" + break + } +} +$credentialTest = ($Credential.GetNetworkCredential().Password).Length +if ($credentialTest -eq $null) { + Write-Error "Password lenght used is equeal 0" + break +} + +function Encrypt-Script ($ScriptPath, $DestinationSctiptPath, [SecureString]$Password) { + $script = Get-Content $ScriptPath | Out-String + $secure = ConvertTo-SecureString -String $script -AsPlainText -Force + $export = $secure | ConvertFrom-SecureString -SecureKey $Password + Set-Content $DestinationSctiptPath $export + "Script '$ScriptPath' has been encrypted as '$DestinationSctiptPath'" +} +Encrypt-Script -ScriptPath $scriptPath -DestinationSctiptPath $DestinationSctiptPath -Password $Credential.Password diff --git a/Execute-Script.ps1 b/Execute-Script.ps1 new file mode 100644 index 0000000..321414e --- /dev/null +++ b/Execute-Script.ps1 @@ -0,0 +1,33 @@ +<# + .EXAMPLE + .\Execute-Script.ps1 -BinFilePath "C:\scripts\script.bin" -Credential (Get-credential) +#> + +param ( + [Parameter(Mandatory)] + [String]$BinFilePath , + [Parameter(Mandatory)] + [System.Management.Automation.PSCredential]$Credential +) + +$testPath = Test-Path -Path $BinFilePath +if ($testPath -eq $false) { + Write-Error "Path '$BinFilePath' does not exists" + break +} +$credentialTest = ($Credential.GetNetworkCredential().Password).Length +if ($credentialTest -eq $null) { + Write-Error "Password lenght used is equeal 0" + break +} + +function Execute-EncryptedScript($BinFilePath, [SecureString]$Password) { + trap { "Decryption failed"; break } + $raw = Get-Content $BinFilePath + $secure = ConvertTo-SecureString $raw -SecureKey $Password + $helper = New-Object system.Management.Automation.PSCredential("test", $secure) + $plain = $helper.GetNetworkCredential().Password + Invoke-Expression $plain +} + +Execute-EncryptedScript -BINFilePath $BinFilePath -Password $Credential.Password