Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config Parameters not loading #147

Closed
mgeorgebrown89 opened this issue Feb 12, 2019 · 6 comments
Closed

Config Parameters not loading #147

mgeorgebrown89 opened this issue Feb 12, 2019 · 6 comments

Comments

@mgeorgebrown89
Copy link

Parameters aren't being pulled from config. Maybe I've set it up incorrectly.

In my plugin:

[CmdletBinding()]
[PoshBot.BotCommand(
    Aliases = ('nfHeader'),
    Permissions = 'update'
)]
param(
[PoshBot.FromConfig('nfUsername')]
$nfusername,
[PoshBot.FromConfig('nfPassword')]
$nfPassword
)

Those two variables are then used to make API calls, but it's not working. This is my config file:

  PluginConfiguration = @{
    vectoria = @{
      nfUsername = "xxxxxxxx"
      nfPassword = "xxxxxxxxx"
   }
  }

When i hard code the values above, it works fine. I'm not sure what's going on here. I've tried a few different things like making the parameter mandatory, but its not working.

Context: This function is called by another function. It creates the header for use with other API calls. My intention is to call it from another function, so this function itself isn't exported to the plugin. Could that be the issue? ###

@devblackops
Copy link
Member

@mgeorgebrown89 PoshBot will only automatically supply those configuration-provided parameters to the command that was executed. It doesn't transfer to any other functions you call. In your primary command, define those parameters and then pass them to your other function.

@mgeorgebrown89
Copy link
Author

@devblackops Do the parameters need the mandatory option?

@mgeorgebrown89
Copy link
Author

mgeorgebrown89 commented Feb 13, 2019

I'm still having issues. I must be missing something.

config.psd1

@{
  ApprovalConfiguration = @{
    Commands = @()
    ExpireMinutes = 30
  }
  DisallowDMs = $False
  ModuleManifestsToLoad = @()
  LogDirectory = 'C:\poshbot\logs'
  Name = 'vectoria'
  BotAdmins = @('michael.g.brown')
  AlternateCommandPrefixSeperators = @(':',',',';')
  CommandHistoryMaxLogSizeMB = 10
  MiddlewareConfiguration = @{
    PreReceive = @()
    PostReceive = @()
    PreExecute = @()
    PostResponse = @()
    PreResponse = @()
    PostExecute = @()
  }
  FormatEnumerationLimitOverride = -1
  LogLevel = 'Verbose'
  SendCommandResponseToPrivate = @()
  ChannelRules = @{
    IncludeCommands = @('*')
    Channel = '*'
    ExcludeCommands = @()
  }
  ConfigurationDirectory = 'C:\poshbot'
  MaxLogsToKeep = 5
  AddCommandReactions = $True
  CommandHistoryMaxLogsToKeep = 5
  MaxLogSizeMB = 10
  PluginDirectory = 'C:\poshbot\plugins'
  MuteUnknownCommand = $False
  PluginConfiguration = @{
    vectoria = @{
        nfUsername = 'username'
        nfPassword = 'password'
    }
  }
  AlternateCommandPrefixes = @('VEC','Vec','vec')
  CommandPrefix = '!'
  BackendConfiguration = @{
    Token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    Name = 'SlackBackend'
  }
  PluginRepository = @('PSGallery')
  LogCommandHistory = $True
}

And the primary command:

function Get-NfClouds {
    [CmdletBinding()]
    param(
        [PoshBot.FromConfig('nfUsername')]
        [parameter(mandatory)]
        $Username,
        [PoshBot.FromConfig('nfPassword')]
        [parameter(mandatory)]
        $Password
     )
    $session = Get-NfAPISession -nfusername $Username -nfPassword $Password
    $Clouds_Uri = $session.api + "/clouds"
    $Clouds = Invoke-RestMethod -Uri $Clouds_Uri -Method Get -Headers $session.headers - 

ContentType "application/json"
#Write-Output $Clouds.Name
return $Clouds
}

I pass the username and password to construct the URIs and session headers, but I get this error in slack:

CommandRequirementsNotMet: Mandatory parameters for [Get-NfClouds] not provided.
Usage:
    syntaxitem
    ----------
    {@{name=get-nfclouds; commonparameters=true; workflowcommonparameters=false;...

When I hard-code the values into those parameters, everything works as expected, so for some reason it's not pulling the values from the config file....

Edit: Something else I just thought of for context: I'm using Export-ModuleMember to choose which functions are actually exported, rather than determining this in the config file. I saw @RamblingCookieMonster do this on his blog and for some reason I don't remember I went this way. Perhaps this is a problem?

Edit2: Even just copying and pasting the Get-foo example and trying to print the parameter from the config as ouput gives me the same error as above.

@devblackops
Copy link
Member

@mgeorgebrown89 I'll take your config and command and see if I can repro. Is your plugin named vectoria?

Your plugin configuration has the following:

PluginConfiguration = @{
    vectoria = @{
      nfUsername = "xxxxxxxx"
      nfPassword = "xxxxxxxxx"
   }
}

Is your plugin that contains the Get-NfClouds commands called vectoria? Those names must match.

@mgeorgebrown89
Copy link
Author

So I think I'm a moron... Anytime I edited the config file, I was updating the plugin and testing and I kept getting the same results, but obviously that would be the case because that doesn't update the configuration the bot is running off of. Ugh. So I would make a change, and sometimes I would take effect, and other times not, so I would get varying results from doing the same thing.

Long story short, I fixed it. @devblackops

@devblackops
Copy link
Member

Glad to hear it @mgeorgebrown89 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants