Hi, All<\/p>\n
I have a script that should allow me to shut down VM’s, but it’s giving me grief.<\/p>\n
The script looks like this:<\/p>\n
$waitstart = 200 \n$waitshutdown = 120 \n \nif ($args[1] -match \"on\") { \n$inputfile=get-content $args[0] \nforeach ($guest in $inputfile) { \nwrite-host \"Starting '$guest'\"\n#THE LINE BELOW HAS PROBLEMS!\n$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\"\n$result = $vm.requeststatechange(2) \nif ($result.returnvalue -match \"on\") { \nstart-sleep -s $waitstart \nwrite-host \"\" \nwrite-host \"$guest is started\" -foregroundcolor green \nwrite-host \"\" \n} \nelse { \nwrite-host \"\" \nwrite-host \"unable to start $guest\" -foregroundcolor red \nwrite-host \"\" \n}}} \n \nif ($args[1] -match \"off\") { \n$inputfile=get-content $args[0] \nforeach ($guest in $inputfile) { \nwrite-host \"shutting down $guest\" \n$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\" \n$vmname = $vm.name \n$vmshut = gwmi -namespace root\\virtualization -query \"SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'\" \n$result = $vmshut.InitiateShutdown(\"$true\",\"no comment\") \nif ($result.returnvalue -match \"off\") { \nstart-sleep -s $waitshutdown \nwrite-host \"\" \nwrite-host \"no error while shutting down $guest\" \nwrite-host \"shutdown of $guest completed\" -foregroundcolor green \nwrite-host \"\"} \n \nelse { \nwrite-host \"\" \nwrite-host \"unable to shutdown $guest\" -foregroundcolor red \nwrite-host \"\" \n}}} \n \nelse { \nwrite-host \"USAGE: to shutdown VMs,\" -nonewline; write-host \".\\managehyperV.ps1 c:\\scripts\\hosts.txt off\" -foregroundcolor yellow \nwrite-host \"USAGE: to start VMs,\" -nonewline; write-host \".\\managehyperV.ps1 c:\\scripts\\hosts.txt on\" -foregroundcolor yellow \n} \n\n<\/code><\/pre>\nAnd it should work, apart from the line I indicated as not working:<\/p>\n
$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\"\n<\/code><\/pre>\nSo I looked further into it, and could get awn output of the query, as long as I don’t try to define the elementname bit (I’ve listed the host here, plus one of the Guests I’m trying to manipulate):<\/p>\n
PS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem\"\n\n__GENUS : 2\n__CLASS : Msvm_ComputerSystem\n__SUPERCLASS : CIM_ComputerSystem\n__DYNASTY : CIM_ManagedElement\n__RELPATH : Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"SUPPORT\"\n__PROPERTY_COUNT : 29\n__DERIVATION : {CIM_ComputerSystem, CIM_System, CIM_EnabledLogicalElement, CIM_LogicalElement...}\n__SERVER : SUPPORT\n__NAMESPACE : root\\virtualization\n__PATH : \\\\SUPPORT\\root\\virtualization:Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"SUPPORT\"\nAssignedNumaNodeList : \nCaption : Hosting Computer System\nCreationClassName : Msvm_ComputerSystem\nDedicated : \nDescription : Microsoft Hosting Computer System\nElementName : SUPPORT\nEnabledDefault : 2\nEnabledState : 2\nHealthState : 5\nIdentifyingDescriptions : \nInstallDate : \nName : SUPPORT\nNameFormat : \nOnTimeInMilliseconds : \nOperationalStatus : {2}\nOtherDedicatedDescriptions : \nOtherEnabledState : \nOtherIdentifyingInfo : \nPowerManagementCapabilities : \nPrimaryOwnerContact : \nPrimaryOwnerName : \nProcessID : \nRequestedState : 12\nResetCapability : 1\nRoles : \nStatus : \nStatusDescriptions : {OK}\nTimeOfLastConfigurationChange : \nTimeOfLastStateChange : \nPSComputerName : SUPPORT\n\n__GENUS : 2\n__CLASS : Msvm_ComputerSystem\n__SUPERCLASS : CIM_ComputerSystem\n__DYNASTY : CIM_ManagedElement\n__RELPATH : Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"DDE47CAF-EC2E-4E1D-8A0E-FBACA72D6E7F\"\n__PROPERTY_COUNT : 29\n__DERIVATION : {CIM_ComputerSystem, CIM_System, CIM_EnabledLogicalElement, CIM_LogicalElement...}\n__SERVER : SUPPORT\n__NAMESPACE : root\\virtualization\n__PATH : \\\\SUPPORT\\root\\virtualization:Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"DDE47CAF-EC2E-4E\n 1D-8A0E-FBACA72D6E7F\"\nAssignedNumaNodeList : {}\nCaption : Virtual Machine\nCreationClassName : Msvm_ComputerSystem\nDedicated : \nDescription : Microsoft Virtual Machine\nElementName : VMSUPP-WIN7-MI7\nEnabledDefault : 2\nEnabledState : 3\nHealthState : 5\nIdentifyingDescriptions : \nInstallDate : 20140610111150.000000-000\nName : DDE47CAF-EC2E-4E1D-8A0E-FBACA72D6E7F\nNameFormat : \nOnTimeInMilliseconds : 0\nOperationalStatus : {2}\nOtherDedicatedDescriptions : \nOtherEnabledState : \nOtherIdentifyingInfo : \nPowerManagementCapabilities : \nPrimaryOwnerContact : \nPrimaryOwnerName : \nProcessID : 0\nRequestedState : 12\nResetCapability : 1\nRoles : \nStatus : \nStatusDescriptions : {Operating normally}\nTimeOfLastConfigurationChange : 20141217085039.102211-000\nTimeOfLastStateChange : 20141217102712.000000-000\nPSComputerName : SUPPORT\n\n<\/code><\/pre>\nSo that bit looks correct, right?\n<\/code><\/pre>\nMoving forward (and I think the Spiceworks WYSIWYG doesn't like so many text boxes!) I tried to refine it a little more:\n<\/code><\/pre>\n\n```\nPS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname=VMSUPP-WIN7-MI7\"\ngwmi : Invalid query \"select * from msvm_computersystem where elementname=VMSUPP-WIN7-MI7\"\nAt line:1 char:1\n+ gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem wh ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException\n + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand\n\nPS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname=\"VMSUPP-WIN7-MI7\"\"\nGet-WmiObject : A positional parameter cannot be found that accepts argument 'VMSUPP-WIN7-MI7'.\nAt line:1 char:1\n+ gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem wh ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException\n + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand\n\n```\n\nAnd it's at this point that my meager skills run out.\n\nI tried Get-vm, but am told it doesn't exist as a command, so can't use that, despite using Powershell 3.0.\n\nAnyone got any ideas?\n\n<\/code><\/pre>","upvoteCount":4,"answerCount":23,"datePublished":"2014-12-17T09:46:14.000Z","author":{"@type":"Person","name":"lisalyons","url":"https://community.spiceworks.com/u/lisalyons"},"acceptedAnswer":{"@type":"Answer","text":"It didn’t work the first time with the name in the variable because you used single quotes on this line:<\/p>\n
$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\"\n<\/code><\/pre>\nDo not wrap the $guest variable in single quotes on powershell will think you want that exact text as the string ‘$guest’ not ‘VMSUPP-WIN7-MI7’<\/p>\n
The line should read:<\/p>\n
$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname=$guest\"\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2014-12-18T00:17:36.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/17","author":{"@type":"Person","name":"chamele0n","url":"https://community.spiceworks.com/u/chamele0n"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi, All<\/p>\n
I have a script that should allow me to shut down VM’s, but it’s giving me grief.<\/p>\n
The script looks like this:<\/p>\n
$waitstart = 200 \n$waitshutdown = 120 \n \nif ($args[1] -match \"on\") { \n$inputfile=get-content $args[0] \nforeach ($guest in $inputfile) { \nwrite-host \"Starting '$guest'\"\n#THE LINE BELOW HAS PROBLEMS!\n$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\"\n$result = $vm.requeststatechange(2) \nif ($result.returnvalue -match \"on\") { \nstart-sleep -s $waitstart \nwrite-host \"\" \nwrite-host \"$guest is started\" -foregroundcolor green \nwrite-host \"\" \n} \nelse { \nwrite-host \"\" \nwrite-host \"unable to start $guest\" -foregroundcolor red \nwrite-host \"\" \n}}} \n \nif ($args[1] -match \"off\") { \n$inputfile=get-content $args[0] \nforeach ($guest in $inputfile) { \nwrite-host \"shutting down $guest\" \n$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\" \n$vmname = $vm.name \n$vmshut = gwmi -namespace root\\virtualization -query \"SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'\" \n$result = $vmshut.InitiateShutdown(\"$true\",\"no comment\") \nif ($result.returnvalue -match \"off\") { \nstart-sleep -s $waitshutdown \nwrite-host \"\" \nwrite-host \"no error while shutting down $guest\" \nwrite-host \"shutdown of $guest completed\" -foregroundcolor green \nwrite-host \"\"} \n \nelse { \nwrite-host \"\" \nwrite-host \"unable to shutdown $guest\" -foregroundcolor red \nwrite-host \"\" \n}}} \n \nelse { \nwrite-host \"USAGE: to shutdown VMs,\" -nonewline; write-host \".\\managehyperV.ps1 c:\\scripts\\hosts.txt off\" -foregroundcolor yellow \nwrite-host \"USAGE: to start VMs,\" -nonewline; write-host \".\\managehyperV.ps1 c:\\scripts\\hosts.txt on\" -foregroundcolor yellow \n} \n\n<\/code><\/pre>\nAnd it should work, apart from the line I indicated as not working:<\/p>\n
$vm = gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='$guest'\"\n<\/code><\/pre>\nSo I looked further into it, and could get awn output of the query, as long as I don’t try to define the elementname bit (I’ve listed the host here, plus one of the Guests I’m trying to manipulate):<\/p>\n
PS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem\"\n\n__GENUS : 2\n__CLASS : Msvm_ComputerSystem\n__SUPERCLASS : CIM_ComputerSystem\n__DYNASTY : CIM_ManagedElement\n__RELPATH : Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"SUPPORT\"\n__PROPERTY_COUNT : 29\n__DERIVATION : {CIM_ComputerSystem, CIM_System, CIM_EnabledLogicalElement, CIM_LogicalElement...}\n__SERVER : SUPPORT\n__NAMESPACE : root\\virtualization\n__PATH : \\\\SUPPORT\\root\\virtualization:Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"SUPPORT\"\nAssignedNumaNodeList : \nCaption : Hosting Computer System\nCreationClassName : Msvm_ComputerSystem\nDedicated : \nDescription : Microsoft Hosting Computer System\nElementName : SUPPORT\nEnabledDefault : 2\nEnabledState : 2\nHealthState : 5\nIdentifyingDescriptions : \nInstallDate : \nName : SUPPORT\nNameFormat : \nOnTimeInMilliseconds : \nOperationalStatus : {2}\nOtherDedicatedDescriptions : \nOtherEnabledState : \nOtherIdentifyingInfo : \nPowerManagementCapabilities : \nPrimaryOwnerContact : \nPrimaryOwnerName : \nProcessID : \nRequestedState : 12\nResetCapability : 1\nRoles : \nStatus : \nStatusDescriptions : {OK}\nTimeOfLastConfigurationChange : \nTimeOfLastStateChange : \nPSComputerName : SUPPORT\n\n__GENUS : 2\n__CLASS : Msvm_ComputerSystem\n__SUPERCLASS : CIM_ComputerSystem\n__DYNASTY : CIM_ManagedElement\n__RELPATH : Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"DDE47CAF-EC2E-4E1D-8A0E-FBACA72D6E7F\"\n__PROPERTY_COUNT : 29\n__DERIVATION : {CIM_ComputerSystem, CIM_System, CIM_EnabledLogicalElement, CIM_LogicalElement...}\n__SERVER : SUPPORT\n__NAMESPACE : root\\virtualization\n__PATH : \\\\SUPPORT\\root\\virtualization:Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"DDE47CAF-EC2E-4E\n 1D-8A0E-FBACA72D6E7F\"\nAssignedNumaNodeList : {}\nCaption : Virtual Machine\nCreationClassName : Msvm_ComputerSystem\nDedicated : \nDescription : Microsoft Virtual Machine\nElementName : VMSUPP-WIN7-MI7\nEnabledDefault : 2\nEnabledState : 3\nHealthState : 5\nIdentifyingDescriptions : \nInstallDate : 20140610111150.000000-000\nName : DDE47CAF-EC2E-4E1D-8A0E-FBACA72D6E7F\nNameFormat : \nOnTimeInMilliseconds : 0\nOperationalStatus : {2}\nOtherDedicatedDescriptions : \nOtherEnabledState : \nOtherIdentifyingInfo : \nPowerManagementCapabilities : \nPrimaryOwnerContact : \nPrimaryOwnerName : \nProcessID : 0\nRequestedState : 12\nResetCapability : 1\nRoles : \nStatus : \nStatusDescriptions : {Operating normally}\nTimeOfLastConfigurationChange : 20141217085039.102211-000\nTimeOfLastStateChange : 20141217102712.000000-000\nPSComputerName : SUPPORT\n\n<\/code><\/pre>\nSo that bit looks correct, right?\n<\/code><\/pre>\nMoving forward (and I think the Spiceworks WYSIWYG doesn't like so many text boxes!) I tried to refine it a little more:\n<\/code><\/pre>\n\n```\nPS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname=VMSUPP-WIN7-MI7\"\ngwmi : Invalid query \"select * from msvm_computersystem where elementname=VMSUPP-WIN7-MI7\"\nAt line:1 char:1\n+ gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem wh ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException\n + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand\n\nPS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname=\"VMSUPP-WIN7-MI7\"\"\nGet-WmiObject : A positional parameter cannot be found that accepts argument 'VMSUPP-WIN7-MI7'.\nAt line:1 char:1\n+ gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem wh ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException\n + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand\n\n```\n\nAnd it's at this point that my meager skills run out.\n\nI tried Get-vm, but am told it doesn't exist as a command, so can't use that, despite using Powershell 3.0.\n\nAnyone got any ideas?\n\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2014-12-17T09:46:14.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/1","author":{"@type":"Person","name":"lisalyons","url":"https://community.spiceworks.com/u/lisalyons"}},{"@type":"Answer","text":"Hmmm. OK, it worked when I used the name, and not the variable, but also when I used the CORRECT quotes!<\/p>\n
PS C:\\Scripts> gwmi -namespace root\\virtualization -query \"select * from msvm_computersystem where elementname='VMSUPP-WIN7-MI7'\"\n\n__GENUS : 2\n__CLASS : Msvm_ComputerSystem\n__SUPERCLASS : CIM_ComputerSystem\n__DYNASTY : CIM_ManagedElement\n__RELPATH : Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"DDE47CAF-EC2E-4E1D-8A0E-FBACA72D6E7F\"\n__PROPERTY_COUNT : 29\n__DERIVATION : {CIM_ComputerSystem, CIM_System, CIM_EnabledLogicalElement, CIM_LogicalElement...}\n__SERVER : SUPPORT\n__NAMESPACE : root\\virtualization\n__PATH : \\\\SUPPORT\\root\\virtualization:Msvm_ComputerSystem.CreationClassName=\"Msvm_ComputerSystem\",Name=\"DDE47CAF-EC2E-4E1D-8A0E-FBACA72D6E\n 7F\"\nAssignedNumaNodeList : {}\nCaption : Virtual Machine\nCreationClassName : Msvm_ComputerSystem\nDedicated : \nDescription : Microsoft Virtual Machine\nElementName : VMSUPP-WIN7-MI7\n[SNIP]\n<\/code><\/pre>\nBut still not working if I plug it into the script and use the $guest variable, which is pulled from a text file, which simply reads: vmsupp-win7-mi7<\/strong><\/p>","upvoteCount":0,"datePublished":"2014-12-17T10:01:07.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/2","author":{"@type":"Person","name":"lisalyons","url":"https://community.spiceworks.com/u/lisalyons"}},{"@type":"Answer","text":"What error do you get when using the variable? What server version is this?<\/p>\n
I’m doing a few checks here but the class name changed in win 8.1 so I can’t debug your exact code<\/p>","upvoteCount":0,"datePublished":"2014-12-17T10:22:08.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/3","author":{"@type":"Person","name":"mattfippard","url":"https://community.spiceworks.com/u/mattfippard"}},{"@type":"Answer","text":"
Sorry…<\/p>\n
Win server 2008 R2, with the updated Powershell 3.0<\/p>\n
However… I THINK it’s now working! I changed the \" to ’ and it seems t be turning things off and on again - Except it reports that it can’t.<\/p>\n
I guess that’s the two variables at the top being too short? What is that measured in? MS?<\/p>","upvoteCount":0,"datePublished":"2014-12-17T10:51:44.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/4","author":{"@type":"Person","name":"lisalyons","url":"https://community.spiceworks.com/u/lisalyons"}},{"@type":"Answer","text":"
All that write host hurts my eyes, I think the whole script can be shortened and improved.<\/p>","upvoteCount":2,"datePublished":"2014-12-17T11:08:29.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/5","author":{"@type":"Person","name":"Little-Green-Man","url":"https://community.spiceworks.com/u/Little-Green-Man"}},{"@type":"Answer","text":"
HEH! That wouldn’t surprise me, Bill…<\/p>\n
Sorry for the pain in the eyes!<\/p>","upvoteCount":0,"datePublished":"2014-12-17T11:09:34.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/6","author":{"@type":"Person","name":"lisalyons","url":"https://community.spiceworks.com/u/lisalyons"}},{"@type":"Answer","text":"
I think my next step is to have it generate the text file of those machines which are actually running…<\/p>\n
So I don’t have to prepare a list of servers I want to work with… and to automate it a little further.<\/p>\n
But everyone has to start somewhere, right?<\/p>","upvoteCount":0,"datePublished":"2014-12-17T11:10:39.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/7","author":{"@type":"Person","name":"lisalyons","url":"https://community.spiceworks.com/u/lisalyons"}},{"@type":"Answer","text":"
I have some ideas, will post later, I’m on my phone at the moment.<\/p>","upvoteCount":1,"datePublished":"2014-12-17T11:12:34.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/8","author":{"@type":"Person","name":"Little-Green-Man","url":"https://community.spiceworks.com/u/Little-Green-Man"}},{"@type":"Answer","text":"
What I was getting when I ran was a carriage return in the variable which made the query invalid but glad to see you got it working.<\/p>\n
I think I would still look at getting the native hyper-v commands working, they do make things much easier, in fact that whole script could probably be done on about 5 lines.<\/p>","upvoteCount":2,"datePublished":"2014-12-17T11:29:55.000Z","url":"https://community.spiceworks.com/t/shutting-down-vms-script-problems/365052/9","author":{"@type":"Person","name":"mattfippard","url":"https://community.spiceworks.com/u/mattfippard"}},{"@type":"Answer","text":"