Skip to main content
Glama
DynamicEndpoints

PowerShell Exec MCP Server

generate_script_from_template

Create PowerShell scripts by populating templates with specific parameters for automation tasks in Microsoft Intune and IBM BigFix environments.

Instructions

Generate a PowerShell script from a template.

Args:
    template_name: Name of the template to use (without .ps1 extension)
    parameters: Dictionary of parameters to replace in the template
    output_path: Where to save the generated script (optional)
    timeout: Command timeout in seconds (1-300, default 60)
    
Returns:
    Generated script content or path where script was saved

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
template_nameYes
parametersYes
output_pathNo
timeoutNo

Implementation Reference

  • The primary handler function for the 'generate_script_from_template' MCP tool. It loads a PowerShell template file, substitutes parameters (including auto-adding DATE), optionally writes to output_path, and returns the generated script content or save path. Registered via @mcp.tool() decorator.
    @mcp.tool()
    async def generate_script_from_template(
        template_name: str,
        parameters: Dict[str, Any],
        output_path: Optional[str] = None,
        timeout: Optional[int] = 60
    ) -> str:
        """Generate a PowerShell script from a template.
        
        Args:
            template_name: Name of the template to use (without .ps1 extension)
            parameters: Dictionary of parameters to replace in the template
            output_path: Where to save the generated script (optional)
            timeout: Command timeout in seconds (1-300, default 60)
            
        Returns:
            Generated script content or path where script was saved
        """
        template_path = os.path.join(TEMPLATES_DIR, f"{template_name}.ps1")
        if not os.path.exists(template_path):
            raise ValueError(f"Template {template_name} not found")
            
        with open(template_path, 'r') as f:
            template_content = f.read()
            
        # Replace template variables
        script_content = template_content
        parameters['DATE'] = datetime.now().strftime('%Y-%m-%d')
        
        for key, value in parameters.items():
            script_content = script_content.replace(f"{{{{{key}}}}}", str(value))
            
        if output_path:
            with open(output_path, 'w') as f:
                f.write(script_content)
            return f"Script generated and saved to: {output_path}"
        
        return script_content
  • src/server.py:148-148 (registration)
    MCP tool registration decorator for generate_script_from_template.
    @mcp.tool()
  • Global constant defining the templates directory path, used by generate_script_from_template to locate template files.
    TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'templates')
  • Supporting utility function ensure_directory that ensures output directories exist; indirectly supports generate_script_from_template when output_path is provided in calling tools.
    @mcp.tool()
    def ensure_directory(path: str) -> str:
        """Ensure directory exists and return absolute path."""
        abs_path = normalize_path(path)
        if os.path.splitext(abs_path)[1]:  # If path has an extension
            dir_path = os.path.dirname(abs_path)
        else:
            dir_path = abs_path
        os.makedirs(dir_path, exist_ok=True)
        return abs_path

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/DynamicEndpoints/PowerShell-Exec-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server