Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Start-Process in a for loop Powershell

$
0
0

Hi,

I am trying to write a script that starts a defrag on selected volumes. The idea is to get the volume letters of volumes that are greater than 50GB, loop through each letter and add it to a line that calls defrag.exe /H /U /V, and waits until each defrag instance has exited before starting the next. 

Here are my lines:

#Lets do some defrag
$Defrag = 'C:\Windows\System32\Defrag.exe'
$Argumentals = '/H /U /V'
#Get volumes
$MinVolSize = 53687091200 #50GB
$Volumes = @((Get-WmiObject -Class Win32_LogicalDisk) | 
           ? {$_.Size -gt $MinVolSize}).DeviceID

for($vol=0;$vol -lt $Volumes.Length;$vol++){
    
         Write-Host $Defrag $($Volumes[$vol]) $Argumentals
      
}

When I run this, I get the following output in the console:

C:\Windows\System32\Defrag.exe C: /H /U /V
C:\Windows\System32\Defrag.exe D: /H /U /V
C:\Windows\System32\Defrag.exe E: /H /U /V
C:\Windows\System32\Defrag.exe F: /H /U /V


But then if I change the "Write-Host" to "Start-Process", I get 4 instances of this:

Start-Process : A positional parameter cannot be found that accepts argument '/H /U /V'.
At C:\Users\a-ahinton\SkyDrive\Scripts\Overnight.Maintainence.ps1:34 char:10
+          Start-Process $Defrag $($Volumes[$vol]) $Argumentals
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

Would someone please point me in the direction of information that will teach me why this is happening and what I need to know to fix it or prevent this error from throwing? 

Any links, key terms and criticism will be received with much appreciation and many thanks in advance.


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>