是否有方法将exe嵌入到现有的powershell脚本中?我的老板想让我想出一种方法,在我们的员工电脑上安装软件,这些软件是在家工作,不懂技术的。本质上,我需要将文件在本地复制到他们的计算机(这是一个exe),并使用一些参数(如/norestart、/quiet等)从powershell (或命令行)终端中运行它。
发布于 2020-04-16 23:14:40
可以使用Base64编码将exe嵌入到PowerShell脚本中。运行此脚本对exe进行编码。它在下载文件夹中生成“base64Decoder.ps1”。
# Requires PowerShell 5.1
# Run this script to encode the exe.
# It produces 'base64Decoder.ps1' in the Downloads folder.
$folder = "$env:UserProfile\Downloads\Demo\"
$file = "PowerShell-7.0.0-win-x64.msi"
$option = [System.Base64FormattingOptions]::InsertLineBreaks
$path = Join-Path -Path $folder -ChildPath $file
$bytes = Get-Content $path -Encoding Byte -ReadCount 0
$outputProgram = [System.Text.StringBuilder]::new()
[void]$outputProgram.AppendLine( '$encodedText = @"' )
[void]$outputProgram.AppendLine( ([Convert]::ToBase64String($bytes, $option)) )
[void]$outputProgram.AppendLine( '"@' )
[void]$outputProgram.Append(
@"
`$downloads = Join-Path -Path `$Env:USERPROFILE -ChildPath "Downloads"
`$file = "$file"
`$path = Join-Path -Path `$downloads -ChildPath `$file
`$value = [System.Convert]::FromBase64String(`$encodedText)
Set-Content -Path `$path -Value `$value -Encoding Byte
"@
)
$downloads = Join-Path -Path $Env:USERPROFILE -ChildPath "Downloads"
$outFile = "base64Decoder.ps1"
$outPath = Join-Path -Path $downloads -ChildPath $outFile
Set-Content -Path $outPath -Value ($outputProgram.ToString())您可以将base64Decoder.ps1的内容复制并粘贴到现有的PowerShell脚本中,以嵌入exe。或者,如果太大,将base64Decoder.ps1包含在原始脚本中,并在必要时调用它。
在目标计算机上运行脚本,在下载文件夹中复制原始文件。这是有效的PowerShell语法,可以包含在脚本中。
& "$env:UserProfile\Downloads\base64Decoder.ps1"在脚本运行之前,您可能必须使用设置执行策略。
Set-ExecutionPolicy RemoteSigned使用启动过程调用exe。这可以保存在脚本中。
Start-Process -FilePath "$env:UserProfile\Downloads\PowerShell-7.0.0-win-x64.msi" -ArgumentList '/? '如果您想通过电子邮件发送PowerShell脚本,请将其附加为.txt并让它们重命名它。我相信您知道文件附件通常限制在10 to以内。
如果exe在线可用,则可以使用调用-WebRequest,这要容易得多。
Invoke-WebRequest "https://github.com/PowerShell/PowerShell/releases/download/v7.0.0/PowerShell-7.0.0-win-x64.msi" -outfile "$env:UserProfile\Downloads\PowerShell-7.0.0-win-x64.msi"您可以在窗砂箱中测试这些步骤。
虽然这是对你问题的正确回答,但我不推荐。
首先,它比简单地从因特网下载安装程序并使用(MSI)交换机要复杂得多。
其次,对于非平凡的exe,我的脚本的性能很差,而会造成比解决更多的问题。
我不知道这里的假设是什么。但是,如果这些计算机没有得到管理,我想每个安装都会有一个支持请求。你不能做的就是把这个脚本用电子邮件寄给100个人,或者把它放到登录脚本中,然后离开。那就太糟了。即使这是在办公室,我不会部署一个无人值守的安装,除非进行彻底的测试。这是假设本地存储和登录脚本或类似的:而不是在家工作的人一次过。
https://stackoverflow.com/questions/61258768
复制相似问题