Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?

C#
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){";
        //  psScript23 = "Get-VM -Name " + vmName;
          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)}";
Posted
Comments
trobolan 23-Aug-12 5:19am    
forgot to post how i invoke the code.. like this:
var shell = PowerShell.Create();
shell.Commands.AddScript(psScript1);
shell.Commands.AddScript(psScript2);
shell.Commands.AddScript(psScript3);
(and more lines)
var results = shell.Invoke();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900