Script_encryption/Execute-Script.ps1
2023-11-29 17:05:43 +01:00

34 lines
1.0 KiB
PowerShell

<#
.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