Passing URL as parameter input

Welcome Forums General PowerShell Q&A Passing URL as parameter input

This topic contains 8 replies, has 5 voices, and was last updated by

 
Participant
4 months, 1 week ago.

  • Author
    Posts
  • #129518

    Participant
    Points: 50
    Rank: Member

    I have a module for updating a SharePoint list that takes 3 input values.

    • URL
    • List Name
    • CSV Path

    The URL value is used by get-spweb to gain access to the site.   If I set the $URL value manually and then run get-spweb $url in either Windows ps (with sharepoint snapin) or in the SP mgmt shell it returns the spsite.

    If I run the module at update-list -url http://xxxx/xxx/xxx I get an error that 'A parameter cannot be found that matches parameter name 'url'.  I have tried formatting the URL as 'http://xxx...' and "http://xxx.." but receive the same error.

    [CmdletBinding()]
    param (
    
    [Parameter(Mandatory = $True)]
    [string]$URL,
    [string]$ListName,
    [string]$CSVPath
    )
    
    $Tasks = import-csv $CSVPath
    $Web = get-spweb $URL
    $List = $web.lists[$ListName]
    $Date = get-date
    
    

     

    I feel like this has something do to with how the URL is presented to the -URL param but can't figure out what I am missing.

  • #129522

    Participant
    Points: 315
    Helping Hand
    Rank: Contributor

    Your Param() block is incorrectly defined

    Instead of

    param ( 
    
    [Parameter(Mandatory = $True)]
    [string]$URL,
    [string]$ListName,
    [string]$CSVPath
    )
    
    

    use

    param (
    [Parameter(Mandatory = $True)] [string]$URL,
    [Parameter(Mandatory = $True)] [string]$ListName,
    [Parameter(Mandatory = $True)] [string]$CSVPath
    )
    
    
    • #129543

      Participant
      Points: 50
      Rank: Member

      And this is going to resolve the issue of the URL not being found when used with get-spweb?   And actually that is correct if you are only making the first parameter mandatory.  You only have to format it as in your display if you are making multiple parameters mandatory.

       

    • #129603

      Moderator
      Points: 1,976
      Helping Hand
      Rank: Community Hero

      There is no problem with the parameter declarations here.

  • #129557

    Participant
    Points: 659
    Helping Hand
    Rank: Major Contributor

    How are you invoking this file? Generally you'd wrap that in a function Update-List { } declaration so that you can call it by name.

  • #129606

    Moderator
    Points: 1,976
    Helping Hand
    Rank: Community Hero

    @juli , as Joel mentioned, how you are invoking this code is very important here. If you are saving this as a script, then you would invoke it .\script.ps1 -url '...' else as joel mentioned you will invoke it like Update-List -Url '...' .

  • #129630

    Participant
    Points: 639
    Helping Hand
    Rank: Major Contributor

    If I run the module at update-list -url http://xxxx/xxx/xxx I get an error that 'A parameter cannot be found that matches parameter name 'url'. I have tried formatting the URL as 'http://xxx...' and "http://xxx.." but receive the same error.

    There is a Powershell cmdlet Update-List that has nothing to do with SharePoint: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/update-list?view=powershell-5.1

    Is the Update-List a function you are defining somewhere? If the function is in memory, the function should take precedence over the module cmdlet, but I would change the function to something unique if possible (Update-SPList). My guess is that you when you make the call the Update-List, it is using the above reference and there is not URL param, so that is where you are seeing the error.

  • #129636

    Moderator
    Points: 1,976
    Helping Hand
    Rank: Community Hero

    I think @Rob nailed it ! I didn't know about this cmdlet.

    • #129740

      Participant
      Points: 50
      Rank: Member

      Yes it is a Module and I am calling it as update-list.

      Update-List -url http://xxxxxxxxx

      The interesting thing about this is it did work one time and created my items but didn't update one of the columns because I had the wrong internal name.   Once I changed that, it stopped working again.

      I will certainly try changing the function name.   Will report back on the progress.

      Thanks for all the advice.

The topic ‘Passing URL as parameter input’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort