Skip to main content
Glama
DynamicEndpoints

PowerShell Exec MCP Server

get_system_info

Retrieve system information from Windows computers using PowerShell. Specify which ComputerInfo properties to collect and set timeout parameters for enterprise automation and monitoring.

Instructions

Get system information.

Args:
    properties: List of ComputerInfo properties to retrieve (optional)
    timeout: Command timeout in seconds (1-300, default 60)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
propertiesNo
timeoutNo

Implementation Reference

  • The core handler function for the 'get_system_info' tool. It constructs a PowerShell 'Get-ComputerInfo' command, optionally filters by specified properties, ensures JSON output formatting, and executes it via the shared execute_powershell helper.
    @mcp.tool()
    async def get_system_info(properties: Optional[List[str]] = None, timeout: Optional[int] = 60) -> str:
        """Get system information.
        
        Args:
            properties: List of ComputerInfo properties to retrieve (optional)
            timeout: Command timeout in seconds (1-300, default 60)
        """
        code = "Get-ComputerInfo"
        if properties:
            properties_str = ",".join(properties)
            code = f"{code} -Property {properties_str}"
        return await execute_powershell(format_json_output(code), timeout)
  • Shared utility function that securely executes arbitrary PowerShell code with timeout protection, dangerous command validation, and optional MCP context logging. Called by get_system_info to run the system info query.
    async def execute_powershell(code: str, timeout: Optional[int] = 60, ctx: Optional[Context] = None) -> str:
        """Execute PowerShell commands securely.
        
        Args:
            code: PowerShell code to execute
            timeout: Command timeout in seconds (1-300, default 60)
            ctx: MCP context for logging and progress reporting
        
        Returns:
            Command output as string
        """
        # Validate timeout
        if not isinstance(timeout, int) or timeout < 1 or timeout > 300:
            raise ValueError("timeout must be between 1 and 300 seconds")
            
        # Validate code
        if not validate_powershell_code(code):
            raise ValueError("PowerShell code contains potentially dangerous commands")
    
        if ctx:
            await ctx.info("Validating PowerShell code...")
    
        # Create and run process
        if ctx:
            await ctx.info("Starting PowerShell process...")
        
        process = await asyncio.create_subprocess_exec(
            "powershell",
            "-NoProfile",      # Don't load profiles
            "-NonInteractive", # No interactive prompts
            "-Command",
            code,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
    
        try:
            if ctx:
                await ctx.info("Executing command...")
            stdout, stderr = await asyncio.wait_for(
                process.communicate(),
                timeout=timeout
            )
        except asyncio.TimeoutError:
            process.kill()
            if ctx:
                await ctx.error(f"Command timed out after {timeout} seconds")
            raise TimeoutError(f"Command timed out after {timeout} seconds")
    
        if process.returncode != 0:
            error_msg = stderr.decode() if stderr else "Command failed with no error output"
            if ctx:
                await ctx.error(f"PowerShell command failed: {error_msg}")
            raise RuntimeError(error_msg)
        
        result = stdout.decode() if stdout else ""
        if ctx:
            await ctx.info(f"Command completed successfully, returned {len(result)} characters")
            
        return result
  • Helper function that appends '| ConvertTo-Json' to PowerShell code if missing, ensuring JSON-formatted output. Used by get_system_info before execution.
    def format_json_output(code: str) -> str:
        """Add JSON formatting to PowerShell code if not present."""
        if not code.strip().lower().endswith('| convertto-json'):
            code = f"{code} | ConvertTo-Json"
        return code

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