Not Able to Use Variables in Place of Function Parameters

Welcome Forums General PowerShell Q&A Not Able to Use Variables in Place of Function Parameters

Viewing 7 reply threads
  • Author
    Posts
    • #176497
      Participant
      Topics: 34
      Replies: 52
      Points: 96
      Rank: Member

      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?

       

    • #176503
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      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:

       

    • #176566
      Participant
      Topics: 34
      Replies: 52
      Points: 96
      Rank: Member

      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:

      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
      )
    • #176638
      Participant
      Topics: 34
      Replies: 52
      Points: 96
      Rank: Member

      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'.

      
      
      		
      	
    • #176656
      Participant
      Topics: 34
      Replies: 52
      Points: 96
      Rank: Member

      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

    • #176893
      Participant
      Topics: 34
      Replies: 52
      Points: 96
      Rank: Member

      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?

    • #176956
      Participant
      Topics: 2
      Replies: 511
      Points: 1,306
      Helping Hand
      Rank: Community Hero

      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
      • #177004
        Participant
        Topics: 34
        Replies: 52
        Points: 96
        Rank: Member

        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."

    • #177013
      Participant
      Topics: 34
      Replies: 52
      Points: 96
      Rank: Member

      Hmm may have just found something. Think I can add to the hash table based on the variable.

      if(

      $aqualnkMember)
      {
      $Switches.aqualnkMember = $true
      }

Viewing 7 reply threads
  • You must be logged in to reply to this topic.