我想让我的scvmm插件自动创建vm bakcups的快捷方式。我在主机上使用powershell-cmdlet:
string command2 = "$shell = New-Object -ComObject WScript.Shell;";
foreach (var vm in VMs)
{
command2 = command2 + string.Format("${1} = shell.CreateShortcut(\"{0}\\{1}\\{2}.lnk\"); ${1}.TargetPath = \"..\\_VMBackup\\{2}\\{1}\"; ${1}.Save();", backupDir, vm.Name, date);
}下面是我调用它们的方式:
PowerShellContext.ExecuteScript<Host>(string.Format("Invoke-SCScriptCommand -Executable \"{0}\" -VMHost (Get-SCVMHost -ID \"{1}\") -CommandParameters \"{2}\" -RunAsynchronously -TimeoutSeconds 360000", PowershellPath, VMs.First(), command2), (vms, error) => { if (error != null) { } else { } });但似乎有些地方不对劲,因为我无法正确执行,即使我在shell中随意尝试它也能正常工作。有什么想法吗?
发布于 2015-01-10 07:08:54
您在string.Format中的"shell“变量之前忘记了一个"$”
尝试:
command2 = command2 + string.Format("${1} = $shell.CreateShortcut(\"{0}\\{1}\\{2}.lnk\"); ${1}.TargetPath = \"..\\_VMBackup\\{2}\\{1}\"; ${1}.Save();", backupDir, vm.Name, date);而不是:
command2 = command2 + string.Format("${1} = shell.CreateShortcut(\"{0}\\{1}\\{2}.lnk\"); ${1}.TargetPath = \"..\\_VMBackup\\{2}\\{1}\"; ${1}.Save();", backupDir, vm.Name, date);https://stackoverflow.com/questions/27858537
复制相似问题