How to start a VM through Powershell - powershell

I am looking for an automated way to start a virtual machine in Windows Powershell. Does anyone have any input on how I may accomplish this task?
I have looked around and have been unsuccessful so far with finding a way to do so.
I thought that this was a standard cmdlet form powershell, but I am beginning to think that I am wrong on that assumption.
I have looked into using (If i am understanding this correctly) PowerCLIs Start-VM and am having issues saying that I am currently not connected to the server, but I don't believe I have a server to connect to (unless I am thinking about this the wrong way). I am just trying to do this locally right now to check to see if it is a valid VM to use.
--- EDIT ---
I am looking to do this with VMware, but really I could use Windows if that means doing this without spending any money.
I am also using powershell 2.0 and it does not seem that Start-VM is an option for me to use unfortunately...
--- EDIT 2 ---
Can anybody further explain how WMI works? I am relatively new using powershell and I have never had to use WMI before.
Any help would be much appreciated. Thank you!

Extension of my comment above... this code snippet should do what you need:
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='serverName'" -namespace "root\virtualization" -computername "."
$VM.requeststatechange(2)
WMI is pretty dated though (PowerShell 1.0), this works for me on Server 2008 R2. You should add what OS you're working with. If you're on Server 2012, it's a lot easier. Just use Start-VM (PowerShell 4.0):
Start-VM –Name VMName
Arguably a lot easier than using WMI.

Related

Powershell 6.2 and Get-CimInstance to remotely stop/start/restart service issues

So I am at a big roadblock right now. I was working in PowerShell 5.1 and had the Get-WmiObject with the Win32_Service Class working perfectly fine to remotely stop/start/restart services remotely. However, I found out that the .StopService() method has been removed in PowerShell 6 (which I used this to bypass the dependencies issues I kept running in to). As well, I have found out that the -ComputerName variable has also been removed from a lot of the different commandlets.
Since the removed the -ComputerName from things like the Stop-Service commandlet, I am struggling to figure out how to properly handle the stopping of the service itself. I can retrieve the service or services I want without issue. I just can't seem to figure out how to handle stopping the service.
Tried being as detailed as I can. I know I am missing something stupidly small, but all of my Google searches appear to return everything with PowerShell 5, but so little on 6.
Thanks.
Ok, nevermind about this question. I decided to approach my Google searching another way and I stumbled upon the syntax I needed. For those of you wondering, you cannot run commands straight from the returned object. So for example, you could do "$service.StopService()" straight on the service previously. However, now you have to do an invoke of that StopService method by doing "Invoke-CimMethod -Name StopService" and that is only after you pipe the Get-CimInstance returned object to it. So the full syntax would look something like this.
get-ciminstance win32_service -filter "Name='spooler'" | Invoke-CimMethod -Name StartService
A really good website I found to explain and give really good examples of why and how the Cim Instances are they way they are.
https://4sysops.com/archives/managing-services-the-powershell-way-part-7

How do I use Get-EventLog to get the same result of Get-WinEvent in PowerShell?

I am working on Windows Server 2003 and I need to get something like the following by using this command Get-WinEvent -ListLog Application, Security, System
LogMode MaximumSizeInBytes RecordCount LogName
------- ------------------ ----------- -------
Circular 33554432 15188 Application
Circular 201326592 298459 Security
Circular 33554432 10074 System
I need the result of the property MaximumSizeInBytes but Get-WinEvent is not supported on Server 2003
I see that Get-EventLog has a property called MaximumKilobytes but the result I get is different
I would like to know if there is a command can be ran locally to get the same result
First why are you still on WS2K3? --- ;-}
Before you respond, I know, I know, some orgs... right!? ;-}
Yet, unless someone on this site has WS2K3, there is no way for them to validate stuff.
This cmdlet not supported on WS2K3 is not a bug or missing thing. cmdlets are OS version and PowerShell version specific.
All that being said. Just because a command does not exist on your system, does not mean you cannot try use it.
This is why implicit PSRemoting exists.
Remoting the Implicit Way
Using implicit PowerShell remoting to import remote modules
Mostly you see this used for ADDS, Exchange, O365 cmdlets and the like, but you can do it for any module / cmdlet on a remote host to use on your local session. Using implicit remoting the cmdlet really does not run on your system it is proxied. Just be sure to use the -prefix argument so to not end up with duplicate cmdlets being listed.
Example
$RemoteSession = New-PSSession -ComputerName 'RemoteHost' -Credential (Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME")
Import-PSSession -Session $RemoteSession -Prefix RS
So, no you call the cmdlets using the prefix when you want to use one from that session.
Get-RSWinEvent
Now, as I said, I have no WS2K3 boxes to mess with as I am all WS2K12R2/16/19. Yet, give it a shot.
As no one has provided a satisfying answer yet I will just post the answer I found online here. The following command saved my life:
Get-WmiObject -Class Win32_NTEventLogFile | Select-Object -Property MaxFileSize, LogfileName, Name, NumberOfRecords
I will not choose my own answer as the final answer just yet so if you can think of a better solution please feel free to add it :)
Thank you for viewing my post and tried to help

