Powershell - 不同颜色的猫输出字符串
Powershell - Cat out string in different colors
如有任何建议或帮助,我们将不胜感激。
我正在为 windows 编写一个简单的 base64 编码器,因此我可以在需要时更快地编码字符串。
编码发生在 CMD 中,然后脚本打开 PowerShell 以便在终端中提取编码字符串。
为了便于阅读,我尝试更改用 cat 检索到的字符串的颜色,但我很难做到这一点。这是有问题的代码片段:
certutil -encode data.txt tmp.b64 && findstr /v /c:- tmp.b64 > secret.b64
powershell.exe -Command "Write-Host Your secret is: -ForegroundColor Green";cat secret.b64
PAUSE
检索信息时; “你的秘密是:”在代码中是绿色的,但我似乎找不到为“cat secret.b64”命令着色的方法。
完全在 PowerShell 中完成; PS 可以调用 CMD 可以调用的任何控制台可执行文件。要获取控制台可执行文件的标准输出并将其用作 PS cmdlet 或函数的字符串输入,请将整个控制台可执行文件的命令括在括号中(例如,$foocontent = (cat.exe foo)
),或将其通过管道传递给参数接受管道输入的 PS cmdlet/function(例如,cat foo | Format-Table
)
(请注意,在 Windows 下的 PowerShell 中,cat
通常是 PowerShell cmdlet Get-Content
的别名。)
就最初的要求而言,我想您想要的是:
powershell -command "$i = cat secret.b64; Write-Host Your secret is: $i -ForegroundColor Green"
但是您可以跳过 powershell
并只执行 batch-file
。
@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "cl=%%a"
(certutil -encode data.txt tmp.b64 && findstr /v /c:- tmp.b64)>secret.b64
set /p secr=<secret.b64
echo Your secret is: %cl%[92m%secr%%cl%[0m
pause
如有任何建议或帮助,我们将不胜感激。 我正在为 windows 编写一个简单的 base64 编码器,因此我可以在需要时更快地编码字符串。
编码发生在 CMD 中,然后脚本打开 PowerShell 以便在终端中提取编码字符串。
为了便于阅读,我尝试更改用 cat 检索到的字符串的颜色,但我很难做到这一点。这是有问题的代码片段:
certutil -encode data.txt tmp.b64 && findstr /v /c:- tmp.b64 > secret.b64
powershell.exe -Command "Write-Host Your secret is: -ForegroundColor Green";cat secret.b64
PAUSE
检索信息时; “你的秘密是:”在代码中是绿色的,但我似乎找不到为“cat secret.b64”命令着色的方法。
完全在 PowerShell 中完成; PS 可以调用 CMD 可以调用的任何控制台可执行文件。要获取控制台可执行文件的标准输出并将其用作 PS cmdlet 或函数的字符串输入,请将整个控制台可执行文件的命令括在括号中(例如,$foocontent = (cat.exe foo)
),或将其通过管道传递给参数接受管道输入的 PS cmdlet/function(例如,cat foo | Format-Table
)
(请注意,在 Windows 下的 PowerShell 中,cat
通常是 PowerShell cmdlet Get-Content
的别名。)
就最初的要求而言,我想您想要的是:
powershell -command "$i = cat secret.b64; Write-Host Your secret is: $i -ForegroundColor Green"
但是您可以跳过 powershell
并只执行 batch-file
。
@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "cl=%%a"
(certutil -encode data.txt tmp.b64 && findstr /v /c:- tmp.b64)>secret.b64
set /p secr=<secret.b64
echo Your secret is: %cl%[92m%secr%%cl%[0m
pause