Totally stuck here and have no idea what to do: I've got a multiple choice interactive Powershell script I'm putting together for the ops so they shave off a few minutes whenever they want to perform quick tasks on remote computers.
I've replaced all the code for all the other choices with just You have selected #
, so I don't bore anyone with the rest of the script content, but choice 6
is driving me mad.
This is what I'm aiming for it to do, but can't get past the first bullet point yet:
- Create new
.vnc
file - Add standard VNC protocol body of text into the file
- Add
$ComputerName
afterHost=
- Launch the file
Problem is the below won't create the file at all; if copy/pasted directly into Powershell, it works, but it won't run in a script! Anyone know why that could be?
$commands = {
function Show-Menu
{
param (
[string]$Title = 'My Menu'
)
Clear-Host
Write-Host "================ $Title ================"
Write-Host "1: Press '1' (Description)."
Write-Host "2: Press '2' (Description)."
Write-Host "3: Press '3' (Description)."
Write-Host "4: Press '4' (Description)."
Write-Host "5: Press '5' (Description)."
Write-Host "6: Press '6' To start a VNC Connection."
Write-Host "Q: Press 'Q' to quit."
}
Show-Menu –Title 'My Menu'
$selection = Read-Host "Please make a selection"
switch ($selection)
{
'1' {
"You have selected 1"
sleep -seconds 2
} '2' {
"You have selected 2"
sleep -seconds 2
} '3' {
"You have selected 3"
sleep -seconds 2
} '4' {
"You have selected 4"
sleep -seconds 2
} '5' {
"You have selected 5"
sleep -seconds 2
} '6' {
$ComputerName = (Read-Host "ComputerName")
{
New-Item -Path "C:\Windows\Temp\$ComputerName.VNC"
Set-Content "C:\Windows\Temp\$ComputerName.VNC" '
[connection]
host=$ComputerName
port=5900
[options]
use_encoding_1=1
copyrect=1
viewonly=0
fullscreen=0
8bit=0
shared=1
belldeiconify=0
disableclipboard=0
swapmouse=0
fitwindow=0
cursorshape=1
noremotecursor=0
preferred_encoding=7
compresslevel=-1
quality=6
localcursor=1
scale_den=1
scale_num=1
local_cursor_shape=1'
}
} 'q' {
#Closes the script
return
}
}
.$commands
}
&$commands
won't run in a script
.