Before shut this down please note its not a code issue, but an environment, privileges and policy issue on how do we enable this across the landscape.
We are creating VHDs on regular basis, servers and clients and for a large part this script is run using the Batch File below from an elevated Admin Command prompt.
The script is not an issue as it's running fine on several Win 8.1 Pro and Server machines.
For the Win 8.1 machine(s) & some servers where it's not working.
I'd like to figure out why and apply environmental settings, policies to all machines.
Preferably an automated way or if no choice do it manually.
The error message it fires up:
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
The code, for reference. Apparently this cmdlet was added from Win 8.1/ 08 R2/ 12 onwards from what I'm reading online.
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")
Running it via Batch file of same name:
VHD_CF.bat
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