Error Creating New VMs using PowerShell - powershell

I'm struggling to create a new virtual machine on Hyper-V using a simple example from the help file. I can create VMs manually using the GUI but I was trying to learn how to do this using PowerShell but I'm getting the error below.
New-VM -Name "base" -MemoryStartupBytes 1GB -NewVHDPath "J:\Hyper-V machines\dc.vhdx" -NewVHDSizeBytes 100GB
Error Message:
New-VM : Failed to create a new virtual machine. 'base' failed to realize.
(Virtual machine ID 80967A7D-7380-4771-B200-99129D63496C) Failed to access
configuration store: The device is not ready. (0x80070015).
At line:1 char:1
+ New-VM -Name "base" -MemoryStartupBytes 1GB -NewVHDPath J:\VM\base.vh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], VirtualizationExcept ion
+ FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.NewVM
My VMs are located in an external harddrive and the path exists ...there's not much help about this error online.

Related

Copy-Item throws Null reference exception

I am trying to copy a file from Guest Linux VM to Host Hyper V windows machine.
I am using powershell to copy the file from guest vm(linux) to Host(windows).
Write-Host "$session, $TestLogPath, $destLogs" #all variables are valid and not null
Copy-Item -FromSession $session -Path $TestLogPath/Test.txt -Destination $destLogs
I get the following error while doing that:
Object reference not set to an instance of an object.
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : 10.218.10.13
I am not sure what this error is since the desired file is copied successfully from Linux VM to Windows Host.
Any one has any idea why this error is shown despite file copied successfully from Linux VM to Host machine.

PowerShell optimize-Volume throwing error

I am creating an automation to optimize the C drive in my environment but when I run the below PowerShell command its throwing an error.(I am running this command on virtual Hard Disk VHDX)
Optimize-Volume -DriveLetter C -Defrag -Verbose
I have checked Both defragsvc (Defragmentation service) and Microsoft Storage SMP services are running. Can any one please help me on this?
Error
Optimize-Volume : Failed
Activity ID: {288b5478-2fd1-4159-8a4f-237d1484c50c}
At line:1 char:1
Optimize-Volume -DriveLetter C -Defrag -Verbose
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified:
(StorageWMI:ROOT/Microsoft/...age/MSFT_Volume) [Optimize-Volume],
CimException
FullyQualifiedErrorId : StorageWMI 4,Optimize-Volume

Hyper-v Export-VM : Provider load failure error in 2012r2

I'm using powershell 4 to export all the VMs in hyper-V and move it to a backup server(nas unix). Export was successful for few VMs and after a while it fails with the following errors:
Export-VM : Provider load failure
At C:\scripts\test.ps1:37 char:5
Export-VM -Name $vmname -Path $ExportPath
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Export-VM], ManagementException
FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.ExportVMCommand
This is the error i get in event viewer:
The description for Event ID 18350 from source Microsoft-Windows-Hyper-V-VMMS cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
71706037-090D-4B74-BB0B-AEE96D47D898
%%2147942432
0x80070020
The locale specific resource for the desired message is not present
The same script works fine in windows 2016 server running powershell 5. I'm not sure if this is OS specific or it has to do with any updates. Please share your view on this and help me resolve this issue.
Update
I also get this Error:
Get-VM : Quota violation
At line:1 char:1
+ Get-VM
+ ~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-VM], ManagementException
+ FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.GetVMCommand
This issue occurred because Hyper-V service became unresponsive. Restarting Windows Management Instrumentation service or hyper-v service fixed this issue.

PowerShell remote restart service running from a TFS build error

I have a TFS build definition contains a PowerShell Script build step. I would like to run the following command on a remote computer(DEVWS45PC) which should restart the service named StartSeleniumGridHub:
winrm s winrm/config/client '#{TrustedHosts="DEVWS45PC"}'
Restart-Service -InputObject $(Get-Service -Computer DEVWS45PC -Name StartSeleniumGridHub)
This gives me this error:
##[error]Get-Service : Cannot find any service with service name 'StartSeleniumGridHub'.
At E:\builds_2017\killBrowsersOnAllNodesAndRestartHub.ps1:43
char:36
+ Restart-Service -InputObject $(Get-Service -Computer DEVWS45PC -Name StartSe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (StartSeleniumGridHub:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Restart-Service : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At E:\builds_2017\killBrowsersOnAllNodesAndRestartHub.ps1:43
char:34
+ Restart-Service -InputObject $(Get-Service -Computer DEVWS45PC -Name StartSe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Restart-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.RestartServiceCommand
I can run this script without any problem from any of my computers. Note that 'DEVWS45PC' is truly set as a trusted host on the TFS build agent computer, so it's not the problem.
I have no clue what is the problem. Maybe authentication problems?
You could manually RDP to the remote machine DEVWS45PC with your build service account and run the powershell script.
Most likely lacking of permissions of your build service account(Due to could not find the service). Suggest you add the service account to your local Administrator group on the remote machine DEVWS45PC and try again.
Also make sure you are using the PowerShell on Target Machines task instead of powershell task in the build definition.

Powershell Hyper-V Scripts won't work? + Auto Installing Windows

So I have a strange problem. When I do commands such as this:
New-VM -Name "MattTest" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName Lab -path c:\VMdocs -NewVHDSizeBytes 40GB –NewVHDPath c:\base.vhdx
Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
Start-VM -Name MattTest
It works fine, However if I make a script I get a TON of errors, for example the exact same thing in a script called PS.ps1 and ran through Powershell I get:
New-VM : The parameter is not valid. No switch can be found by given
criteria. At C:\Users\mrsmith\Desktop\PS.ps1:1 char:1
+ New-VM -Name "MattTest" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName Lab - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Lab:String) [New-VM], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVMCommand
Set-VMDvdDrive : The parameter is not valid. Hyper-V was unable to
find a virtual machine with name MattTest. At
C:\Users\mrsmith\Desktop\PS.ps1:3 char:1
+ Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MattTest:String) [Set-VMDvdDrive], VirtualizationInvalidArgumentExcept ion
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMDvdDriveCommand
Set-VMDvdDrive : The parameter is not valid. No DVD drives are found.
At C:\Users\mrsmith\Desktop\PS.ps1:3 char:1
+ Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VMDvdDrive], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMDvdDriveCommand
Start-VM : The parameter is not valid. Hyper-V was unable to find a
virtual machine with name MattTest. At
C:\Users\mrsmith\Desktop\PS.ps1:5 char:1
+ Start-VM -Name MattTest
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MattTest:String) [Start-VM], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.StartVMCommand
Im not sure what's causing this? is it something to do with the module, because I have checked and the Hyper-V cmdlet IS running (considering I can manually type these in and they will be fine)
Also sidequestion: These set of instructions will get up to the "Install Windows XXXX" Screen. Is there any way to Automate THAT process? Or not really (Like make it click next, select a hard drive, install, etc...
Just to make sure, the prompt (e.g 'PS C:\Users\mrsmith\Desktop>') is not a part of the script, right? If so remove it.
As it appears, it is a part of the script because PS is an alias for Get-Process and it tries to pass 'C:\Users\mrsmith\Desktop' as a process name.