average joe
average joe

Reputation: 319

Powershell functions executed regardless of being called

I'm having trouble understanding how Powershell treats functions. In the following script all functions are called, even if I never actually call the main function. Does powershell not have a concept of call chain?

param([string]$directory)

[string]$global:sqlscript;
$global:types = @{
"double" = "DOUBLE PRECISION"; 
"int" = "INTEGER"; 
"QString" = "INTEGER";
"Ignored" = "1";
"Normal" = "2";
"Critical" = "3" }

function resultToSql($element)
{
  $global:sqlscript += ('"')
  $global:sqlscript += ($element.name + '" ')
  $global:sqlscript += ($global:types.Get_Item($element.type))
  $global:sqlscript += (',' + [Environment]::Newline)
  $global:sqlscript += ('"' + $element.name + "_metric_group" + " " + $global:types.Get_Item($element.metric_group.type))   
  $global:sqlscript += (',' + [Environment]::Newline)
}

function xmlToSql($source)
{
  Write-Host "Parsing...";
  $global:sqlscript += "CREATE TABLE IF NOT EXISTS " + '"' + $source.spec.task.ToLower() + '"'+ [Environment]::NewLine
  $global:sqlscript += '"' + "id" + '"' + " SERIAL NOT NULL" + [Environment]::NewLine

  foreach ($node in $source.spec.measure) {
      resultToSql $node
  }

  foreach ($m in $source.spec.error) {
    resultToSql $m
  }

  $global:sqlscript += '"' + "weighted_sum" + '" ' + $global:types.Get_Item("double") + [Environment]::Newline;
}

function main
{
  if ($directory -eq $null) { exit }
  else
  {
    $xmlfiles = Get-ChildItem -Path $directory -include *Spec.xml
    foreach ($xmlfile in $xmlfiles)
    {
        Write-Host "Filename:" $xmlfile;
        [xml]$spec = Get-Content $file;
        xmlToSql $spec; 
        Write-Host $script;
    }
  }
}

Upvotes: 1

Views: 1559

Answers (3)

user983965
user983965

Reputation: 1121

Remove the main function container so it resembles the code below:

  if ($directory -eq $null) { exit }
  else
  {
    $xmlfiles = Get-ChildItem -Path $directory -include *Spec.xml
    foreach ($xmlfile in $xmlfiles)
    {
        Write-Host "Filename:" $xmlfile;
        [xml]$spec = Get-Content $file;
        xmlToSql $spec; 
        Write-Host $script;
    }
  }

Powershell doesn't execute from Main like C#/C++. It executes what statements are first received outside functions. In this case above it will execute the if statement first as it appears outside the function box.

Upvotes: 0

Keith Hill
Keith Hill

Reputation: 201672

Unlike a C/C++/C# program, "you" need to call the Main function - at the bottom of this script. When you run the script above all it does is create the functions you've defined. It doesn't run any of them. You have to do that by calling them in the script and one of those calls has to be at the script level (outside any functions).

Upvotes: 1

user1462199
user1462199

Reputation:

PowerShell cant magically detect changes to scripts, close the ISE and re-open it then run your script again. If that fails take the contents of your script paste it in the ISE and click the execute button, i just did that and main didnt run.

Upvotes: 3

Related Questions