/------------------------------------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<----------------------------------/
I can't test the code but it looks like your for loop condition is exceeding
the array upper bound.
Try to change it to '-lt 4' or even better, try not to hard code the limit,
set it to '-lt $ComputerNamesList.length'
---
Shay Levy
Windows PowerShell MVP
blog: http://scriptolog.blogspot.com
DK> Please, I am very confused why this script run OK:
DK>
DK> /------------------------------------8<-----------------------------
DK> -----\ Start-Transcript C:\Temp\PowerShell-Transcript.log
DK>
DK> [string[]]$ComputerNamesList = @("PC061","PC062","PC063","PC064")
DK>
DK> for ($I=0;$I -le 4;$I++) {
DK> $S = $ComputerNamesList[$I]
DK> schtasks.exe /Query /FO LIST /V /S $S
DK> }
DK> Stop-Transcript
DK>
DK> \------------------------------------8<-----------------------------
DK> -----/
DK>
DK> but this NO:
DK>
DK> /------------------------------------8<-----------------------------
DK> -----\ Start-Transcript C:\Temp\PowerShell-Transcript.log
DK>
DK> [string[]]$ComputerNamesList = @("PC061","PC062","PC063","PC064")
DK>
DK> for ($I=0;$I -le 4;$I++) {
DK> $S = $ComputerNamesList[$I]
DK> schtasks.exe /Query /FO LIST /V /S $S > Tasks.log
DK> }
DK> Stop-Transcript
DK>
DK> \------------------------------------8<-----------------------------
DK> -----/
DK>
DK> PS show this output:
DK> /------------------------------------8<-----------------------------
DK> -----\
DK> PS D:\W> .\Sch.ps1
DK> Transcript started, output file is C:\Temp\PowerShell-Transcript.log
DK> ERROR: Invalid syntax. Value expected for '/S'.
DK> Type "SCHTASKS /QUERY /?" for usage.
DK> Transcript stopped, output file is C:\Temp\PowerShell-Transcript.log
DK> \------------------------------------8<-----------------------------
DK> -----/
DK> In file C:\Temp\PowerShell-Transcript.log is:
DK> /------------------------------------8<-----------------------------
DK> -----\
DK> **********************
DK> Windows PowerShell Transcript Start
DK> Start time: 20080704174742
DK> Username : CCV\admindavid
DK> Machine : SRV-SIEMENS (Microsoft Windows NT 5.2.3790 Service Pack
DK> 2)
DK> **********************
DK> Transcript started, output file is C:\Temp\PowerShell-Transcript.log
DK> **********************
DK> Windows PowerShell Transcript End
DK> End time: 20080704174745
DK> **********************
DK> \------------------------------------8<-----------------------------
DK> -----/
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