2017-04-14 56 views
0
$adminUPN="[email protected]" 
$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)    
     } 

} 

下面是我得到 出口-CSV的错误:无法绑定参数参数“InputObject”,因为它是空的。 在C:\ Users \ EMXBG \ Downloads \ Script_AddSiteContent \ Script_AddSiteContent \ ScriptForSiteContentLinkQuickLaunch - Copy.ps1:126 char:29 + $ sitesCollection | export-csv $ sitesFile -notypeinformation + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidData:(:) [导出-CSV],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand得到错误,同时运行PowerShell脚本添加网站内容链接

回答

0

这个错误可能是因为$ sitesCollection是空/空。我看不到任何代码赋予它一个值。

+0

好的,是的,请找到更新后的代码,我无法在quickLaunch变量中获得任何值,但它没有选择快速启动导航对象 –

+0

我仍然没有看到代码的任何部分为' $ sitesCollection' –

+0

是的,你是正确的,它没有在例外,所以没有得到任何错误,将更新, 谢谢 –