Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Powershell Script for adding CT fails

$
0
0

#FÜR ALLE WEBS aller SITECOL und Subweb


 


$WEB_APPLICATION_URL

="http://aktiv/"


$lookForCT

="Arbeitsbereich Excel"  #Excel  or Powerpoint


$lookForList

="Dokumente"




$webApplication

=Get-SPWebApplication-identity$WEB_APPLICATION_URL


#Get site object and

#specify name of the content type to look for in each library


 

 

 


ForEach

($siteCollectionin$webApplication.Sites)

{

  

  

#Walk through each site in the site collection


  

ForEach($subSitein$siteCollection.AllWebs)

      {

       

write-host"Checking site:"$subSite.URL

       

   

    

$update=$false


   

   

#Go through each document library in the site


   

$subSite.Lists|where{$_.Title -eq$lookForList-and$subSite.URL -match'sc002'}|ForEach-Object{

       

       

write-host"Checking list:"$_.Title

       

$_.ContentTypesEnabled=$true


       

$_.Update()

       

      

       

       

#Check to see if the library contains the content type specified


       

#at the start of the script


       

if(($_.ContentTypes|where{$_.Name-eq$lookForCT})-eq$null)

        {

           

write-host"No content type exists with the name"$lookForCT"on list"$_.Title

           

$update=$true;

           

        }

       

else


        {

           

write-host"YES content type exists with the name"$lookForCT"on list"$_.Title

           

        }

       

if($update) {

           

#Add site content types to the list


           

$ctToAdd=$subSite.RootWeb.ContentTypes[$lookForCT]


           

write-host"CT = "$ctToAdd.Name

           

#$ct = $_.ContentTypes.Add($ctToAdd)


           

$ct=$_.Lists[$lookForList].ContentTypes.Add($ctToAdd)

           

write-host"Content type"$ct.Name"added to site "$subSite.URL

           

#$_.Update()


    }

  }

}

}


#Dispose of the site object


$site

.Dispose()

-------------------------------------

Cannot index into a null array.    =====================  WHY?

At line:52 char:13

+             $ctToAdd = $subSite.RootWeb.ContentTypes[$lookForCT]

+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArray


HC


Viewing all articles
Browse latest Browse all 2314