我一直在开发一个自动安装程序,因为我经常需要重新安装Windows,我安装的软件之一是Visual 2013,它是作为ISO提供的。
我已经编写了一些安装ISO的代码,但是当我运行安装程序时,我想不出如何返回驱动器号。
else if (soft == "Visual Studio 2013 Pro")
{
var isoPath = Loc.path + Loc.vs2013pro;
using (var ps = PowerShell.Create())
{
ps.AddCommand("Mount-DiskImage").AddParameter("ImagePath", isoPath).Invoke();
}
var proc = System.Diagnostics.Process.Start(Loc.path + Loc.vs2013pro);
proc.WaitForExit();
System.IO.File.Copy(Loc.path + @"\Visual Studio 2013 Pro\Serial.txt", folder + "/Serials/VS2013Pro Serial.txt");
}发布于 2015-07-16 19:28:45
添加PassThru参数后,将返回一个MSFT_DiskImage。DevicePath属性中有挂载点。
发布于 2015-07-30 20:25:44
标记的答案对我不起作用,因此根据我在PowerShell ISO安装和拆卸上找到的一篇博客文章,我决定使用C# (必需参考System.Management.Automation & System.Management.Automation.Runspaces)将其编码为一个很好的示例。
string isoPath = "C:\\Path\\To\\Image.iso";
using (var ps = PowerShell.Create())
{
//Mount ISO Image
var command = ps.AddCommand("Mount-DiskImage");
command.AddParameter("ImagePath", isoPath);
command.Invoke();
ps.Commands.Clear();
//Get Drive Letter ISO Image Was Mounted To
var runSpace = ps.Runspace;
var pipeLine = runSpace.CreatePipeline();
var getImageCommand = new Command("Get-DiskImage");
getImageCommand.Parameters.Add("ImagePath", isoPath);
pipeLine.Commands.Add(getImageCommand);
pipeLine.Commands.Add("Get-Volume");
string driveLetter = null;
foreach (PSObject psObject in pipeLine.Invoke())
{
driveLetter = psObject.Members["DriveLetter"].Value.ToString();
Console.WriteLine("Mounted On Drive: " + driveLetter);
}
pipeLine.Commands.Clear();
//Unmount Via Image File Path
command = ps.AddCommand("Dismount-DiskImage");
command.AddParameter("ImagePath", isoPath);
ps.Invoke();
ps.Commands.Clear();
//Alternate Unmount Via Drive Letter
ps.AddScript("$ShellApplication = New-Object -ComObject Shell.Application;" +
"$ShellApplication.Namespace(17).ParseName(\"" + driveLetter + ":\").InvokeVerb(\"Eject\")");
ps.Invoke();
ps.Commands.Clear();
}https://stackoverflow.com/questions/31462846
复制相似问题