Which Powershell Cmdlet Can Be Used to ​start a Virtual Machine?

To understand why Offset-Procedure and all of these other commands are limited you first have to understand how a typical EXE file works. When an EXE file is run, information technology performs whatever action information technology was designed to exercise; information technology pings something (ping), starts a software installer (setup), looks up some DNS tape (nslookup), any. For this part and Offset-Process and other commands to beginning a process work great. Information technology's elementary. The limitation comes when that EXE returns some output.

We tin can first a process in PowerShell many different ways. We've got the PowerShell Start-Process and Invoke-Expression cmdlets, we tin telephone call the executable directly or utilize the ampersand (&) to invoke expressions. The most common way is to apply Kickoff-Process because information technology's probably the nigh intuitive. PowerShell is known for it'southward intuitive approach to command naming and First-Process is an first-class choice. Notwithstanding, that command is limited.

Streams

An EXE file like many other executable files not express to Windows has a concept of standard streams. Standard streams are how executable files return output. These streams come in three flavors; stdin, stdout, and stderr. Stdin is the stream that can get passed into the executable which we won't exist focusing on here. Stdout is the stream that the executable uses to send normal non-error output.

In PowerShell terms, retrieve of stdout every bit what Write-Output returns. When an error occurs (depending on if the developer of the executable wrote it correctly), the executable should return output via stderr. This is a stream that's set aside for returning any fault information.

These streams permit users of these executable files to differentiate betwixt what'south typical output and what'southward an fault. Streams have been around for decades and Windows thus PowerShell know all about them and have adopted their own.

Exit Codes

An exit code, return lawmaking or issue lawmaking is another historic period-onetime concept that executable files follow likewise. An go out lawmaking is an integer that allows the executable to signal a status to the user when it exits.

There are some standard get out codes that programs are supposed to follow like if the exit code is 0, everything is fine, exit code 3010 means it needs a reboot and then on. PowerShell can capture this exit code a few different ways like using the $LastExitCode automatic variable. Exit codes are some other method that the executable file communicates to the user.

Capturing Streams and Leave Codes

Now that you understand what we're working with let'southward utilize an example. To keep information technology simple, I'll use the good ol' ping.exe. First, I'll ping google.com which will return a successful outcome. We aren't using PowerShell start procedure here.

            PS C:\> ping google.com Pinging google.com [2607:f8b0:4004:80b::200e] with 32 bytes of data:  Answer from 2607:f8b0:4004:80b::200e: time=61ms Respond from 2607:f8b0:4004:80b::200e: time=65ms Reply from 2607:f8b0:4004:80b::200e: time=80ms Reply from 2607:f8b0:4004:80b::200e: time=78ms  Ping statistics for 2607:f8b0:4004:80b::200e:  Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 61ms, Maximum = 80ms, Average = 71ms  PS> $LastExitCode 0 PS> $? True          

I'll now purposefully ping a host that doesn't resolve a DNS name and will neglect. Notice the value of $LastExitCode.

            PS> ping googlllll.com  Ping request could not find host googlllll.com. Please cheque the name and try again. PS> $LASTEXITCODE 1          

Y'all saw that the go out code differs because the result of ping was unlike but you didn't come across streams because natively PowerShell doesn't understand the difference when it comes to executable files. The text you see in the console is white; not the ruddy error text you lot know and beloved.

We can capture the streams and redirect to files a few different ways like using > and two> similar below. This is the old school way.

            PS> ping googllll.com > output.msg 2> output.err PS> Get-Content .\output.err PS> Get-Content .\output.msg  Ping request could not notice host googllll.com. Please cheque the name and effort again.          

Detect though that, in this case, even though ping.exe is returning an go out code of one (equally shown higher up), that output is nonetheless going to stdout. This is mutual. What stream and exit code that's returned is completely upwardly to the developer and comes in a lot of different styles, unfortunately.

If y'all'd like to get the "new school" manner you could use Start-Process and use the -RedirectStandardError and -RedirectStandardOutput parameters, simply they'd still just become to files.

Limitations of Start-Process

You tin run into that starting a procedure and returning some mutual output isn't likewise common. On summit of that, PowerShell doesn't handle the streams and exit codes too well. If I were to apply Start-procedure to run ping.exe and want to rail what happened in the consequence I'd have to do something like this every time I wanted to run an executable file.

            PS> Starting time-Process -FilePath -NoNewWindow ping -ArgumentList 'google.com' -RedirectStandardOutput output.txt -RedirectStandardError err.txt PS> $LastExitCode PS> Get-Content -Path output.txt PS> Go-Content -Path err.txt          

I notwithstanding wouldn't get my mistake text either!

Permit'southward fix all of this. Download a modest function I created called Invoke-Procedure from the PowerShell Gallery.

            PS> Install-Script 'Invoke-Procedure' PS> . Invoke-Process.ps1          

At present run ping using a valid host and see what happens.

Invoke-Process -FilePath ping -ArgumentList 'google.com'
Invoke-Process -FilePath ping -ArgumentList 'google.com'

Notice how we get the aforementioned output. This is practiced!

Now run ping using an invalid host.

Invoke-Process -FilePath ping -ArgumentList 'googlddde.com'
Invoke-Process -FilePath ping -ArgumentList 'googlddde.com'

Now notice that nosotros get the typical red error text as we'd expect if something went wrong. Invoke-Process uses Start-Process nether the covers to capture stdout, stderr, and the exit code and so sends the output to whatever stream information technology's supposed to go to. This is how it should be!

Too bad PowerShell didn't get in easier to work with streams and exit codes from executable files, but I suppose nowadays, we tin add that in at that place at present that it's open source!

walterssoodia1977.blogspot.com

Source: https://adamtheautomator.com/start-process/

0 Response to "Which Powershell Cmdlet Can Be Used to ​start a Virtual Machine?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel