2017-04-14 55 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

是的,你是正確的,它沒有在例外,所以沒有得到任何錯誤,將更新, 謝謝 –