来自C#的PowerShell/预定脚本 [英] PowerShell/scheduled script from C#
问题描述
大家好
我在powershell脚本中还挺新的,所以这可能是个奇怪的问题,但是现在我们开始:我正在写一个ASP.Net网站,该网站在C#方法中包含PowerShell脚本代码.在虚拟机上执行计划的shutdownGuest操作.我需要从C#/Script代码而不是在VM Client中完成此操作.如下所述,我获得了一个功能脚本,该脚本执行计划的关机...
SCRIPT/.ps1文件
#Add-PSSnapin VMware.VimAutomation.Core
$ VIServer = Connect-VIserver-服务器服务器协议https-用户用户密码密码
$ VMs = Get-View -ViewType VirtualMachine -Filter @ {"Name" ="Gameserver *"}
$ timestart =(获取日期).addminutes(5)
foreach($ VM中的$ vm){
$ ma =新对象VMware.Vim.MethodAction
$ ma.Argument = $ null
$ ma.Name ="PowerOffVM_Task"
$ ots =新对象VMware.Vim.OnceTaskScheduler
$ ots.runat = $ timestart
$ spec =新对象VMware.Vim.ScheduledTaskSpec
$ spec.Name =关闭访客"#+ $ VM.name
$ spec.Description =关闭访客" + $ VM.name
$ spec.Enabled = $ true
$ spec.Notification ="p.mikkelsen81@gmail.com"
$ spec.Action = $ ma
$ spec.Scheduler = $ ots
$ si =获取视图ServiceInstance
$ stm =获取视图$ si.Content.ScheduledTaskManager
$ stm.CreateScheduledTask($ vm.MoRef,$ Spec)
}
但是我的问题是我想通过直接从C#代码运行它来避免.ps1文件(我的方法在下面发布),但是它不能正常工作.错误,看不到我在这里缺少什么..有人可以帮忙吗?
const string quote = " ;
psScript1 = " ;
psScript2 = " +服务器+ -协议" +协议+ +用户名+ " +密码;
psScript3 = " + vmName + " ;
psScript4 = " +引用+ " +引号+ " =" +引用+ vmName + *" +引用+ " ;
psScript5 = " + (" +天+ ;
psScript6 = " ;
// psScript23 ="Get-VM -Name" + vmName;
psScript7 = " ;
psScript8 = " ;
psScript9 = " +引用+ " +引号;
psScript10 = " ;
psScript11 = " ;
psScript12 = " ;
psScript13 = " +引用+ " +引号;
psScript14 = " +引用+ " +引用+ " + $ VM.name";
psScript15 = " ;
psScript16 = " +引用+ " +电子邮件+引用+ " ;
psScript17 = " ;
psScript18 = " ;
psScript19 = " ;
psScript20 = " ;
psScript21 = " ; 解决方案
VIServer = Connect-VIserver-服务器服务器协议https-用户用户密码密码
VM = Get-View -ViewType VirtualMachine -Filter @ {"Name" ="Gameserver *"}
timestart =(获取日期).addminutes(5)
foreach(
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){";
// 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)}";
解决方案 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(
这篇关于来自C#的PowerShell/预定脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!