Variable is not assigned in the method

Dave Gray 586 Reputation points
2021-04-16T08:40:51.827+00:00

Hello

I have a piece of code that runs fine in Powershell (Azure automation runbook) but when I transplant this into a class method/function I get an error about "Variable is not assigned in the method."

The code returns a DataSet from SQL server

e.g.

 $DS = ($Helper.GetHTML(($Helper.ExcuteSQLProcs("EXEC [dbo].[usp_FirewallRuleCountChanged_Get] @DayAgo = 2")).Tables[0]))  

works fine in inline code but in a method I get an error

[void] SendAlert([string] $To, [string] $Subject, [int] $DaysAgo )  
{  
	$DS = ($Helper.GetHTML(($Helper.ExcuteSQLProcs("EXEC [dbo].[usp_FirewallRuleCountChanged_Get] @DayAgo = 2")).Tables[0]))  
}  

Variable is not assigned in the method.

Any ideas anyone?

Thanks

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
{count} votes

1 answer

Sort by: Most helpful
  1. Elmar 26 Reputation points
    2021-04-16T09:52:10.767+00:00

    You should put your parameters in a param block, like:

     [void] SendAlert
     {
         param ([string] $To, [string] $Subject, [int] $DaysAgo)
    
         $DS = ($Helper.GetHTML(($Helper.ExcuteSQLProcs("EXEC [dbo].[usp_FirewallRuleCountChanged_Get] @DayAgo = 2")).Tables[0]))
     }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.