PowerShell GUI - Get-Service

This topic contains 9 replies, has 5 voices, and was last updated by Profile photo of Dan Potter Dan Potter 6 months ago.

  • Author
    Posts
  • #42063
    Profile photo of Kris
    Kris
    Participant

    I am in the process of creating a GUI tool for pulling remote server services to manage them (Start/Stop/Restart). But getting "cannot open service control manager on computer "server name" – is there any other way ?

  • #42076
    Profile photo of Tony Pagliaro
    Tony Pagliaro
    Participant

    Can you view those same servers' remote services from the MMC?
    Also can you share a snippet of your code?

  • #42149
    Profile photo of Kris
    Kris
    Participant

    Default server name is localhost and even that is not working.

    ****CODE*****
    $SvcsBtnConnectList_Click={
    $varServerName = $SvcsTxtBoxServerNameList
    try
    {
    $SvcsList = Get-service -ComputerName $varServerName | where { $_.DisplayName } | select -Property DisplayName
    $ServicesStatusBar.Text = "Connected to $($varServerName) and List Loaded"
    Load-Combobox -ComboBox $SvcsCmbBoxSelectService -Items $SvcsList
    }
    catch
    {
    $SvcsTxtBoxServerNameError.SetIconAlignment($EvtBtnConnect, 'MiddleLeft')
    $SvcsTxtBoxServerNameError.SetError($EvtBtnConnect, $($_.Exception.Message))
    $ServicesStatusBar.Text = "$($varServerName) : $($_.Exception.Message)"
    }
    }

  • #42152
    Profile photo of Tony Pagliaro
    Tony Pagliaro
    Participant

    If I set $varServerName to 'localhost' and run this line:

    Get-service -ComputerName $varServerName | where { $_.DisplayName } | select -Property DisplayName
    

    ...it works fine. If you just do that one line too, does it work?

    I'm assuming that's the line that's tripping you up. Have you tried using the F9 breakpoint feature in ISE to step through your function?

  • #42154
    Profile photo of Kris
    Kris
    Participant

    In PS ISE, Yes it works. When we do via GUI tool i am building, throws error.

  • #42158
    Profile photo of Jack Neff
    Jack Neff
    Participant

    Complete shot in the dark here, but should this line be?...

    $varServerName = $SvcsTxtBoxServerNameList.Text
    
  • #42171
    Profile photo of Kris
    Kris
    Participant

    Hi,

    Somehow this is still fails for remote computer, I tried two below variations. This failing even in PS ISE

    Get-service -ComputerName vdaldc01v | where { $_.DisplayName } | select -Property DisplayName
    Error:
    Get-service : Cannot open Service Control Manager on computer 'vdaldc01v'. This operation might require other privileges.
    At line:1 char:1
    + Get-service -ComputerName vdaldc01v | where { $_.DisplayName } | select -Propert ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-Service], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand

    ***************************************************************
    Get-WmiObject -Class WIN32_service -ComputerName vdaldc01v
    Error :
    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At line:1 char:1
    + Get-WmiObject -Class WIN32_service -ComputerName vdaldc01v
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    1. Running with Domain admin privileges in my test lab
    2. I saw few posts for similar issue, which suggest enable RPC/DCOM manually or Read permissions. For WinRM is disabled in our PRD
    enviroment. So, want to check is there any real alternative to gather remote services without any modification.

    Thanks
    Kris

    • #42198
      Profile photo of Mystic pl
      Mystic pl
      Participant

      Hello
      Have you tried Invoke-Command?

      Invoke-Command -ComputerName "pc" -ScriptBlock  { Get-Service bits | Stop-Service -Force}
      

      Edit:
      Just saw WinRM is disabled in Prod, so it's not an alternative for you.

      • This reply was modified 6 months ago by Profile photo of Mystic pl Mystic pl.
  • #42246
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Do your remote machines require credentials? You should really post your psf in the sapien forums.

    $services = (Get-WmiObject -Class WIN32_service -ComputerName " -Credential $cred).displayname
    Load-ComboBox $combobox1 -Items $services

  • #42253
    Profile photo of Dan Potter
    Dan Potter
    Participant
    #------------------------------------------------------------------------
    # Source File Information (DO NOT MODIFY)
    # Source ID: 7480120c-0d76-4c46-b0b7-4b89203b9b91
    
    #------------------------------------------------------------------------
    
    
    #----------------------------------------------
    #region Application Functions
    #----------------------------------------------
    
    #endregion Application Functions
    
    #----------------------------------------------
    # Generated Form Function
    #----------------------------------------------
    function Call-services_psf {
    
    	#----------------------------------------------
    	#region Import the Assemblies
    	#----------------------------------------------
    	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    	[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    	[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    	#endregion Import Assemblies
    
    	#----------------------------------------------
    	#region Generated Form Objects
    	#----------------------------------------------
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$textbox1 = New-Object 'System.Windows.Forms.TextBox'
    	$buttonConnect = New-Object 'System.Windows.Forms.Button'
    	$combobox1 = New-Object 'System.Windows.Forms.ComboBox'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	$form1_Load = {
    		
    		$script:cred = Get-Credential
    		
    	}
    	
    	#region Control Helper Functions
    	function Load-ComboBox 
    	{
    	
    		Param (
    			[ValidateNotNull()]
    			[Parameter(Mandatory=$true)]
    			[System.Windows.Forms.ComboBox]$ComboBox,
    			[ValidateNotNull()]
    			[Parameter(Mandatory=$true)]
    			$Items,
    		    [Parameter(Mandatory=$false)]
    			[string]$DisplayMember,
    			[switch]$Append
    		)
    		
    		if(-not $Append)
    		{
    			$ComboBox.Items.Clear()	
    		}
    		
    		if($Items -is [Object[]])
    		{
    			$ComboBox.Items.AddRange($Items)
    		}
    		elseif ($Items -is [Array])
    		{
    			$ComboBox.BeginUpdate()
    			foreach($obj in $Items)
    			{
    				$ComboBox.Items.Add($obj)	
    			}
    			$ComboBox.EndUpdate()
    		}
    		else
    		{
    			$ComboBox.Items.Add($Items)	
    		}
    	
    		$ComboBox.DisplayMember = $DisplayMember	
    	}
    	#endregion
    	
    	$buttonConnect_Click={
    		
    		$services = (Get-WmiObject -Class WIN32_service -ComputerName $textbox1.text -Credential $cred).displayname
    		Load-ComboBox $combobox1 -Items $services
    		$combobox1.DroppedDown = $true
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$buttonConnect.remove_Click($buttonConnect_Click)
    			$form1.remove_Load($form1_Load)
    			$form1.remove_Load($Form_StateCorrection_Load)
    			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	$form1.SuspendLayout()
    	#
    	# form1
    	#
    	$form1.Controls.Add($textbox1)
    	$form1.Controls.Add($buttonConnect)
    	$form1.Controls.Add($combobox1)
    	$form1.ClientSize = '343, 123'
    	$form1.Name = 'form1'
    	$form1.Text = 'Form'
    	$form1.add_Load($form1_Load)
    	#
    	# textbox1
    	#
    	$textbox1.Location = '13, 29'
    	$textbox1.Name = 'textbox1'
    	$textbox1.Size = '132, 20'
    	$textbox1.TabIndex = 2
    	#
    	# buttonConnect
    	#
    	$buttonConnect.Location = '166, 27'
    	$buttonConnect.Name = 'buttonConnect'
    	$buttonConnect.Size = '75, 23'
    	$buttonConnect.TabIndex = 1
    	$buttonConnect.Text = 'Connect'
    	$buttonConnect.UseVisualStyleBackColor = $True
    	$buttonConnect.add_Click($buttonConnect_Click)
    	#
    	# combobox1
    	#
    	$combobox1.FormattingEnabled = $True
    	$combobox1.Location = '12, 75'
    	$combobox1.Name = 'combobox1'
    	$combobox1.Size = '314, 21'
    	$combobox1.TabIndex = 0
    	$form1.ResumeLayout()
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $form1.ShowDialog()
    
    } #End Function
    
    #Call the form
    Call-services_psf | Out-Null
    
    

You must be logged in to reply to this topic.