来自C#的PowerShell/预定脚本

编程入门 行业动态 更新时间:2024-10-14 22:16:35
本文介绍了来自C#的PowerShell/预定脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好 我在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" $ spec.Action = $ ma $ spec.Scheduler = $ ots $ si =获取视图ServiceInstance $ stm =获取视图$ si.Content.ScheduledTaskManager $ stm.CreateScheduledTask($ vm.MoRef,$ Spec) } 但是我的问题是我想通过直接从C#代码运行它来避免.ps1文件(我的方法在下面发布),但是它不能正常工作.错误,看不到我在这里缺少什么..有人可以帮忙吗?

const string quote = " \""; psScript1 = " Add-PSSnapin VMware.VimAutomation.Core" ; psScript2 = " $ VIServer = Connect-VIserver -Server" +服务器+ " -协议" +协议+ " -用户" +用户名+ " -密码" +密码; psScript3 = " Get-VM -Name" + vmName + " |启动VM" ; psScript4 = " $ VMs = Get-View -ViewType VirtualMachine -Filter @ {" +引用+ " 名称" +引号+ " =" +引用+ vmName + " *" +引用+ " }" ; psScript5 = " $ timestart =(Get-Date).addDays" + " (" +天+ " )" ; psScript6 = " foreach($ VM中的$ vm){" ; // psScript23 ="Get-VM -Name" + vmName; psScript7 = " $ ma =新对象VMware.Vim.MethodAction" ; psScript8 = " $ ma.Argument = $ null" ; psScript9 = " $ ma.Name =" +引用+ " ShutdownGuest" +引号; psScript10 = " $ ots =新对象VMware.Vim.OnceTaskScheduler" ; psScript11 = " $ ots.runat = $ timestart" ; psScript12 = " $ spec =新对象VMware.Vim.ScheduledTaskSpec" ; psScript13 = " $ spec.Name =" +引用+ " ShutDownGuest" +引号; psScript14 = " $ spec.Description =" +引用+ " ShutDownGuest" +引用+ " + " + " $ VM.name"; psScript15 = " $ spec.Enabled = $ true" ; psScript16 = " $ spec.Notification =" +引用+ " " +电子邮件+引用+ " "; 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-服务器服务器协议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" $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/预定脚本

本文发布于:2023-11-07 23:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!