Databases Reference
In-Depth Information
Another troubleshooting question you may wish to i nd out is when the computer last booted up.
You can also achieve this with a simple script (code i le: PS_LastBootTime01.PS1 ):
$wmiBootTime = (get-wmiobject Win32_OperatingSystem).lastbootuptime;
[DateTime]$lastBootUpTime =
[Management.ManagementDateTimeConverter]::ToDateTime($wmiBootTime);
$uptime = (Get-Date) - $lastBootUpTime;
Write-Host $lastBootUpTime;
Write-Host $uptime;
In the preceding example I created a variable and stored the last boot-up time, which is initially a
string when retrieved from WMI. I then converted it into a DateTime object. I calculated the uptime
by using the Get-Date cmdlet, subtracting the date on which the machine was rebooted. I then
displayed the last boot-up time and the uptime in the console.
You can build a suite of such statements and scripts that you want to run later, and you can easily
package them into a script that you can run on a server. Don't worry if these scripts look a little
daunting right now. This chapter explains in detail how scripts are composed, and by the end of it,
you will have enough knowledge to understand numerous useful cmdlets and scripts to help with
troubleshooting and diagnostics.
The PowerShell Environment
PowerShell 2 is pre-installed on Windows 7 and Windows Server 2008 R2 operating systems.
Although PowerShell can be installed onto earlier versions of Windows, it is not immediately obvi-
ous from where you can obtain the download. This is because the actual product name that is
indexed by all the popular Internet search engines is “Windows Management Framework,” not
PowerShell. You can i nd the PowerShell download from the following link: http://support
.microsoft.com/kb/968929 .
Once installed, you can either use PowerShell in interactive command-line mode (the shell), or use
a script editor to create script i les containing multiple statements to execute. To launch the interac-
tive command line, run PowerShell.exe or i nd the “Windows PowerShell” shortcut that is installed
in your Start menu. There is an alternate icon in the Start menu called “Windows PowerShell
Modules”; this is similar to the standard “Windows PowerShell” item, but while loading the shell it
also imports all the modules in the PowerShell Modules folder. The following PowerShell variable
allows you to discover where the PowerShell modules are located on your machine:
$env:PSModulePath
The environments variable contains many coni guration settings on your system. You can discover
all of them by listing the contents of the variable as shown below:
PS > get-childitem env:
When you install PowerShell packs as part of Windows features, the PowerShell modules will be
installed here, which provides a quick way to ensure that every Windows feature with PowerShell
support installed has all its functionality available to you when you load the command line.
 
Search WWH ::




Custom Search