Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Find a string within a variable string

Reply
 
Old 08-14-2008   #1 (permalink)
Edward.A.Gonzalez


 
 

Find a string within a variable string

I'm still learning PowerShell, and here's what I'm trying to
accomplish.

Find out what service pack version my machines are running and then
save this information to a file.

The way I'm going about this task is:
1. I'm going to feed the computer netbios name to the ps script.
2. The ps script will find this information and match the netbios name
to the service pack version
3. Write this information to a file and append to it until it's done.

So far, I'm able to pass a parameter to the script and get the service
pack version; however, I can't figure out how to find a string within
the variable string that holds the results.

Here's what I have so far:
# Handle parameter received from users
param( [string] $ComputerName )


$SpFound = Get-WMIObject Win32_OperatingSystem –Property
ServicePackMajorVersion –Computer $ComputerName


Any suggestions will be greatly appreciated.

My System SpecsSystem Spec
Old 08-14-2008   #2 (permalink)
Kryten


 
 

Re: Find a string within a variable string

Hi Edward,

I have to admit I don't completely understand what you mean, but how
about this:-

function process-servicepack ($Hostname) {
$a = gwmi win32_operatingsystem | Select-Object
"ServicePackMajorVersion"
$a -match "\d" | Out-Null ; $v = $matches[0]
Add-content -path "C:\servicepacks.txt" -Value "Netbios Name :
$hostname`tServicepack : $v"
}

Get-Content "C:\computers.txt" | % {process-servicepack $_ }

Where "C:\computers.txt" is your list of Netbios names. One on each
line.
Where "C:\servicepacks.txt" is your output file containing the result
( I think ) you want.

Hope it helps,

Stuart
My System SpecsSystem Spec
Old 08-14-2008   #3 (permalink)
Kryten


 
 

Re: Find a string within a variable string

Whoops, in my mad rush to post I made a little mistake; I forgot to
put the -computername parameter and value in the gwmi line.
This is the correct code:-

--------------
function process-servicepack ($Hostname) {
$a = gwmi win32_operatingsystem -ComputerName $Hostname | Select-
Object "ServicePackMajorVersion"
$a -match "\d" | Out-Null ; $v = $matches[0]
Add-content -path "C:\servicepacks.txt" -Value "Netbios Name :
$hostname`tServicepack : $v"
}

Get-Content "C:\computers.txt" | % {process-servicepack $_ }
---------------

Sorry about that. The above ought to do what you need...if I
understood properly(!)

Regards,
Stuart
My System SpecsSystem Spec
Old 08-15-2008   #4 (permalink)
IT STAFF


 
 

Re: Find a string within a variable string

I suggest using Quest software which is a free download, it can search for
computer services packs, etc in a liner.




"Kryten" <Kryten68@xxxxxx> wrote in message
news:a42a9485-cbeb-4c7d-a96d-b4f00f884fc6@xxxxxx
Quote:

> Whoops, in my mad rush to post I made a little mistake; I forgot to
> put the -computername parameter and value in the gwmi line.
> This is the correct code:-
>
> --------------
> function process-servicepack ($Hostname) {
> $a = gwmi win32_operatingsystem -ComputerName $Hostname | Select-
> Object "ServicePackMajorVersion"
> $a -match "\d" | Out-Null ; $v = $matches[0]
> Add-content -path "C:\servicepacks.txt" -Value "Netbios Name :
> $hostname`tServicepack : $v"
> }
>
> Get-Content "C:\computers.txt" | % {process-servicepack $_ }
> ---------------
>
> Sorry about that. The above ought to do what you need...if I
> understood properly(!)
>
> Regards,
> Stuart

My System SpecsSystem Spec
Old 08-15-2008   #5 (permalink)
Edward.A.Gonzalez


 
 

Re: Find a string within a variable string

On Aug 14, 6:06*pm, Kryten <Kryte...@xxxxxx> wrote:
Quote:

> Whoops, in my mad rush to post I made a little mistake; I forgot to
> put the -computername parameter and value in the gwmi line.
> This is the correct code:-
>
> --------------
> function process-servicepack ($Hostname) {
> $a = gwmi win32_operatingsystem -ComputerName $Hostname | Select-
> Object "ServicePackMajorVersion"
> $a -match "\d" | Out-Null ; $v = $matches[0]
> Add-content -path "C:\servicepacks.txt" -Value "Netbios Name :
> $hostname`tServicepack : $v"
>
> }
>
> Get-Content "C:\computers.txt" | % {process-servicepack $_ }
> ---------------
>
> Sorry about that. The above ought to do what you need...if I
> understood properly(!)
>
> Regards,
> Stuart
Thanks for the script! (although I was trying to write it myself so I
could learn this thing )
Yes, it was exactly what I needed. I'm just going to go over the code,
that way I can understand how you accomplished the task.

Thanks again.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
variable evaluation in a string PowerShell
How to concatenate a literal string and a variable value? VB Script
Re: how to tokenize a string into a variable via regex PowerShell
SSIS - string variable! .NET General
Subject: using a Variable with/in a string PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46