Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell - foreach with if loop

$
0
0

Hello,

I have a little problem with my script. Contain Sharepoint, but my problem is programming related.

I have 3 folder.  So this will run 3 times. After the script examine a folder it drop an error (not the write part in the script) that the folder is exist and go to the next one. Finally find the folder that already exist and go to the write-host "exist" part.

If the Folder not exist then the script run successfully and create it without an error.

So how can i avoid that the script enter to the ELSE part of the loop when the foreach examine a folder?

Add-PSSnapin microsoft.sharepoint.powershell
$spWeb = Get-Spweb http://sptest
$doclib=[Microsoft.SharePoint.SPDocumentLibrary]$spweb.Lists["mydoc"]
$foldercoll=$doclib.Folders;

foreach($folder in $foldercoll)
 {
  Write-Host $folder.Name
    if ($folder.name.equals("MAPPAS"))
    {
    write-host "Exist"
    }
    Else
    {
    $spfolder = $doclib.Additem("",[microsoft.sharepoint.SPFIleSystemObjectType]::Folder,"MAPPAS")
     $spfolder.update()    
    }
}
read-host


I Hate Mondays


Viewing all articles
Browse latest Browse all 15028

Trending Articles



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