Trouble with Remote Sessions and Exchange 2007 Servers

Anyway, I'm working on some Powershell scripts for work, and I'm stuck on something I thought would be fairly simple. Basically, there are some tasks that I need to perform on our Exchange server that would be great to script. I spent time writing a script that would connect me to our server (on the same domain), and run invoke-command for a few test commands. Once I get the test commands running, I can actually start writing the meat of the script.
For now however, I'm stuck just getting get-mailbox to return any information, which is really just me trying to test that things are working. See the three lines of test code here:
$mainSession = New-PSSession -ComputerName TheServerName
invoke-command -session $mainSession -ScriptBlock {add-pssnapin Microsoft.Exchange.Management.Powershell.Admin}
invoke-command -session $mainSession -ScriptBlock {get-mailbox}
I get a "psremotingtransportexception" exception with a "Job_Failure" error. What could this be? I know the snap-in and get-mailbox commands work when I actually log-in to the server via remote-desktop and test them out. Assuming I can get get-mailbox to function, I could actually perform the more complicated tasks I want to put in the script. The Exchange Server I'm connecting to is pretty old (2007?), so it's not that I'm using the wrong snap-in. Am I missing some credentials? Right now it's not asking me for any, but I tried adding the -credential flag and that didn't help either.
I'm super new to Powershell, so any help is greatly appreciated. Here is the exact error, apologies for the crap formatting:
Processing data for a remote command failed with the following error message: The WSMan provider host process did not return a proper response. A provider in the host process may have behaved improperly. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OperationStopped: (System.Manageme...pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRemotingTransportException + FullyQualifiedErrorId : JobFailure
I should mention that I already tried increasing the shell MB size via set-item wsman:\localhost\shell\MaxMemoryPerShellMB 1024 -force. My best guess is this has something to do with the -credential flag but so far I haven't had any success with it.
Official support for remote Exchange administration with PowerShell was added in Exchange 2010, I believe.
It seems at least one person has found a way to make it work with Exchange 2007.
Sorry for the link-only answer, but the solution seems to be pretty involved, using impersonation, a special user account created for this purpose, script modules that have to be installed on the exchange server, etc.
Given this solution, I wonder if you could make it work more simply by using a PowerShell endpoint running as an exchange administrator. That would be cleaner than this mess. Basically you would create the endpoint, set the RunAsUser, and then you could use StartupScript to add the snapin. I really can't say if this will work though and I have no access to a Exchange 2007 environment where I could test it.

Powershell - Refresh SNMP from registry or do a SNMPServiceResetEvent

I've written a powershell script that writes registry entries for network drivers to change DCB settings. Things like turning DCB on and off, defining traffic classes and bandwidth groups. After writing the values to registry sometimes you cannot see the changes with SNMP remotely. Though this is inconsistent.
I've scoured the web to see if there is a way to force SNMP to get its values from registry again. We have a script that sets the values in SNMP which automatically changes the registry. I'm trying to go the opposite way and set the values in the registry and have the MIB updated. I've tried reseting the SNMP service and network device in the script with no luck.
After modifying the registry, do a SetEvent on the global event named
"SNMPServiceResetEvent". (That is, do a CreateEvent to that named event and
then do a SetEvent). That should cause the agent to reintialize using the
current registry values.
Jeff Kelley
Microsoft / Windows CE Networking
The only thing I've found that sounds like what I need is the above quote, though futher research suggests he is talking about C++ or C#. Is there a way to do what he suggests in powershell? I found a New-Event commandlet though I'm unsure of its suitability to the current task. Code I've tried:
$snmpService = New-Event -sourceidentifier dcbScriptSnmpReset -sender SNMPServiceResetEvent -messagedata "Reset SNMP to refresh Registry"
Though there is no Set-Event commandlet and I don't know how to proceed.
Any help or leads would be much appreciated.
Thanks,
Marcus
Apparently I misunderstood how SNMP worked and now I can get it to update values entered into the registry by enabling and disabling the network adapter and stopping and starting the SNMP service:
Stop-Service $snmpService.Name
$adaptor = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.DeviceID -eq $deviceID }
$adaptor.Disable()
$adaptor.Enable()
Start-Service $snmpService.Name

How to extend volume using powershell?

How to extend volume using powershell (I prefer WMI over powershell remoting) on remote computer ?
OS is win XP sp3.
I ended up with somethin like this:
Invoke-Command -ComputerName $compName -Credential $compCred -ScriptBlock {"rescan","select volume 2","extend" | diskpart}
I'm still looking for better solution, if there is one.
There is a set of scripts Microsoft's Storage Team wrote to handle this that can also be hooked into System Insights as automated remediation actions:
https://blogs.technet.microsoft.com/filecab/2018/06/19/creating-remediation-actions-for-system-insights/
You can also see my answer with PowerShell function here, in Remotely extend a partition using WMI: