Reputation: 649
I've created a renaming script that includes aliases for variables that I would like to define as parameters when I call the script. If I define the variables in the script the traditional way ($variable = 'defined'
) the script renames the appropriate file correctly. I think I am misunderstanding the param portion.
Here's the script:
#retrieve the name from the text file
$name = Get-Content "C:\name.txt"
#identify the .jpg that needs to be renamed based on naming (Get-Date -Format "yyyy-MM-dd HH-mm-ss")
$fileName = Get-Date -Format "yyyy-MM-dd"
param (
[Parameter(<#mandatory=$true#>)]
[alias("v")]
[string] $variant,
[Parameter(<#mandatory=$true#>)]
[alias("pc")]
[string] $processClass
)
#identify new name set up and identify the file to address (oldname) then rename it
$newname = "D:\Output\$name" + "." + $variant + ".PC_" + $processClass + ".jpg"
$oldName = gci "D:\Output\" -Filter *.jpg | Where-Object {$_.name -like "*$fileName*"}
rename-item $oldName.FullName $newname -Force
Then I will call the script with the parameters applied like this:
powershell.exe -File C:\Rename.ps1 -v "XXX" -pc "YYY"
However at this point, the $oldName
file is only partially renamed, missing the $variant
and $processClass
portion and I get a PowerShell error:
param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
It seems im not properly defining the parameters or I have just misunderstood how this part works, I've not tried this before.
Thanks for any suggestions
Upvotes: 0
Views: 1619
Reputation: 7479
your problem is the placement of code before the Param()
block. [grin] simply move all that pre-Param()
code to after the Param()
block and things will work.
you may also want to consider adding [CmdletBinding()]
just before the Param()
to make it a full "advanced function".
Upvotes: 4