TechNet Webcast Q & A: Advanced Windows PowerShell Scripting - December 15th, 2008
Question 1:
Can I download scripts for Exchange 2007 without installing Exchange on a server or Windows Vista?
Answer 1:
I assume you're asking if you can you get the Exchange PowerShell cmdlets onto your desktop. The only way to do that is to install the Exchange management GUI, which will require PowerShell, and will install the cmdlets you're talking about.
Question 2:
Why would you use "findstr" versus "select-string" ?
Answer 2:
The Select-String cmdlet searches for text and text patterns in input strings and files. You can use it like Grep in Unix and Findstr in Windows. So, select-string in PowerShell, Findstr in cmd.exe.
Question 3:
In the get-service | select-object servicesdependon demo. What's the best way to find an object's properties (servicesdependon in this example)? Especially for .NET or COM objects?
Answer 3:
You should be able to find what you want at the .NET Framework Class Library
UPDATE: Per the comment to the post get-member does the job of course. I had assumed since we talked about get-member in the webcast the questioner was wanting a document location.
Question 4:
How do we access the power shell command prompt
Answer 4:
Download and install PowerShell, it will then be in the Start -> All programs -> Windows PowerShell 1.0 folder.
Question 5:
Is the batch command are compatible with PowerShell script?
Answer 5:
No.
Question 6:
Any chance the test scripts can be made available for those playing along at home?
Answer 6:
Yes!!! Listed below....
script1.ps1
---------------------------------------------------------
$processes = get-process
if ($processes[0].CPU -lt $processes[1].CPU) {
write-host "Process 0 is less than process 1" }
else { "Process 1 is less than or equal to process 0" }
script2.ps1
---------------------------------------------------------
$processes = get-process
foreach ($process in $processes) {
if ($process.ProcessName -eq "notepad") {
$value1 = $process.WS
$id = $process.id }
if ($process.ProcessName -eq "powershell") {
$value2 = $process.WS}
}
if ($value1 -lt $value2) { stop-process $id }
script3.ps1
---------------------------------------------------------
$services = get-service | where-object {$_.status -eq "Running" }
foreach ($service in $services) {
switch -wildcard ($service.DisplayName) {
"win*" {$winsvccount++}
"net*" {$netsvccount++}
"rem*" {$remsvccount++}
default {}
} #switch block
} #foreach block
"There are " + $winsvccount + " running services that begin with 'Win'"
"There are " + $netsvccount + " running services that begin with 'Net'"
"There are " + $remsvccount + " running services that begin with 'Rem'"
script4.ps1
---------------------------------------------------------
$events = get-eventlog "System" -newest 100
function CountEvents{
$count = 0
foreach ($event in $args[0]) {
if ($event.entrytype -like $args[1]) {$count++}
}#foreach block
return $count
}#function block
$errors = CountEvents $events "err*"
$warnings = CountEvents $events "warn*"
$info = CountEvents $events "info*"
"The System log contains " + $errors + " error messages."
"The System log contains " + $warnings + " warning .messages."
"The System log contains " + $info + " information messages."
script5.ps1
---------------------------------------------------------
function CountEvents{
BEGIN {
$errors = 0
$warnings = 0
$info = 0
}
PROCESS {
switch -wildcard ($_.entrytype) {
"err*" {$errors++}
"warn*" {$warnings++}
"info*" {$info++}
default {}
}#switch block
}#process block
END {
"The System log contains " + $errors + " error messages."
"The System log contains " + $warnings + " warning messages."
"The System log contains " + $info + " information messages."
}#end block
}#function block
get-eventlog "System" -newest 100 | CountEvents
script6.ps1
---------------------------------------------------------
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Simple Form"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(110,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)
[void] $objForm.ShowDialog()
script7.ps1
---------------------------------------------------------
$processlist = get-wmiobject –query “select * from win32_process”
foreach ($process in $processlist)
{
$listitem = “Process “+$process.name+” has a working set size of “+$process.workingsetsize
out-file –filepath “C:\processworkingset-ps.txt” –inputobject $listitem –append
}
write-host “Process Listing Complete”
Question 7:
I thought try-catch-finally isn't supported until PowerShell Version 2?
Answer 7:
That's correct, I was talking about version 2 when I mentioned it.
Question 8:
Is there a good resource to discover what class you need? (i.e. there are a LOT of WMI classes that relate to networking/NICs)?
Answer 8:
Check the links below
Win32_NetworkAdapterConfiguration Class
Win32_NetworkAdapterSetting Class
Question 9:
When getting the latest 100 events, the output showed a total events of 4+3+92 = 99. Where is event 100?
Answer 9:
Hmm.... you're right... not sure why. I'll check into it.
Comments
Anonymous
January 01, 2003
I disagree about your answer to #3. The best way to find properties of objects that you are working with is Get-Member. Get-Member -Properties will show the available properties for the pipeline objects. Especially for admins that are not familiar with the MSDN documentation.Anonymous
January 01, 2003
You're right of course. I'll update the post. Thanks