-->
Geek School: Writing Your Showtime Amount Powershell Script

Geek School: Writing Your Showtime Amount Powershell Script

Geek School: Writing Your Showtime Amount Powershell Script

https://www.howtogeek.com/141495/geek-school-writing-your-first-full-powershell-script/

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

A few weeks ago, The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was started upward last. In this concluding installation of Geek School for PowerShell, nosotros are going to write a reusable PowerShell ascendency to produce the same thing.

Be certain to read the previous articles inward the series:

Writing Your First Script

The commencement affair nosotros ask to produce is notice a way to access the data nosotros looking for. Since nosotros are dealing amongst administration information, nosotros belike ask to direct maintain a aspect at using WMI, which does indeed direct maintain a bird called Win32_OperatingSystem which allows you lot to sentiment verbose data almost your operating system, including the concluding fourth dimension it started up.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

So forthwith that nosotros know where nosotros tin notice the data nosotros looking for, opened upward up the ISE as well as type the following.

Get-WmiObject -Class Win32_OperatingSystem –ComputerName localhost |

Select-Object -Property CSName,LastBootUpTime

Note: I had to separate my code over 2 lines hence that it all check into the screenshot, but experience gratis to type it on a unmarried line. If you lot produce direct to separate it over 2 lines, delight brand certain that the pipage grapheme is the concluding grapheme on trace 1.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Now click on the greenish "Run Script" push or press the F5 fundamental on your keyboard to examination the code.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

WMI times tin live a petty cryptic. By that nosotros hateful if you lot aspect at the LastBootUpTime property, it says 2013-03-19 at 18:26:21, but for to a greater extent than or less argue the WMI guys decided to concatenate all of that into a unmarried string. Luckily for us, nosotros don't direct maintain to worry almost manually parsing the string equally in that place is an easier way to produce it, albeit to a greater extent than advanced. You volition ask to modify the Select-Object part of the code to aspect similar this:

Select-Object -Property CSName,@{n="Last Booted";

e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}

What nosotros are doing hither is creating a custom belongings called "Last Booted" as well as specifying that its value must live the lawsuit of calling the ToDateTime static method on the LastBootUpTime belongings of the electrical flow pipeline object. Your code should forthwith aspect similar this.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Running the code forthwith volition yield a much to a greater extent than readable concluding kicking time.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Now that nosotros are happy amongst the basic functionality of our script, nosotros ask to relieve it. For simplicity, let's relieve it equally the following:

C:\Get-LastBootTime.ps1

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Now switch to the bottom one-half of the ISE as well as run the following:

C:\Get-LastBootTime.ps1

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Great! Our script is working equally expected, withal in that place is soundless 1 work amongst our script. We hardcoded the cite of the figurer nosotros desire to acquire the concluding kicking fourth dimension for. Instead of hardcoding values, nosotros should rather render a parameter hence that whoever is using the script tin direct which figurer they run the script against. To produce that, acquire to the give of your script as well as produce the following.

param(
[string]$ComputerName
)

Then supersede the hardcoded localhost value amongst $ComputerName variable. Your script should forthwith aspect similar this:

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Save your script, hence caput dorsum to the bottom one-half of the ISE as well as sentiment the assistance for your script.

help C:\Get-LastBootTime.ps1

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Awesome, hence forthwith nosotros tin specify the cite of the figurer nosotros desire to acquire the concluding kicking fourth dimension for using our novel ComputerName parameter. Unfortunately, in that place are soundless a few things wrong. Firstly, the ComputerName parameter is optional as well as secondly, that's the ugliest helpful I've e'er seen, hence let's cook those issues quickly. To brand the ComputerName parameter mandatory, modify the contents of the param block to the following.

[Parameter(Mandatory=$true)][string]$ComputerName

As for making a amend assistance file, the most mutual method is to utilisation comment based help. That way nosotros but add together an extra long comment to the give of out script.

<#
.SYNOPSIS
Shows when concluding your PC started up.
.DESCRIPTION
This is a WMI wrapper business office to acquire the fourth dimension that your PC concluding started up.
.PARAMETER ComputerName
The cite of the Computer you lot desire to run the ascendency against.
.EXAMPLE
Get-LastBootTime -ComputerName localhost
.LINK
www.howtogeek.com
#>

Once all that is done, you lot should halt upward amongst a script looking similar this.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Let's forthwith acquire as well as banking enterprise check out our novel assistance file.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

Ahhh, looking great! Now that our script is complete, nosotros direct maintain 1 concluding affair to do: testing. For this, I'm going to travel out out of the ISE as well as caput dorsum into the PowerShell console but hence that nosotros tin brand certain that in that place are no anomalies.

 The Geek showed you lot how you lot tin utilisation the ascendency prompt to notice when your figurer was sta Geek School: Writing Your First Full PowerShell Script

If you lot start amongst a uncomplicated 1 liner as well as but cash inward one's chips along edifice onto it similar nosotros did inward this guide, you lot volition acquire the hang of it inward no time. That's all for this fourth dimension folks, run into you lot inward the side past times side installation of Geek School.


Blogger
Disqus
Pilih Sistem Komentar

No comments

Advertiser