运行Powershell脚本时出错以添加站点内容链接

时间:2017-04-14 10:55:40

标签: powershell sharepoint-online

$adminUPN="xxxxx@Home500.onmicrosoft.com"
$orgName="xxxxxx"
$userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."

Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential

# Begin the process
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")

#Add SharePoint PowerShell SnapIn if not already added
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) 
{    
    Write-Host "Loading SharePoint Powershell Snapin"    
    Add-PSSnapin "Microsoft.SharePoint.Powershell"  -EA SilentlyContinue
}

CLS

 $StartTime = $(get-date -f F)
 $timeStamp = Get-Date -format "MM_dd_yy_hh_mm"

 #Get Current folder file path
 $invocation = (Get-Variable MyInvocation).Value
 $currentPath = Split-Path $invocation.MyCommand.Path 
 $currentPath = $currentPath + "\"

 #Config File Path
 #$configPath = $currentPath + "Config.xml"
 $configPath = "C:\Users\EMXBG\Downloads\Script_AddSiteContent\Script_AddSiteContent\Config.xml"

#fetching details from config.xml
[xml]$configXML = Get-Content $configPath
 $inputFileName = [string]$configXML.Config.Constants.InputFileName
 $errorFileName = [string]$configXML.Config.Constants.ErrorFileName
 $outFilePath = [string]$configXML.Config.Constants.OutputFileName

 #Source File path containing list of WebApplications in a farm. 
 $webApplFilePath = $currentPath + $inputFileName

 #Output File path of the exported AD Security Groups with Site collection and Group Name details. 
 $sitesFilePath = $currentPath + $outFilePath

 #File path of the file which will capture all the errors while running the script. 
 $errorPath = $currentPath + $errorFileName + $timeStamp + ".csv"

# Creating object to write logging into the error and output file
 $sitesFile = New-Object System.IO.StreamWriter  $sitesFilePath
 $errorfile = New-Object System.IO.StreamWriter $errorPath

# Fetching SharePoint WebApplications list from a CSV file
$CSVData = Import-CSV -path $webApplFilePath          

$sitesFile.WriteLine("SiteCollectionName"+","+"SiteURL")
$errorfile.WriteLine("SiteURL"+"`t"+"ExceptionLevel"+"`t"+"ExceptionMsg");

addSiteContentLink $CSVData

      $sitesFile.Close()
      $errorfile.Close()


# Function to add Site Content link in thes where it does not exists

function addSiteContentLink($CSVData)
{
  try
     {
     $compareText = "Site contents" 
     foreach ($row in $CSVData)  
     {

        $webUrl = $row.webUrl
        #$username = $row.username
        #$password = $row.password

            #Get Web Application and credentials

            #$securePass = ConvertTo-SecureString $password -AsPlainText -Force

        #$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
        #$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass)

        # Get the collection of navigation nodes from the quick launch bar
        #$web = $ctx.Web

        $quickLaunch = $webUrl.Navigation.QuickLaunch


            try
            {
                #Iterate through each iten in Quick launch menu
                foreach($quickLaunch in $web)
                {
                       if ($quickLaunch -contains $compareText)
            {

            Write-Host "Site Content link Exists!"

            }   

            else 
            {
                # Add a new navigation node
                $navNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
                $navNode.AsLastNode = $true
                $navNode.Title = "Site Contents"
                $navNode.Url = $web.Url + "_layouts/15/viewlsts.aspx"
                $navNode.IsExternal = $false

                $ctx.Load($quickLaunchColl.Add($navNode))
                $ctx.ExecuteQuery()
            }
                }

            }
            catch
            {
                Write-Host("Exception at Site Collection Url :" + $currentSite.Url)                    
                $errorfile.WriteLine($currentSite.Url+"`t"+"`t"+$_.Exception.Message)              
            }
        }
         #Export Data to CSV 
         $sitesCollection | export-csv $sitesFile -notypeinformation
         $site.Dispose()

    }

     catch
     {
            Write-Host("Exception at Site Collection Url :" +$currentSite.Url)                    
            $errorfile.WriteLine($currentSite.Url+"`t"+"SiteCollection"+"`t"+$_.Exception.Message)              
      }

}

以下是我得到的错误 Export-Csv:无法将参数绑定到参数'InputObject',因为它为null。     在C:\ Users \ EMXBG \ Downloads \ Script_AddSiteContent \ Script_AddSiteContent \ ScriptForSiteContentLinkQuickLaunch - Copy.ps1:126 char:29     + $ sitesCollection | export-csv $ sitesFile -notypeinformation     + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~         + CategoryInfo:InvalidData :( :) [Export-Csv],ParameterBindingValidationException         + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

1 个答案:

答案 0 :(得分:0)

此错误可能是因为$ sitesCollection为空/ null。我在代码中看不到任何赋予它值的内容。