July 2007 - Posts
When we work with windows PowerShell command console, we have access to a host of helpful editing features that are built into the console (Similar like DOS command prompt).
For example, we could edit any command by using the left and right arrow keys to move back and forth to positions within the command and use Backspace or Delete keys to remove characters from the command.
Below are some shortcut key(s) to help us to move around in the PS console more efficiently:
Key(s) Function
Up Arrow Moves back one position in the command line history buffer.
Down Arrow Moves forward one position in the command line history buffer.
Page Up Moves to the first command stored in the command history buffer.
Page Down Moves to the last command stored in the command line history buffer.
Home Jumps the cursor to the end of the command line.
End Jumps the cursor to the end of command line.
Ctrl + Left Arrow Moves the cursor the the left a word at a time.
Ctrl + Right Arrow Moves the cursor to the right a word at a time.
The Windows PowerShell command console maintains a list of commands that you executed during the current session – history buffer.
You can view the history buffer by pressing the F7 key. You can then execute any command in the list by scroll up/down the list and press Enter.
You can also use get-history cmdlet to get the list of entries of history buffer:
PS C:\Myworkplace\PS> get-history
Id CommandLine
-- -----------
1 get-process
2 Get-ChildItem
3 get-alias
You can then execute any of the commands in the list by using the invoke-history cmdlet:
invoke-history 2
So the choices are yours…
Windows PowerShell has a built in environment provider ($env) which will help you to access all the environment variables on a computer.
Interested to know what’s in your PATH system environment variable?
Type this :
$path = $env:path
$path
To view all the environment variables and their values use this command
dir env:
As you can see, here we leave off the $ and reference the env: drive instead.
Here is a powershell script to check against a list of servers for McAfee version, Scan engine and virus definition version and date. It was translated/upgraded from my previous posted VB script and I added the repository server column here:
$erroractionpreference = "SilentlyContinue"
$a = New-Object -comobject Excel.Application
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "Server Name"
$c.Cells.Item(1,2) = "AV Product"
$c.Cells.Item(1,3) = "Version"
$c.Cells.Item(1,4) = "Scan Engine"
$c.Cells.Item(1,5) = "Virus Definition"
$c.Cells.Item(1,6) = "Virus Definition Date"
$c.Cells.Item(1,7) = "Repository Server"
$c.Cells.Item(1,8) = "Report Time Stamp"
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$intRow = 2
$colComputers = get-content C:\Myworkplace\McAfee\MachineList.txt
foreach ($strComputer in $colComputers)
{
$c.Cells.Item($intRow,1) = $strComputer
Function GetRegInfo
{
$key="SOFTWARE\Network Associates\TVD\VirusScan Enterprise\CurrentVersion"
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $strComputer)
$regKey = $regKey.OpenSubKey($key)
$Product = $regKey.GetValue("Product")
$c.Cells.Item($intRow,2) = $Product
$productver = $regKey.GetValue("szProductVer")
$c.Cells.Item($intRow,3) = $Productver
$ScanEngine = $regKey.GetValue("szEngineVer")
$c.Cells.Item($intRow,4) = $ScanEngine
$VirDefVer = $regKey.GetValue("szVirDefVer")
$c.Cells.Item($intRow,5) = $VirDefVer
$virDefDate = $regKey.GetValue("szVirDefDate")
$c.Cells.Item($intRow,6) = $virDefDate
}
GetRegInfo
Function GetSiteInfo
{
$x = Test-path "\\$strcomputer\c$\Documents and Settings\All Users\Application Data\McAfee\Common Framework\UpdateHistory.ini"
if($x -eq "True")
{
$y = get-content "\\$strcomputer\c$\Documents and Settings\All Users\Application Data\McAfee\Common Framework\UpdateHistory.ini"
$z = $y[3]
$SiteServer = $z.substring(19,($z.length-19))
}
$c.Cells.Item($intRow,7) = $SiteServer.ToUpper()
}
GetSiteInfo
$c.Cells.Item($intRow,8) = Get-date
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()
cls
Resveratrol may slow aging - Jan. 19, 2007.
Key word: Red wine, Reseveratrol, Sirtris, CR – Calorie Restriction and mitochondria
Key person: Christoph Westphal, David Sinclair
I am ready to buy their shares!
If you want to add more power or to have more fun with PowerShell, you could try adding PowerGadgets to your toolbox.
PowerGadgets is a PowerShell snap-in that add charts, gauges and maps to your scripts.
Try save the following script as FolderSize.ps1:
$items = get-childitem $args
foreach($item in $items)
{if($item -is [System.IO.DirectoryInfo])
{add-member -inputobject $item -membertype ScriptProperty -Name FolderSize
-Value{$a = get-childitem $this.fullname -include *.* -recurse |
measure-object -sum Length;if($a-eq $null) {return 0;}else {return $a.sum}}
write-output $item
}
}
Then type FolderSize | out-chart
Now you can see PowerGadgets in Action!
Of course, you need to have PowerGadgets installed first! You can get a free trial copy of PowerGadgets at www.powergadgets.com
Understanding the Profiles
As in you can see in my previous post, when you add aliases, functions, and variables to Windows PowerShell, you are actually adding them only to the current Windows PowerShell session. If you exit the session or close Windows PowerShell, the changes are lost.
To retain these changes, you can create a Windows PowerShell profile and add the aliases, functions, and variables to the profiles. The profile is loaded every time that Windows PowerShell starts.
You can have four different profiles in Windows PowerShell. The profiles are listed in load order. The most specific profiles have precedence over less specific profiles where they apply.
-
%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
This profile applies to all users, but only to the Microsoft.PowerShell shell.
-
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
This profile applies only to the current user, but affects all shells.
Creating a Profile
Windows PowerShell profiles are not created automatically. To create a profile, create a text file with the specified name in the specified location. Typically, you will use the user-specific, shell-specific profile, known as the Windows PowerShell user profile. The location of this profile is stored in the $profile variable.
To display the path to the Windows PowerShell profile, type:
To determine whether a Windows PowerShell profile has been created on the system, type:
If the profile exists, the response is True; otherwise, it is False.
To create a Windows PowerShell profile file, type:
new-item -path $profile -itemtype file -force
To open the profile in Notepad, type:
To create one of the other profiles, such as the profile that applies to all users and all shells, type:
new-item -path C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 -itemtype file -force
As you guessed, PowerShell has built-in Alias to help you reduce your keystrokes or to “simulate” the command from another scripting language. For example, cd is the PowerShell Alias for set-location and pwd is the Alias for get-location.
Get-Alias (ls Alias: ) will list the aliases for the current session which includes built-in alias, aliases that you have set.
You can create your own alias using new-alias:
Set-Alias help get-help
By default, aliases(you created) are not saved between Windows PowerShell sessions: each time you restart Windows PowerShell you will need to recreate the alias. To permanently save your alias, create a file named Microsoft.PowerShell_profile.ps1 and add the following line:
Set-Alias help Get-Help
and save it in C:\WINDOWS\system32\windowspowershell\v1.0 folder.
It’s official, I got the 2007 Microsoft MVP Award. Cheers!
Thanks Rod Trent and Don Hite from myITforum for their continued support.