Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Create .BAT files with Powershell Code

$
0
0

Hello,

I am trying to write a small script that will accept as File.PS1, Encode it and create a File.Bat to be run manually by users. In current case, The file will switch users to a different file server.

This is the script that I have created:

$PS1_Path=".\scripts\Move-PrintServer.ps1"
$BatFile=".\scripts\TestingBat.Bat"
$Code=Get-Content$PS1_Path
$PowerShellPath
='C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -encoded'
$Encoded=[convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
If (Test-Path$BatFile) { Del$BatFile }
$ScriptLine="$PowerShellPath$Encoded"
Set-Content-Path$BatFile-Value$ScriptLine-EncodingASCII

It works fantastic when the source file is small. However, when the source file is large and creates a very LONG ended line, the TestingFile.bat does not execute. It simply list its content.

However, If I am to manually copy and paste the contents of $Encoded variable and remove the line breaks manually, the file runs.

Please help me fix this code.

 

Thank You!

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles