Question VBS Script Help - How to input text from text file into powershell window

domocorn

Distinguished
Sep 25, 2014
20
0
18,520
Hi. I am attempting to make a vbs script that I can run off a USB stick on any windows based machine that will open an ADMIN powershell windows and then run this script...

function robocop{clear-host;$confirmation = Read-Host "Data Migration(m) n Data Backup (b) n Exit (e)";if($confirmation -eq 'm'){$mSourceName = Read-Host 'Enter Source Directory.';if(($mSourceName -eq '') -or ($mSourceName -like 'c:')){Write-Warning -Message 'Source directory can not be empty or be C Drive.'}else{$mDestinationName = Read-Host 'Enter Destination Directory.';if($mDestinationName -eq ''){Write-Warning -Message 'Source directory can not be empty.'}else{if(!(test-path $mDestinationName)){New-Item -ItemType Directory -Force -Path $mDestinationName};Robocopy /E /XJ /W:0 /R:0 /ZB /EFSRAW $mSourceName $mDestinationName}}}elseif($confirmation -eq 'b'){$bSourceName = Read-Host 'Enter Source Directory.';if($bSourceName -eq ''){Write-Warning -Message 'Source directory can not be empty.'}else{$bDestinationName = Read-Host 'Enter Destination Directory.';if(($bDestinationName -eq '') -or ($bDestinationName -like 'c:')){Write-Warning -Message 'Destination directory can not be empty or be C Drive.'}else{if(!(test-path $bDestinationName)){New-Item -ItemType Directory -Force -Path $bDestinationName};Robocopy /E /XJ /W:0 /R:0 /ZB /EFSRAW $bSourceName $bDestinationName}}}elseif($confirmation -eq 'e'){break}else{robocop}};robocop

I have a script that gets me in the admin pwoershell window but i can't use the wshshell.SendKeys command because the script has brackets and quotes that break the code

Does anyone know a way around this?

Thanks in advanced
 

domocorn

Distinguished
Sep 25, 2014
20
0
18,520
so this is my script right now

Set wshShell =wscript.CreateObject("WScript.Shell")

wscript.sleep 100
CreateObject("WScript.Shell").Run "cmd.exe"
wscript.sleep 100
wshshell.SendKeys "powershell.exe -ExecutionPolicy unrestricted"
wscript.sleep 100
wshshell.SendKeys "{ENTER}"
wscript.sleep 100
wshshell.SendKeys "{function robocop{clear-host;$confirmation = Read-Host "Data Migration(m) n Data Backup (b) n Exit (e)";if($confirmation -eq 'm'){$mSourceName = Read-Host 'Enter Source Directory.';if(($mSourceName -eq '') -or ($mSourceName -like 'c:')){Write-Warning -Message 'Source directory can not be empty or be C Drive.'}else{$mDestinationName = Read-Host 'Enter Destination Directory.';if($mDestinationName -eq ''){Write-Warning -Message 'Source directory can not be empty.'}else{if(!(test-path $mDestinationName)){New-Item -ItemType Directory -Force -Path $mDestinationName};Robocopy /E /XJ /W:0 /R:0 /ZB /EFSRAW $mSourceName $mDestinationName}}}elseif($confirmation -eq 'b'){$bSourceName = Read-Host 'Enter Source Directory.';if($bSourceName -eq ''){Write-Warning -Message 'Source directory can not be empty.'}else{$bDestinationName = Read-Host 'Enter Destination Directory.';if(($bDestinationName -eq '') -or ($bDestinationName -like 'c:')){Write-Warning -Message 'Destination directory can not be empty or be C Drive.'}else{if(!(test-path $bDestinationName)){New-Item -ItemType Directory -Force -Path $bDestinationName};Robocopy /E /XJ /W:0 /R:0 /ZB /EFSRAW $bSourceName $bDestinationName}}}elseif($confirmation -eq 'e'){break}else{robocop}};robocop}"
wscript.sleep 100
wshshell.SendKeys "{ENTER}"

this doesnt work though running as a .vbs because there are brackets in the command that make it not type the whole thing out
 

gardenman

Splendid
Moderator
Try changing the 2 inner double quotes to single quotes.

From: "Data Migration(m) n Data Backup (b) n Exit (e)"
To: 'Data Migration(m) n Data Backup (b) n Exit (e)'

I tried running it after that and got 8 or so command prompts opening all with errors. Since I don't know exactly what the script does or how to use it (appears to copy folders or something), I stopped testing it at that point.