PowerShell Examples

This component provides a short overview / example on how to execute a PowerShell script, and then wait on the results. Once returned, the results are loaded into simple DOM elements for display.

This example executed the Get-Service PowerShell cmdlet, and returned the following information:
Display Name: {{serviceDefinition.displayName}}
Machine Name: {{serviceDefinition.machineName}}
Service Handle: {{serviceDefinition.serviceHandle}}
Service Type: {{serviceDefinition.serviceType}}
Start Type: {{serviceDefinition.startType}}
Status: {{serviceDefinition.status}}
When setting up a PowerShell component, it's important to create and store the Session object inside the component if there are going to be multiple calls. A PowerShell session takes ~1 second to initialize before any scripts can be executed.
                
    public ngOnInit() {{ '{' }}
        this.displayCodeButtonContent = this.strings.HelloWorld.showCode;
        this.psSession = this.appContextService.powerShell.createSession(this.appContextService.activeConnection.nodeName);
        this.getServices();
    {{ '}' }}
                
            

Once the session is created, the service is invoked, and we subscribe to the results:
                
    /*
    //  The Get Services call on the "hello service" initiates a PowerShell session executes
    */
    private getServices() {{ '{' }}
        this.serviceSubscription = this.helloService.getService(this.psSession, 'winrm').subscribe(
            (service: any) => {{ '{' }}
                this.loading = false;
                if (service) {{ '{' }}
                    this.serviceDisplayName = service.displayName;
                    this.serviceDefinition = service;
                {{ '}' }} else {{ '{' }}
                    this.serviceDisplayName = this.strings.HelloWorld.notFound;
                {{ '}' }}
            },
            (error: AjaxError) => {{ '{' }}
                this.errorMessage = Net.getErrorMessage(error);
                this.loading = false;
            {{ '}' }}
        );
    {{ '}' }}