Reputation: 425
I have a powershell script which I call using a .vbs file. This reduces the effort of the user to go to cmd every time,type powershell and then calling the .ps1 file. The vbs script directly opens powershell in a new cmd window calling the ps1 file.
The VB script is:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit C:\Scripts\anydrive.ps1")
What I want to achieve now is, Can I embed the .ps1 file inside .vbs file so that I have a single .vbs file which I can circulate to users rather than having 2 separate files.
Upvotes: 0
Views: 1426
Reputation: 47772
You can do what you're asking. The easiest way to is to encode it as a base64 string, perhaps something like this (this is a one time thing):
$script = Get-Content C:\Scripts\anydrive.ps1 -Raw
$bytes = [System.Text.Encoding]::Unicode.GetBytes($script)
$encoded = [Convert]::ToBase64String($bytes)
$encoded | clip # copy it to the clipboard
Then you paste it into the VBScript encoded.
You would call powershell like this:
powershell.exe -NoExit -EncodedCommand $e
($e
in this case refers to the encoded command string or the VBscript variable that contains it).
If you don't want users to have to open powershell and manually load the file, and you want something they can double click on, why not just make a shortcut for them? The shortcut would just target the exact same command line you're calling from the VBscript.
Upvotes: 3