Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Newbie - Writing to host and a text file

Closed Thread
 
Thread Tools Display Modes
Old 02-15-2007   #1 (permalink)
FreddieF
Guest


 

Newbie - Writing to host and a text file

I would like to list some information about services and write the
information to the console and to a file. Here's my script and a few
questions:

$strComputer = "server1"
Get-WmiObject win32_service -ComputerName $strComputer |
Select-Object Name,State,StartMode,StartName |
Out-File $strComputer+"_services.txt"

1. How do I write it to the console as well? When I try Write-Host it
returns unformated text, so don't think that's appropriate here.
2. How do I concatenate the filename so it's "server1_services.txt". It
seems to ignore the + operator.

Thanks

Old 02-15-2007   #2 (permalink)
$hay
Guest


 

Re: Newbie - Writing to host and a text file

$strComputer = "server1"
gwmi -q "select * from win32_service" -ComputerName $strComputer | Select
Name,State,StartMode,StartName | tee-object "$strComputer_services.txt"


--
$hay
http://scriptolog.blogspot.com



"FreddieF" <FreddieF@discussions.microsoft.com> wrote in message
news:8D7F6E14-3FB5-4AEA-8F9F-9EDA407C687A@microsoft.com...
>I would like to list some information about services and write the
> information to the console and to a file. Here's my script and a few
> questions:
>
> $strComputer = "server1"
> Get-WmiObject win32_service -ComputerName $strComputer |
> Select-Object Name,State,StartMode,StartName |
> Out-File $strComputer+"_services.txt"
>
> 1. How do I write it to the console as well? When I try Write-Host it
> returns unformated text, so don't think that's appropriate here.
> 2. How do I concatenate the filename so it's "server1_services.txt". It
> seems to ignore the + operator.
>
> Thanks
>


Old 02-15-2007   #3 (permalink)
Jeffery Hicks
Guest


 

Re: Newbie - Writing to host and a text file

On Thu, 15 Feb 2007 11:38:07 -0800, FreddieF wrote:

> I would like to list some information about services and write the
> information to the console and to a file. Here's my script and a few
> questions:
>
> $strComputer = "server1"
> Get-WmiObject win32_service -ComputerName $strComputer |
> Select-Object Name,State,StartMode,StartName |
> Out-File $strComputer+"_services.txt"
>
> 1. How do I write it to the console as well? When I try Write-Host it
> returns unformated text, so don't think that's appropriate here.
> 2. How do I concatenate the filename so it's "server1_services.txt". It
> seems to ignore the + operator.
>
> Thanks


Instead of Out-File, Use Tee-Object

$strComputer = "server1"
Get-WmiObject win32_service -ComputerName $strComputer |
Select-Object Name,State,StartMode,StartName |
Tee-object $strComputer+"_services.txt"

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Old 02-15-2007   #4 (permalink)
FreddieF
Guest


 

Re: Newbie - Writing to host and a text file

Thanks.

I still can't work out how to use the + operator to create the file name
server1_services.txt? With VBscript you would just use
strComputer & "_services.txt"

"Jeffery Hicks" <"jhicks[at]SAPIEN.com" wrote:

> On Thu, 15 Feb 2007 11:38:07 -0800, FreddieF wrote:
>
> > I would like to list some information about services and write the
> > information to the console and to a file. Here's my script and a few
> > questions:
> >
> > $strComputer = "server1"
> > Get-WmiObject win32_service -ComputerName $strComputer |
> > Select-Object Name,State,StartMode,StartName |
> > Out-File $strComputer+"_services.txt"
> >
> > 1. How do I write it to the console as well? When I try Write-Host it
> > returns unformated text, so don't think that's appropriate here.
> > 2. How do I concatenate the filename so it's "server1_services.txt". It
> > seems to ignore the + operator.
> >
> > Thanks

>
> Instead of Out-File, Use Tee-Object
>
> $strComputer = "server1"
> Get-WmiObject win32_service -ComputerName $strComputer |
> Select-Object Name,State,StartMode,StartName |
> Tee-object $strComputer+"_services.txt"
>
> --
> Jeffery Hicks
> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
> VBScript & Windows PowerShell Training -
> www.ScriptingTraining.com/classes.asp
> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>
> blog: http://blog.SAPIEN.com
> blog: http://jdhitsolutions.blogspot.com
>

Old 02-15-2007   #5 (permalink)
Jeffery Hicks
Guest


 

Re: Newbie - Writing to host and a text file

On Thu, 15 Feb 2007 13:07:10 -0800, FreddieF wrote:

