• 记录一个测试用的PowerShell脚本


    今天的工作中, 我需要写一个脚本创建两千零一个SPListItem. 特点如下:

    1. 第1个item和第2001个item的所有field的值除了title外全部相同.

    2. 其中有两个field是Managed Metadata Service里的Term.

    3. 一个field是个类型为Choice.

    脚本写好了, 经测试, 已成功运行.

    这份脚本里包含的知识点有:

    1. 如何使用PowerShell获得SharePoint中的Term. 答案是TaxonomySession->TermStore->Term Group->TermSet->Term.

    2. 如何对类型为Choice的field进行赋值. 答案出奇的简单, 字符串相同即可.

    3. 如何对类型为Managed Metadata Term的field进行赋值. 注意SetFieldValue方法的使用.

    4. 如何在判断语句if中同时使用两个条件进行判断. 注意这一行if(($termSet_Country -eq $null) -or ($termSet_Company -eq $null))

    5. 如何让非SharePoint PowerShell Prompt中可以运行SharePoint的PowerShell Cmdlet.  注意Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

    6. 如何对PowerShell中用到的SPSite这样的比较占资源的对象及时dispose掉. 注意Start-SPAssignment和Stop-SPAssignment.

    7. 如何在PowerShell中进行清屏. 注意Clear-Host.

    Clear-Host
    
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" 
    
    #For object dispose
    $spAssignment = Start-SPAssignment
    
    #Get TermStore
    $taxonomySession = Get-SPTaxonomySession -Site "http://sps2010/"
    $termStore = $taxonomysession.TermStores["Managed Metadata Service Application"]
    $group = $termStore.Groups["My Group"]
    if($group -eq $null) 
    { 
        return
    }
    
    #Get TermSet
    $termSetName_Country = "Country"
    $termSetName_Company = "Company"
    $termSet_Country = $group.TermSets | Where-Object { $_.Name -eq $termSetName_Country }
    $termSet_Company = $group.TermSets | Where-Object { $_.Name -eq $termSetName_Company }
    if(($termSet_Country -eq $null) -or ($termSet_Company -eq $null))
    {           
        return            
    } 
    
    #Get Terms
    $term_aus = $termSet_Country.GetAllTerms() | Where-Object { $_.Name -eq "Australia" }
    $term_chi = $termSet_Country.GetAllTerms() | Where-Object { $_.Name -eq "China" }
    $term_usa = $termSet_Country.GetAllTerms() | Where-Object { $_.Name -eq "USA" }
    
    $term_app = $termSet_Company.GetAllTerms() | Where-Object { $_.Name -eq "Apple" }
    $term_goo = $termSet_Company.GetAllTerms() | Where-Object { $_.Name -eq "Google" }
    $term_mic = $termSet_Company.GetAllTerms() | Where-Object { $_.Name -eq "Microsoft" }
    
    #Get List
    $mylist = (Get-SPWeb -identity http://sps2010 -AssignmentCollection $spAssignment).Lists["Term3"]
    
    #Create 1st ListItem
    $newItem1 = $mylist.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
    $newItem1["Title"] = "Item 1"
    $newItem1["Activity"] = "Jump"
    $taxField1 = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem1.Fields["Country"]
    $taxField1.SetFieldValue($newItem1,$term_chi)
    $taxField2 = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem1.Fields["CompanyM"]
    $taxField2.SetFieldValue($newItem1,$term_goo)
    $newItem1.Update();
    
    
    #Create 2nd ~ 2000th ListItem
    for($l=2; $l -le 2000; $l++)
    {
        $newItem = $mylist.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
        $newItem["Title"] = "Item $l"
        $newItem["Activity"] = "Run"
        $taxField3 = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["Country"]
        $taxField3.SetFieldValue($newItem,$term_aus)
        $taxField4 = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["CompanyM"]
        $taxField4.SetFieldValue($newItem,$term_app)
        $newItem.Update()
    }
    
    #Create 2001st List Item
    $newItem2001 = $mylist.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
    $newItem2001["Title"] = "Item 2001"
    $newItem2001["Activity"] = "Jump"
    $taxField5 = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem1.Fields["Country"]
    $taxField5.SetFieldValue($newItem2001,$term_chi)
    $taxField6 = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem1.Fields["CompanyM"]
    $taxField6.SetFieldValue($newItem2001,$term_goo)
    $newItem2001.Update();
    
    Stop-SPAssignment $spAssignment

    运行结果

    image

    image

    另外由于测试代码的时候会试图删除已经创建出来的所有的list item. 但是尝试删除list item的动作非常慢. 最后干脆整个列表都删掉了重来, 这样还会更快些.

    参考资料

    ==================================
    Updating SharePoint Managed Metadata Columns with PowerShell

    http://blogs.msdn.com/b/kaevans/archive/2012/02/15/updating-sharepoint-managed-metadata-columns-with-powershell.aspx

    Using the Write-Host Cmdlet

    http://technet.microsoft.com/library/ee177031.aspx

    Start-SPAssignment

    http://technet.microsoft.com/en-us/library/ff607664

    PowerShell script to delete all items in a List

    http://blogs.msdn.com/b/spses/archive/2011/11/10/powershell-script-to-delete-all-items-in-a-list.aspx

  • 相关阅读:
    .NET 操作XML
    C#常用操作类库五(电脑操作类)
    C#常用操作类库四(File操作类)
    C#常用操作类库一(验证类)
    RabbitMQ 学习
    redis 常用命令
    windows10配置redis一主二从三哨兵
    Oracle递归查询(查询当前记录所有父级或子级)
    echarts 中 柱图 、折线图、柱图层叠
    简单的前台登录
  • 原文地址:https://www.cnblogs.com/awpatp/p/2593796.html
Copyright © 2020-2023  润新知