Powershell: "Attempted to perform an unauthorized operation" when calling constructor
I am trying to run a script for extracting the best bets from a SharePoint 2007 Environment, and this line in the Script 

    $keywords =  new-object Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI)

Is causing an error. As far as I am aware, this is the correct way to call a constructor on a non-static class. Both variables are not null, and yet this line is causing this error:

    New-Object : Exception calling ".ctor" with "2" argument(s): "Attempted to perform an unauthorized operation."
    At D:\2007 Code Cleanup\ExportKeywords.ps1:79 char:80
    +         [Microsoft.Office.Server.Search.Administration] $keywords =  new-object <<<<      Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI) 
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Here is the whole script:

    $global:configFile = "" 
    $global:siteid="" 
    $global:keywordsfile="" 
    $global:bestbetsfile="" 
    $global:synonymsfile="" 
    $global:proceed = "True" 
 
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search") > $null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
 
    function checkFile($path) 
    { 
    $status = Test-Path $path 
         
    return $status 
    } 
 
    function ProcessConfigFile($configFile) 
    { 
    write-host ("[STATUS] Processing config file : " + $configFile) -ForegroundColor green 
             
    $doc = New-Object System.Xml.XmlDocument  
    $doc.Load($configFile)  
 
    $global:siteid = $doc.config.export_SP2007_CSV.SPsiteURL 
    $global:keywordsfile = $doc.config.export_SP2007_CSV.output_keywordsfile 
    $global:bestbetsfile = $doc.config.export_SP2007_CSV.output_bestbetsfile 
    $global:synonymsfile = $doc.config.export_SP2007_CSV.output_synonymsfile     
     
    write-host ("[STATUS] Using these configurations: `n") -ForegroundColor green 
    write-host ("`t site url to export from : " + $siteid) -ForegroundColor green     
    if(checkFile($keywordsfile))    { write-host ("`t File allready exists, will overwrite : " + $keywordsfile) -ForegroundColor green ; del $keywordsfile }  else { write-host ("`t keywordsfile : " + $keywordsfile) -ForegroundColor green } 
    if(checkFile($bestbetsfile))    { write-host ("`t File allready exists, will overwrite : " + $bestbetsfile) -ForegroundColor green ; del $bestbetsfile }  else { write-host ("`t bestbetsfile : " + $bestbetsfile) -ForegroundColor green } 
    if(checkFile($synonymsfile))    { write-host ("`t File allready exists, will overwrite : " + $synonymsfile) -ForegroundColor green ; del $synonymsfile}  else { write-host ("`t synonymsfile : " + $synonymsfile) -ForegroundColor green } 
     
    $temp = New-item -itemtype file $keywordsfile  -force 
    $temp = New-item -itemtype file $bestbetsfile  -force 
    $temp = New-item -itemtype file $synonymsfile  -force 
    } 
 
 
    write-host ("-----------------------------------------------------------`n Export keywords from SP-2007 --> CSV files ") -ForegroundColor yellow 
    write-host ("`n`n This script will export Sharepoint 2007 `n keywords/bestbets/synonyms in to CSV files.`n-----------------------------------------------------------`n`n") -ForegroundColor yellow 
 
 
    if (!$args) 
    { 
    write-host ("You must specificy command line arguments") -ForegroundColor red 
    write-host ("Try : -? for more info") -ForegroundColor red 
    } 
    else 
    {         
    switch($args) 
    { 
        "-c" {  
                if ($args[1] -ne $null) 
                    {  
                        write-host ("[STATUS] Will use config file : " + $args[1] +"") -ForegroundColor green 
                        $global:configFile = $pwd.path + $args[1] 
                         
                        if (checkFile($global:configFile)) { ProcessConfigFile($global:configFile) } 
                        else { write-host ("[Error ] Config file " + $configFile + " does not exist!`n`n") -ForegroundColor red ; $global:proceed = "False"}                                                 
                    } 
                else  
                    {  
                        write-host ("[Error ] Need to specify the config file...!") -ForegroundColor red ; $global:proceed = "False" 
                    } 
             }         
        "-?" { $help = "`n`n Definition : `n`n This script is used for exporting keywords/bestbets/synonyms into CSV files `n from a Sharepoint 2007. `n`n Example: .\export_SP2007_keywords_to_CSV.ps1 -c .\configuration.xml`n`n" ; write-host $help ; exit } 
    }     
     
    if ($global:proceed -eq "True") 
    { 
        $Thesite = new-object Microsoft.SharePoint.SPSite($global:siteid) 
         
        $searchContext =  [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($Thesite) 
        $myURI = new-object system.URI($global:siteid) 
 
        [Microsoft.Office.Server.Search.Administration] $keywords =  new-object Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI) 
         
        #Add first line descriptors in all output files 
        "Keyword" > $global:keywordsfile 
        "Keyword, Synonyms" > $global:synonymsfile 
        "Keyword, BestBet, Url, Description" > $global:bestbetsfile 
         
        foreach($Keyword in $keywords.AllKeywords) 
        { 
             
            $Keyword.Term >> $global:keywordsfile 
             
            foreach($MySynonym in $Keyword.Synonyms) 
            {                 
                ($keyword.Term + "," + $MySynonym.Term) >> $global:synonymsfile 
            } 
            foreach($MyBestBet in $Keyword.BestBets) 
            {                 
                ($keyword.Term + "," + $MyBestBet.title + "," + $MyBestBet.url + "," + $MyBestBet.description) >> $global:bestbetsfile 
            }                     
        } 
                 
        write-host ("`n[STATUS] keywordsfile output captured ") -ForegroundColor green 
        write-host ("[STATUS] synonyms output captured ") -ForegroundColor green 
        write-host ("[STATUS] keywordsfile output captured ") -ForegroundColor green 
        write-host ("[STATUS] Job complete! `n`n") -ForegroundColor green 
    } 
    }
