Hi all
Im pretty new in powershell script, so this might be an odd question, but here we go: I'm writing a ASP.Net site which includes PowerShell script code in the C# methods.. the site is ment to perform PowerOn operations and scheduled shutdownGuest operations on Virtual machines.. I need this to be done from the C#/Script code and not in the VM Client.. As posted below i got a functional Script which performs a scheduled shutdown...
SCRIPT/.ps1 file
#Add-PSSnapin VMware.VimAutomation.Core
$VIServer = Connect-VIserver -Server server-Protocol https -User user-Password password
$VMs = Get-View -ViewType VirtualMachine -Filter @{"Name" = "Gameserver*"}
$timestart = (Get-Date).addminutes(5)
foreach($vm in $VMs){
$ma = New-Object VMware.Vim.MethodAction
$ma.Argument = $null
$ma.Name = "PowerOffVM_Task"
$ots = New-Object VMware.Vim.OnceTaskScheduler
$ots.runat = $timestart
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Shut Down Guest"# + $VM.name
$spec.Description = "Shut Down Guest " + $VM.name
$spec.Enabled = $true
$spec.Notification = "p.mikkelsen81@gmail.com"
$spec.Action = $ma
$spec.Scheduler = $ots
$si = Get-View ServiceInstance
$stm = Get-View $si.Content.ScheduledTaskManager
$stm.CreateScheduledTask($vm.MoRef,$Spec)
}
But my problem is i want to avoid the .ps1 file by running it directly from the C# code (my method posted below), but it aint working.. i got the "Missing closing '}' in statement block." error and can't see what im missing here.. can anybody help on this one?
const string quote = "\"";
psScript1 = "Add-PSSnapin VMware.VimAutomation.Core";
psScript2 = "$VIServer = Connect-VIserver -Server " + server + " -Protocol " + protocol + " -User " + userName + " -Password " + password;
psScript3 = "Get-VM -Name " + vmName + " | Start-VM";
psScript4 = "$VMs = Get-View -ViewType VirtualMachine -Filter @{" + quote + "Name" + quote + " = " + quote + vmName + "*" + quote + "}";
psScript5 = "$timestart = (Get-Date).addDays" + "(" + days + ")";
psScript6 = "foreach($vm in $VMs){";
psScript7 = "$ma = New-Object VMware.Vim.MethodAction";
psScript8 = "$ma.Argument = $null";
psScript9 = "$ma.Name = " + quote + "ShutdownGuest" + quote;
psScript10 = "$ots = New-Object VMware.Vim.OnceTaskScheduler";
psScript11 = "$ots.runat = $timestart";
psScript12 = "$spec = New-Object VMware.Vim.ScheduledTaskSpec";
psScript13 = "$spec.Name = " + quote + "ShutDownGuest" + quote;
psScript14 = "$spec.Description = " + quote + "ShutDownGuest" + quote + " + " + "$VM.name";
psScript15 = "$spec.Enabled = $true";
psScript16 = "$spec.Notification = " + quote + "" + eMail + quote + "";
psScript17 = "$spec.Action = $ma";
psScript18 = "$spec.Scheduler = $ots";
psScript19 = "$si = Get-View ServiceInstance";
psScript20 = "$stm = Get-View $si.Content.ScheduledTaskManager";
psScript21 = "$stm.CreateScheduledTask($vm.MoRef,$Spec)}";