我希望我的scvmm加载项能够自动为我的vm bakcups创建快捷方式。 我在主机上使用powershell-cmdlets:
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中实际尝试它也能正常工作。有什么想法吗?
答案 0 :(得分:0)
你在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);