Powershell: "Attempted to perform an unauthorized operation" when calling constructor
===

I am trying to run a script for extracting the best bets from a SharePoint 2007 Environment, and this line in the Script 

    $keywords =  new-object Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI)

Is causing an error. As far as I am aware, this is the correct way to call a constructor on a non-static class. Both variables are not null, and yet this line is causing this error:

    New-Object : Exception calling ".ctor" with "2" argument(s): "Attempted to perform an unauthorized operation."
    At D:\2007 Code Cleanup\ExportKeywords.ps1:79 char:80
    +         [Microsoft.Office.Server.Search.Administration] $keywords =  new-object <<<<      Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI) 
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Here is the whole script:

    $global:configFile = "" 
    $global:siteid="" 
    $global:keywordsfile="" 
    $global:bestbetsfile="" 
    $global:synonymsfile="" 
    $global:proceed = "True" 
 
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search") > $null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
 
    function checkFile($path) 
    { 
    $status = Test-Path $path 
         
    return $status 
    } 
 
    function ProcessConfigFile($configFile) 
    { 
    write-host ("[STATUS] Processing config file : " + $configFile) -ForegroundColor green 
             
    $doc = New-Object System.Xml.XmlDocument  
    $doc.Load($configFile)  
 
    $global:siteid = $doc.config.export_SP2007_CSV.SPsiteURL 
    $global:keywordsfile = $doc.config.export_SP2007_CSV.output_keywordsfile 
    $global:bestbetsfile = $doc.config.export_SP2007_CSV.output_bestbetsfile 
    $global:synonymsfile = $doc.config.export_SP2007_CSV.output_synonymsfile     
     
    write-host ("[STATUS] Using these configurations: `n") -ForegroundColor green 
    write-host ("`t site url to export from : " + $siteid) -ForegroundColor green     
    if(checkFile($keywordsfile))    { write-host ("`t File allready exists, will overwrite : " + $keywordsfile) -ForegroundColor green ; del $keywordsfile }  else { write-host ("`t keywordsfile : " + $keywordsfile) -ForegroundColor green } 
    if(checkFile($bestbetsfile))    { write-host ("`t File allready exists, will overwrite : " + $bestbetsfile) -ForegroundColor green ; del $bestbetsfile }  else { write-host ("`t bestbetsfile : " + $bestbetsfile) -ForegroundColor green } 
    if(checkFile($synonymsfile))    { write-host ("`t File allready exists, will overwrite : " + $synonymsfile) -ForegroundColor green ; del $synonymsfile}  else { write-host ("`t synonymsfile : " + $synonymsfile) -ForegroundColor green } 
     
    $temp = New-item -itemtype file $keywordsfile  -force 
    $temp = New-item -itemtype file $bestbetsfile  -force 
    $temp = New-item -itemtype file $synonymsfile  -force 
    } 
 
 
    write-host ("-----------------------------------------------------------`n Export keywords from SP-2007 --> CSV files ") -ForegroundColor yellow 
    write-host ("`n`n This script will export Sharepoint 2007 `n keywords/bestbets/synonyms in to CSV files.`n-----------------------------------------------------------`n`n") -ForegroundColor yellow 
 
 
    if (!$args) 
    { 
    write-host ("You must specificy command line arguments") -ForegroundColor red 
    write-host ("Try : -? for more info") -ForegroundColor red 
    } 
    else 
    {         
    switch($args) 
    { 
        "-c" {  
                if ($args[1] -ne $null) 
                    {  
                        write-host ("[STATUS] Will use config file : " + $args[1] +"") -ForegroundColor green 
                        $global:configFile = $pwd.path + $args[1] 
                         
                        if (checkFile($global:configFile)) { ProcessConfigFile($global:configFile) } 
                        else { write-host ("[Error ] Config file " + $configFile + " does not exist!`n`n") -ForegroundColor red ; $global:proceed = "False"}                                                 
                    } 
                else  
                    {  
                        write-host ("[Error ] Need to specify the config file...!") -ForegroundColor red ; $global:proceed = "False" 
                    } 
             }         
        "-?" { $help = "`n`n Definition : `n`n This script is used for exporting keywords/bestbets/synonyms into CSV files `n from a Sharepoint 2007. `n`n Example: .\export_SP2007_keywords_to_CSV.ps1 -c .\configuration.xml`n`n" ; write-host $help ; exit } 
    }     
     
    if ($global:proceed -eq "True") 
    { 
        $Thesite = new-object Microsoft.SharePoint.SPSite($global:siteid) 
         
        $searchContext =  [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($Thesite) 
        $myURI = new-object system.URI($global:siteid) 
 
        [Microsoft.Office.Server.Search.Administration] $keywords =  new-object Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI) 
         
        #Add first line descriptors in all output files 
        "Keyword" > $global:keywordsfile 
        "Keyword, Synonyms" > $global:synonymsfile 
        "Keyword, BestBet, Url, Description" > $global:bestbetsfile 
         
        foreach($Keyword in $keywords.AllKeywords) 
        { 
             
            $Keyword.Term >> $global:keywordsfile 
             
            foreach($MySynonym in $Keyword.Synonyms) 
            {                 
                ($keyword.Term + "," + $MySynonym.Term) >> $global:synonymsfile 
            } 
            foreach($MyBestBet in $Keyword.BestBets) 
            {                 
                ($keyword.Term + "," + $MyBestBet.title + "," + $MyBestBet.url + "," + $MyBestBet.description) >> $global:bestbetsfile 
            }                     
        } 
                 
        write-host ("`n[STATUS] keywordsfile output captured ") -ForegroundColor green 
        write-host ("[STATUS] synonyms output captured ") -ForegroundColor green 
        write-host ("[STATUS] keywordsfile output captured ") -ForegroundColor green 
        write-host ("[STATUS] Job complete! `n`n") -ForegroundColor green 
    } 
    }