0

I want my my scvmm add-in to create shortcuts to my vm bakcups automatically. I use powershell-cmdlets on the host:

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);
}

this is how I invoke them:

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 { } });

Something seems to be wrong though, because I won't execute properly, even though it works if I try it maually in the shell. Got any ideas?

000000000000000000000
  • 1,467
  • 1
  • 19
  • 38

1 Answers1

0

You forgot a "$" before the "shell" variable in string.Format

Try:

command2 = command2 + string.Format("${1} = $shell.CreateShortcut(\"{0}\\{1}\\{2}.lnk\"); ${1}.TargetPath = \"..\\_VMBackup\\{2}\\{1}\"; ${1}.Save();", backupDir, vm.Name, date);

Instead of:

command2 = command2 + string.Format("${1} = shell.CreateShortcut(\"{0}\\{1}\\{2}.lnk\"); ${1}.TargetPath = \"..\\_VMBackup\\{2}\\{1}\"; ${1}.Save();", backupDir, vm.Name, date);