Hi, i want to create a script for my hyper-v virtual machine.
\nThis scritp start vm if the vm is off.I know the command to do that, but the syntax is my problem.
\nIn this scritp i want to select 1 virtual machine and if it is off, my script just start it, but if my vm is on, the script write me “the vm is running”.
\nAt this time i write a script but the syntax is not correct, please help me!<\/p>\n
I think to use this command:<\/p>\n
if (Get-VM | Format-Table name, state -eq running) {write-host -foregroundcolor red \"VM running}\nelse(start-vm -name \"name\")\n<\/code><\/pre>","upvoteCount":3,"answerCount":5,"datePublished":"2017-05-10T06:03:15.000Z","author":{"@type":"Person","name":"lucatorresi","url":"https://community.spiceworks.com/u/lucatorresi"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
sorry but you scritp not run.
\ni solved!!!
\nThanks to all.<\/p>","upvoteCount":0,"datePublished":"2017-05-10T06:44:04.000Z","url":"https://community.spiceworks.com/t/powershell-if-and-else-scripts/579698/4","author":{"@type":"Person","name":"lucatorresi","url":"https://community.spiceworks.com/u/lucatorresi"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hi, i want to create a script for my hyper-v virtual machine.
\nThis scritp start vm if the vm is off.I know the command to do that, but the syntax is my problem.
\nIn this scritp i want to select 1 virtual machine and if it is off, my script just start it, but if my vm is on, the script write me “the vm is running”.
\nAt this time i write a script but the syntax is not correct, please help me!<\/p>\n
I think to use this command:<\/p>\n
if (Get-VM | Format-Table name, state -eq running) {write-host -foregroundcolor red \"VM running}\nelse(start-vm -name \"name\")\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2017-05-10T06:03:15.000Z","url":"https://community.spiceworks.com/t/powershell-if-and-else-scripts/579698/1","author":{"@type":"Person","name":"lucatorresi","url":"https://community.spiceworks.com/u/lucatorresi"}},{"@type":"Answer","text":"if (Get-VM | Format-Table name, state -eq running) {write-host -foregroundcolor red \"VM running\"}\nelse(start-vm -name \"name\")\n<\/code><\/pre>\nYou’re missing a ’ \" ’ in the line “VM running” ?<\/p>","upvoteCount":1,"datePublished":"2017-05-10T06:13:25.000Z","url":"https://community.spiceworks.com/t/powershell-if-and-else-scripts/579698/2","author":{"@type":"Person","name":"michaelengel3777","url":"https://community.spiceworks.com/u/michaelengel3777"}},{"@type":"Answer","text":"
This should be better:<\/p>\n
if (Get-VM | Format-Table name, state -eq running)\n{\n write-host -foregroundcolor red \"VM running\"\n}\nelse \n{\n start-vm -name \"name\"\n}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2017-05-10T06:17:14.000Z","url":"https://community.spiceworks.com/t/powershell-if-and-else-scripts/579698/3","author":{"@type":"Person","name":"michaelengel3777","url":"https://community.spiceworks.com/u/michaelengel3777"}},{"@type":"Answer","text":"Well, good for you.<\/p>\n
But you could post your solution in case someone else stumbles upon this thread.<\/p>","upvoteCount":2,"datePublished":"2017-05-10T06:45:46.000Z","url":"https://community.spiceworks.com/t/powershell-if-and-else-scripts/579698/5","author":{"@type":"Person","name":"michaelengel3777","url":"https://community.spiceworks.com/u/michaelengel3777"}}]}}