I have seen a lot of confusion recently over the use of Select-String.
One mis-conception is that you need to use Get-Content to pipe the file contents into Select-String. Not so. Select-String will read the file for you.
If you just want to scan the files in a single folder to find a specific string then Select-String can do the work for you
Select-String -Path C:\Test*.txt -Pattern “trial” ““SimpleMatch
If you need to work through a folder structure add get-ChildItem to the pipeline
WMI enables you find the number of processors in your system:
PS> Get-WmiObject -Class Win32_ComputerSystem | fl Number*
NumberOfLogicalProcessors : 2
NumberOfProcessors : 1
This works fine for Windows Vista/Windows 2008 and above.
Earlier versions of Windows mis-report the number of processors ““ it counts the number of logical processors reports it as the number of physical processors.
Win32_Processor has the same problem on Windows 2003 and below.
There is a hotfix available from http://support.
A question on my blog asked how do you know which domain controller you are running against when you search Active Directory. Unless you explicitly instruct your script to use a specific domain controller it will use the one to which you authenticated.
You can find the DC to which you authenticated with this simple function
function get-logonserver{
$env:LOGONSERVER -replace “\”, ""
}
Microsoft’s a big company, and that makes it a big target for lawsuits. We all know that. But what doesn’t always sink in is how careful the company has to be.
For example, in Microsoft Official Curriculum course 10961, Automating Administration with Windows PowerShell 3.0, I have to type Windows PowerShell every single time. I’ve actually been using “the shell” a lot, just to break things up a bit. We all casually refer to the shell as PowerShell, but Microsoft never does.
A forum question regarding retrieving WMI based data from multiple servers and displaying it as HTML was interesting. I would approach it like this
`$servers
=
Get-Content
-Path
C:\scripts\servers.txt
$data
=
@(
)
foreach
(
$server
in
$servers
)
{
$compdata
=
New-Object
-TypeName
PSObject
-Property
@{
Computer
=
$server
Contactable
=
$false
LastBootTime
=
""
AllowTSConnections
=
$false
}
if
(
Test-Connection
-ComputerName
$server
-Quiet
-Count
1
)
{
A question on the forum about a function had me thinking. The user had defined two parameters for the function and then used Read-Host to get the values.
NO
Much better way is to use an advanced function and make the parameters mandatory
`function
Getuserdetails
{
[
CmdletBinding
(
)
]
param
(
[
parameter
(
Mandatory
=
$true
)
]
[string]
$Givenname
,
[
parameter
(
Mandatory
=
$true
I"™ve written a series of articles on PowerShell workflows that are appearing on the Scripting Guy blog. The first two in the series have been published at:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/12/26/powershell-workflows-the-basics.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/02/powershell-workflows-restrictions.aspx
Enjoy
Microsoft course 10961, which will be a 5-day course on PowerShell 3.0, is officially in development! We received signoff on the outline this week, and I’ve submitted a first module for review. A big part of that review is making sure I’m using the template properly, as the authoring tool is fairly complex. It does, however, offer (more-or-less) one-touch publishing of the student manual, instructor slide deck, OneNote trainer pack, Lab Answer Key, and other documents, so it’s worth a bit of complexity.
In September 2012, we incorporated PowerShell.org, Inc., and founded PowerShell.org. Our goal was to provide a solid Q&A forum, and to act as a portal to the rest of the PowerShell community.
By any measure, we’ve had a great first showing.
We have more than a dozen shareholders in PowerShell.org, Inc., making this the first community-owned PowerShell organization ever. We’ve signed on three Platinum sponsors - CBT Nuggets, SAPIEN Technologies, and Interface Technical Training.
As I write this, we’re close to sign-off on the outline of 10961A, which is a new 5-day Microsoft course on PowerShell v3. I sat down yesterday and starting doing some detailed-level design work on the proposed Module 9, which will cover PowerShell Remoting.
I love Remoting (and yes, I capitalize the “R” when referring to the specific feature, much as I would for Workflow). And although I’ve taught Remoting over and over and over since it was introduced in v2, although with this course I’m trying something a bit new.