Powershell-VBoxManage克隆脚本
我编写了一个powershell脚本,从文本文件读取数据,然后使用VBoxManage克隆虚拟机。它可以工作,但是,它会报告此错误Powershell-VBoxManage克隆脚本,powershell,virtualbox,Powershell,Virtualbox,我编写了一个powershell脚本,从文本文件读取数据,然后使用VBoxManage克隆虚拟机。它可以工作,但是,它会报告此错误 [string]$vmPath = "C:\Users\someuser\VirtualBox VMs\" [string]$vmName="QuestionMark" [string]$vBoxClonevdi = "clonehd" [string]$vBoxVMName = Get-Content -Path "C:\Users\someuser\Virtua
[string]$vmPath = "C:\Users\someuser\VirtualBox VMs\"
[string]$vmName="QuestionMark"
[string]$vBoxClonevdi = "clonehd"
[string]$vBoxVMName = Get-Content -Path "C:\Users\someuser\VirtualBox VMs\log.txt"
[string]$invokeCommand
[string]$filetype=".vdi"
[string]$vBoxManagewithQuotes= "`"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe`""
if(Test-Path ("C:\Users\someuser\VirtualBox VMs\log.txt")){
[int]$ConvertNum = -1
[int]::TryParse($vBoxVMName.Substring(16), [ref]$ConvertNum)
Write-Output ("Version Number: " + $ConvertNum)
$ConvertAfter = $ConvertNum + 1
Write-Output ("New Version: " + $ConvertAfter)
$vmOut = $vmPath+$vmName+$ConvertAfter+$filetype
$vmToClone=($vmPath+$vmName+$ConvertNum+$fileType)
$write= ($vmName + $ConvertAfter)
}
if(Test-Path ("C:\Users\someuser\cloneVM.bat")){
cmd.exe /c "C:\Users\someuser\cloneVM.bat" $vmName $ConvertAfter
}
错误:
cmd.exe : 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
At C:\Users\someuser\clonevdi.ps1:28 char:6
+ cmd.exe /c "C:\Users\someuser\cloneVM.bat" $vmName $ConvertAfter
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (0%...10%...20%....0%...90%...100%:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
我不确定这个错误是什么意思。这是什么意思?我该如何修复它呢?可以肯定的是,您看到的是cloneVM.bat正在向错误流发送数据,而PowerShell正在温和地报告错误。我可以用下面的bat文件来模拟
'GAME OVER MAN! GAME OVER' 1>&2
从PowerShell调用bat文件
cmd.exe /c C:\temp\test.bat
这会在PowerShell中触发以下错误
cmd.exe : 'GAME OVER MAN! GAME OVER'
At line:1 char:1
+ cmd.exe /c C:\temp\test.bat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ('GAME OVER MAN! GAME OVER' :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
处理此问题的一种方法是将错误发送到输出流,并将其捕获到变量中。如果需要cloneVM.bat的其他输出,这将是首选方法。你可以考虑其他的选择
如果你看一下cloneVM.bat,我会大胆地猜测,你会发现一些带有2>&1的行,我不确定,但看起来它好像在尝试使用脚本的进度输出并执行它?当您作废所有输出[void]cmd.exe/c:\Users\someuser\cloneVM.bat$vmName$ConvertAfter时会发生什么情况?可能这一过程会出现错误流,这就是为什么您会看到所看到的内容感谢您的帮助。我想知道的是,为什么会出错?从命令提示符运行VBoxManage效果很好。只有在powershell中才会发生这种情况。鉴于您能够使用自己的批处理文件再现错误,CMD和Powershell的情况如何?@charleswitfield在错误流中为最终用户生成消息是很常见的。PSEXEC使用它作为开始信息。根据sei,这并没有什么问题。由于PowerShell是调用应用程序,因此它希望显示这些错误。在批处理文件中查找错误重定向。我肯定它就在那里
$return = cmd.exe /c C:\temp\test.bat 2>&1