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

Why version with redirection output run with error?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-04-2008   #1 (permalink)
David Kriz
Guest


 

Why version with redirection output run with error?

Please, I am very confused why this script run OK:

/------------------------------------8<----------------------------------\
Start-Transcript C:\Temp\PowerShell-Transcript.log

[string[]]$ComputerNamesList = @("PC061","PC062","PC063","PC064")

for ($I=0;$I -le 4;$I++) {
$S = $ComputerNamesList[$I]
schtasks.exe /Query /FO LIST /V /S $S
}

Stop-Transcript

\------------------------------------8<----------------------------------/

but this NO:

/------------------------------------8<----------------------------------\
Start-Transcript C:\Temp\PowerShell-Transcript.log

[string[]]$ComputerNamesList = @("PC061","PC062","PC063","PC064")

for ($I=0;$I -le 4;$I++) {
$S = $ComputerNamesList[$I]
schtasks.exe /Query /FO LIST /V /S $S > Tasks.log
}

Stop-Transcript

\------------------------------------8<----------------------------------/



PS show this output:
/------------------------------------8<----------------------------------\
PS D:\W> .\Sch.ps1
Transcript started, output file is C:\Temp\PowerShell-Transcript.log
ERROR: Invalid syntax. Value expected for '/S'.
Type "SCHTASKS /QUERY /?" for usage.
Transcript stopped, output file is C:\Temp\PowerShell-Transcript.log
\------------------------------------8<----------------------------------/



In file C:\Temp\PowerShell-Transcript.log is:
/------------------------------------8<----------------------------------\
**********************
Windows PowerShell Transcript Start
Start time: 20080704174742
Username : CCV\admindavid
Machine : SRV-SIEMENS (Microsoft Windows NT 5.2.3790 Service Pack 2)
**********************
Transcript started, output file is C:\Temp\PowerShell-Transcript.log
**********************
Windows PowerShell Transcript End
End time: 20080704174745
**********************
\------------------------------------8<----------------------------------/

My System SpecsSystem Spec
Old 07-04-2008   #2 (permalink)
Kirk Munro [MVP]
Guest


 

Re: Why version with redirection output run with error?

Hi David,

I agree with Shay. The script you have is processing 5 computer names when
you only have 4. So when the last one get's processed, $S is null and you
get an error on your call.

You don't need to use the for statement and indexing at all. You could do
this instead:

Start-Transcript C:\Temp\PowerShell-Transcript.log
[string[]]$ComputerNamesList = @("PC061","PC062","PC063","PC064")
foreach ($computer in $ComputerNamesList) {
schtasks.exe /Query /FO LIST /V /S $computer > Tasks.log
}
Stop-Transcript

That way it doesn't matter how many computers you put in your list.

--
Kirk Munro [MVP]
Poshoholic
http://poshoholic.com

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Folder Redirection Error John Whites Vista General 0 08-11-2008 01:43 PM
Redirection of output gurbao PowerShell 4 01-28-2008 08:29 AM
ansi versus unicode output with redirection operators? klumsy@xtra.co.nz PowerShell 10 12-02-2006 11:25 PM
Problem with output redirection of nslookup =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= PowerShell 8 08-24-2006 04:04 AM
Why output redirection opens file with read lock? Andrew Savinykh PowerShell 10 08-21-2006 07:41 AM


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 51