교육
base64 처리 PowerShell script
요시아
2022. 5. 11. 15:27
base64 처리하는 것을 찾아보다가...
offline에서 사용하는 PowerShell script를 만들었다.
# 1. 관리자 권한으로 PowerShell 실행.
# 2. 실행 정책을 확인하기 위해 'ExecutionPolicy' 명령어 실행.
# 3. 'Restricted'라고 표시되고 있으면, 현재 정책 때문에 위에 오류가 발생한 것을 알 수 있습니다.
# 4. 스크립트를 허용하기 위해 'Set-ExecutionPolicy Unrestricted' 명령어 실행.
# -> 변경하시겠습니까? [Y] 예(Y)
# 5. 변경된 실행 정책을 확인하기 위해 'ExecutionPolicy' 명령어 실행
# 6. 'Unrestricted'라고 표시되면 스크립트가 허용되는 것을 알 수 있습니다.
# $args
# echo $args.count
# -t : e-> encode(default) d-> decode
[CmdLetbinding()]
param (
[parameter(Mandatory=$true)]
[string]
$file_path,
[string]$t="e"
)
#echo $file_path
#echo $t
if ( !(Test-Path $file_path -PathType Leaf) ){
Write-Error ('The file does not exist.('+$file_path+')') -ErrorAction Stop
}
$read_file = ($PWD.Path+[System.IO.Path]::DirectorySeparatorChar+$file_path)
#echo $convert_path
if( $t -eq "e"){ #base64 encode
$convert_path = $PWD.Path+[System.IO.Path]::DirectorySeparatorChar +$file_path+".png"
$encoding = New-Object System.Text.ASCIIEncoding
[System.IO.File]::WriteAllText($convert_path
, [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($read_file))
, $encoding
)
}elseif ($t -eq "d") { #base64 decode
$convert_path = $PWD.Path+[System.IO.Path]::DirectorySeparatorChar +[System.IO.Path]::GetFileNameWithoutExtension($file_path)
[System.IO.File]::WriteAllBytes($convert_path
, [System.Convert]::FromBase64String([System.IO.File]::ReadAllText($read_file))
)
}
# static byte[] FromBase64String(string s)
# static string ReadAllText(string path)
# static string ReadAllText(string path, System.Text.Encoding encoding)
# static void WriteAllBytes(string path, byte[] bytes)
#static void WriteAllText(string path, string contents)
#static void WriteAllText(string path, string contents, System.Text.Encoding encoding)