locked
Missing statement body in foreach loop RRS feed

  • Question

  • Hi,

    I am receiving the error, Missing statement body in foreach loop.   Any suggestions on what I am doing wrong?  

    $Shares = get-WmiObject -class Win32_Share -computer Server51 -Filter "path like 'E:%' and name != 'E$'" 
    Foreach($folder in $Shares) {
    $shareCommand = $shareCommand + "Get-SmbShare -Name " + $folder.Name + " -Path " + $folder.Path + " -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'" + "`r`n"
    }
    Out-File -FilePath E:\SharesScript.ps1 -InputObject $shareCommand -Encoding ascii
    $shareCommand = ""
    Thanks in Advance!

    DC

    Wednesday, March 27, 2019 8:42 PM

All replies

  • I didn't get any error when I ran your script (with appropriate adjustments to work with my file system).

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, March 27, 2019 9:01 PM
  • Hi Rich,

    Thank you for testing and replying back!  I get the error when I run the below line separately.  

    Foreach($folder in $Shares)


    DC

    Wednesday, March 27, 2019 9:06 PM
  • That's understandable. There's no body! Try this:

    Foreach($folder in $Shares){}


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, March 27, 2019 9:31 PM
  • This is how to easily generate code into a file without any pain.

    $sb = {
    Get-WmiObject -class Win32_Share -ComputerName Server51 -Filter "Path LIKE 'E:\\%' AND NOT Name != 'E$'" |
        ForEach-Object{
            Get-SmbShare -Name $_.Name -Path $_.Path -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
        }
    }
    $sb.ast.Extent.Text | Out-File -FilePath E:\SharesScript.ps1

    This turns any block of code into text as it is formatted.  Just write the code and pull the blocks text out to a file.


    \_(ツ)_/






    • Edited by jrv Wednesday, March 27, 2019 10:45 PM
    Wednesday, March 27, 2019 10:38 PM
  • Thank you both for the replies.  I am very new to Powershell so I apologize for my lack knowledge of PS.   I tried jrv's suggestion and it has fixed the foreach loop error.  Now the ps script output is not correct.  Below is what the SharesScript.ps1 output looks like from jrv's script and my original script.

    jrv's script output:

    {
    Get-WmiObject -class Win32_Share -ComputerName Server51 -Filter "Path LIKE 'E:\\%' AND NOT Name != 'E$'" |
        ForEach-Object{
            Get-SmbShare -Name $_.Name -Path $_.Path -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
        }
    }
    

    My original script output:

    Get-SmbShare -Name Folder1 -Path E:\Folder1 -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
    Get-SmbShare -Name Folder2 -Path E:\Folder2 -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
    Get-SmbShare -Name Folder3 -Path E:\Folder3 -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
    Get-SmbShare -Name Folder4 -Path E:\Folder4 -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
    Get-SmbShare -Name Folder5 -Path E:\Folder5 -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'
    Get-SmbShare -Name Folder6 -Path E:\Folder6 -ContinuouslsyAvailable -FullAccess 'MyDomain\domain users'

    Any suggestions on how to resolve?

    Thank you very much again!!  Your assistance is much appreciated!!



    DC

    Thursday, March 28, 2019 2:03 PM