Update
This commit is contained in:
parent
4a5175593f
commit
3d783d5fa1
43
Encrypt-Script.ps1
Normal file
43
Encrypt-Script.ps1
Normal file
@ -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
|
33
Execute-Script.ps1
Normal file
33
Execute-Script.ps1
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user