Passing URL as parameter input

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.

[pre][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

[/pre]

 

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.

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
)

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.

 

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.

There is no problem with the parameter declarations here.

@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 ‘…’ .

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.

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

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.