Welcome › Forums › General PowerShell Q&A › Not Able to Use Variables in Place of Function Parameters
- This topic has 8 replies, 3 voices, and was last updated 2 months, 4 weeks ago by
Participant.
-
AuthorPosts
-
-
September 12, 2019 at 4:34 pm #176497
Would appreciate some help on this... I'm not able to run this function when passing variables to it from earlier in the same script where I am calling the function. It's basically giving the error "Update-SharePoint : A positional parameter cannot be found that accepts argument '-aqualnkMember'." I have even tried adding "[CmdletBinding(PositionalBinding=$false)]" to the top of the function, but that didn't help. I think it has something to do with using variables for the parameters of the function I'm calling.
Here is what I'm running that is working successfully:
Update-SharePoint -Computer xts028 -include_debug -aqualnkMember -MigrationDate '2019-09-09T12:00:00Z' -IPAddress '10.50.5.235' -OSVersion 'Windows 7 Professional Service Pack 1'
Here is what I'm running that is failing:
Update-SharePoint -Computer $Computer -include_debug $aqualnkMember $MigrationDate $IPAddress $OSVersion"
When I run "Write-Host" with the above code, it comes out exactly the same output as the code that works:
###INPUT: Write-Host "Update-SharePoint -Computer $Computer -include_debug $aqualnkMember $MigrationDate $IPAddress $OSVersion" ###OUTPUT: Update-SharePoint -Computer xts086 -include_debug -aqualnkMember -MigrationDate '08-22-2019T12:00:00Z' -IPAddress '10.50.5.235' -OSVersion 'Windows 10 Pro'
Why is it not working when I use variables when the output appears to look the same as the version that works when typing everything out?
-
September 12, 2019 at 4:46 pm #176503
We need to see how you have written your function "Update-SharePoint" but i am assuming that you need to know about Positional Parameters in PowerShell that will help you to resolve your problem.When using positional parameters, the syntax can be more difficult to read especially with there are many parameters.Positional parameters are created this way:
1234567891011function Get-Something{Param([Parameter(Position=0)] # Positional parameter[string[]]$Path,[Parameter(Position=1)] # Positional parameter[string]$Destination)} -
September 12, 2019 at 7:13 pm #176566
We need to see how you have written your function "Update-SharePoint" but i am assuming that you need to know about Positional Parameters in PowerShell that will help you to resolve your problem.When using positional parameters, the syntax can be more difficult to read especially with there are many parameters.Positional parameters are created this way:
1234567891011function Get-Something{Param([Parameter(Position=0)] # Positional parameter[string[]]$Path,[Parameter(Position=1)] # Positional parameter[string]$Destination)}I'm not using position parameters in the function. I'm curious why the one line I mentioned is working when the other isn't when they are technically identical. This is the param section of the function:
function Update-SharePoint { [CmdletBinding(PositionalBinding=$false)] Param( [switch] $include_debug, [Parameter(Mandatory=$False)][string] $Computer, [Parameter(Mandatory=$False)][string] $MigrationDate, #format = '2019-09-08T12:00:00Z' [Parameter(Mandatory=$False)][string] $OSVersion, [Parameter(Mandatory=$False)][string] $IPAddress, #[Parameter(Mandatory=$False)][string] $Status, [Parameter(Mandatory=$false)][string] $MACAddress, [switch] $aqualnkMember = $false, [switch] $acntAudtNotif = $false, [switch] $smartCardGPO = $false, [switch] $systemCertGPO = $false, [switch] $systemTrackGPO = $false, [switch] $systemAccessGPO = $false, [switch] $cyberArkAdmin = $false, [switch] $smartCardUse = $false, [switch] $tsl = $false, [switch] $privlegedAccess = $false, [switch] $specialUser = $false, [switch] $sccm = $false, [switch] $fireEye = $false, [switch] $carbonBlack = $false, [switch] $autoLogon = $false )
-
September 13, 2019 at 12:54 am #176638
This won't even work:
Update-SharePoint -Computer $ComputerName -include_debug $aqualnkMember
But this does:
Update-SharePoint -Computer $ComputerName -include_debug -aqualnkMember
$aqualnkMember is just a string that equals "-aqualnkMember." Both lines should be indentical, right? Still getting: Update-SharePoint : A positional parameter cannot be found that accepts argument '-aqualnkMember'.
-
September 13, 2019 at 2:05 am #176656
I'm basically running a script to gather various information and then want to pass that information into this "Update-SharePoint" function so that a SharePoint page is updated based on what is collected in this initial script. If anyone has a better way of somehow passing this information into the "Update-SharePoint" function, I'm all ears. Thanks
-
September 13, 2019 at 5:29 pm #176893
checks ACLs. The owner of the user object we are having issues with is "domain admins." The ones that we are not having issues with are not domain admins. I'm assuming we cannot change the owner of an object unless a domain admin does it, so that is out of the questions. Is there an easier way to grant a computer object full control over a user object?
-
September 13, 2019 at 7:54 pm #176956
You can't provide a switch parameter by passing a string value containing its name.
Instead, you'd need to splat the additional argument(s) you want to pass into the function via a hashtable:
$Switches = @{ aqualnkMember = $true include_debug = $true } Update-SharePoint -Computer $ComputerName @Switches
-
September 14, 2019 at 1:25 am #177004
While, that works when you specify the strings, I need to pass variables from the script that I'm running into this "update-sharepoint" function. This does not appear to work:
$Switches = @{ $aqualnkMember include_debug = $true } Update-SharePoint -Computer $ComputerName @Switches
I'm not running this "update-sharepoint" function ad-hoc, I need to be able to pass variables to this function based on whatever the initial script finds so that it can update SharePoint with the proper information. The contents of "$aqualnkMember", along with several other variables, might be either "-aqualnkMember=$true" or "-aqualnkMember=$false."
-
-
September 14, 2019 at 1:50 am #177013
Hmm may have just found something. Think I can add to the hash table based on the variable.
if(
$aqualnkMember)
{
$Switches.aqualnkMember = $true
}
-
-
AuthorPosts
- You must be logged in to reply to this topic.