> Thanks.
>
> I still can't work out how to use the + operator to create the file name
> server1_services.txt? With VBscript you would just use
> strComputer & "_services.txt"
>
> "Jeffery Hicks" <"jhicks[at]SAPIEN.com" wrote:
>
>> On Thu, 15 Feb 2007 11:38:07 -0800, FreddieF wrote:
>>
>>> I would like to list some information about services and write the
>>> information to the console and to a file. Here's my script and a few
>>> questions:
>>>
>>> $strComputer = "server1"
>>> Get-WmiObject win32_service -ComputerName $strComputer |
>>> Select-Object Name,State,StartMode,StartName |
>>> Out-File $strComputer+"_services.txt"
>>>
>>> 1. How do I write it to the console as well? When I try Write-Host it
>>> returns unformated text, so don't think that's appropriate here.
>>> 2. How do I concatenate the filename so it's "server1_services.txt". It
>>> seems to ignore the + operator.
>>>
>>> Thanks

>>
>> Instead of Out-File, Use Tee-Object
>>
>> $strComputer = "server1"
>> Get-WmiObject win32_service -ComputerName $strComputer |
>> Select-Object Name,State,StartMode,StartName |
>> Tee-object $strComputer+"_services.txt"
>>
>> --
>> Jeffery Hicks
>> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
>> VBScript & Windows PowerShell Training -
>> www.ScriptingTraining.com/classes.asp
>> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>>
>> blog: http://blog.SAPIEN.com
>> blog: http://jdhitsolutions.blogspot.com
>>


You don't need anything really
$strComputer_services.txt
but the problem is the _ character. Try
$strComputer-services.txt

and see if that works better.
--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Old 02-16-2007   #6 (permalink)
Marcel J. Ortiz [MSFT]
Guest


 

Re: Newbie - Writing to host and a text file

> You don't need anything really
> $strComputer_services.txt
> but the problem is the _ character. Try
> $strComputer-services.txt
>
> and see if that works better.


An alternative is:

out-file "${strComputer}_services.txt"

"Jeffery Hicks" <"jhicks[at]SAPIEN.com"> wrote in message
news:r5kjwtlulzjh.1q5ymj0ek9j3t$.dlg@40tude.net...
> On Thu, 15 Feb 2007 13:07:10 -0800, FreddieF wrote:
>
>> Thanks.
>>
>> I still can't work out how to use the + operator to create the file name
>> server1_services.txt? With VBscript you would just use
>> strComputer & "_services.txt"
>>
>> "Jeffery Hicks" <"jhicks[at]SAPIEN.com" wrote:
>>
>>> On Thu, 15 Feb 2007 11:38:07 -0800, FreddieF wrote:
>>>
>>>> I would like to list some information about services and write the
>>>> information to the console and to a file. Here's my script and a few
>>>> questions:
>>>>
>>>> $strComputer = "server1"
>>>> Get-WmiObject win32_service -ComputerName $strComputer |
>>>> Select-Object Name,State,StartMode,StartName |
>>>> Out-File $strComputer+"_services.txt"
>>>>
>>>> 1. How do I write it to the console as well? When I try Write-Host it
>>>> returns unformated text, so don't think that's appropriate here.
>>>> 2. How do I concatenate the filename so it's "server1_services.txt".
>>>> It
>>>> seems to ignore the + operator.
>>>>
>>>> Thanks
>>>
>>> Instead of Out-File, Use Tee-Object
>>>
>>> $strComputer = "server1"
>>> Get-WmiObject win32_service -ComputerName $strComputer |
>>> Select-Object Name,State,StartMode,StartName |
>>> Tee-object $strComputer+"_services.txt"
>>>
>>> --
>>> Jeffery Hicks
>>> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
>>> VBScript & Windows PowerShell Training -
>>> www.ScriptingTraining.com/classes.asp
>>> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>>>
>>> blog: http://blog.SAPIEN.com
>>> blog: http://jdhitsolutions.blogspot.com
>>>

>
> You don't need anything really
> $strComputer_services.txt
> but the problem is the _ character. Try
> $strComputer-services.txt
>
> and see if that works better.
> --
> Jeffery Hicks
> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
> VBScript & Windows PowerShell Training -
> www.ScriptingTraining.com/classes.asp
> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>
> blog: http://blog.SAPIEN.com
> blog: http://jdhitsolutions.blogspot.com



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto: Add lines of text from a specific point in a text file.. Daz VB Script 13 06-24-2008 10:55 AM
writing out to file Braden C. Roberson-Mailloux PowerShell 3 05-18-2008 01:12 PM
Newbie - Output to a text file problem. Kryten PowerShell 9 10-11-2007 04:46 PM
How do I read a text file and sort text by fixed positions? Cornelius PowerShell 5 07-20-2007 06:34 PM
Writing to host in functions and cmdlets - how to capture this output Keith Hill [MVP] PowerShell 1 12-30-2006 03:54 PM








Vistax64.com 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 2005-2008

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 47 48 49 50