在关闭此功能之前,请注意这不是代码问题,而是环境、权限和策略问题,关于我们如何在整个环境中启用此功能。
我们定期创建 VHD、服务器和客户端,并且很大一部分此脚本是使用提升的管理员命令提示符下的下面的批处理文件运行的。
该脚本没有问题,因为它在多台 Win 8.1 Pro 和 Server 机器上运行良好。
对于无法运行的 Win 8.1 机器和一些服务器。
我想找出原因并将环境设置和策略应用于所有机器。
最好采用自动化方式,如果没有选择,则手动执行。
它触发的错误消息:
New-VHD : The term 'New-VHD' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At E:\VHD\VhdCreateFormat\VhdCreateFormat.ps1:4 char:1 + New-VHD -Path $vhdpath -Dynamic -SizeBytes $vhdsize | Mount-VHD -Passthru | Init ... + ~~~~~~~ + CategoryInfo : ObjectNotFound: (New-VHD:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
代码,仅供参考。从我在网上读到的内容来看,这个 cmdlet 显然是从 Win 8.1/ 08 R2/ 12 开始添加的。
VHD_CF.ps1
$vhdpath = "E:\VHD\NameItServerClient.vhd" $vhdsize = 25GB $vhdVolFsName = "NameItServerClient" New-VHD -Path $vhdpath -Dynamic -SizeBytes $vhdsize | Mount-VHD -Passthru | Initialize-Disk -PartitionStyle MBR -Passthru |New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -NewFileSystemLabel $vhdVolFsName -FileSystem NTFS -Confirm:$false -Force Write-Host "Press any key to continue ..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Write-Host "Press any key to continue ...2 " Function Pause($M="Press any key to continue . . . "){If($psISE){$S=New-Object -ComObject "WScript.Shell";$B=$S.Popup("Click OK to continue.",0,"Script Paused",0);Return};Write-Host -NoNewline $M;$I=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183;While($K.VirtualKeyCode -Eq $Null -Or $I -Contains $K.VirtualKeyCode){$K=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")};Write-Host} Write-Host "Press any key to continue ...3" $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
通过同名批处理文件运行它:
虚拟硬盘
REM So we don’t have to re-write the batch file for every script, or every time we move a script around, it’s going to make use of a self-referencing variable to build the file path for the PowerShell script. To make this work, the batch file will need to be placed in the same folder as your PowerShell script and have the same file name. So if your PowerShell script is called “MyScript.ps1”, you’ll want to name your batch file “MyScript.bat” and make sure it’s in the same folder. Then, put these lines in the batch script:
REM @ECHO OFF PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}" PAUSE
答案1
New-Vhd
是 Hyper-V PowerShell cmdlet 的一部分。假设您在服务器本身中远程调用此命令,我猜想您的某些服务器没有安装这些 cmdlet。