0

我希望我的 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 中尝试它会起作用。有什么想法吗?

4

1 回答 1

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);
于 2015-01-09T23:08:54.167 回答