Quantcast
Channel: Group Policy forum
Viewing all articles
Browse latest Browse all 19997

Stopping a Windows service using Powershell

$
0
0

Hi,

I have written a powershell script to run via group policy.  It checks to see if the computer is a laptop or desktop using WMI, then attempts to stop a Windows service and then updates a file with the computername, but unfortunately I can't get it to run.

I have other powershell scripts running in the same domain which makes me think its a permisisons thing.  The script is here does anyone have any other ideas or other ways to stop the service, thanks

Function Get-Laptop
{
 Param(
 [string]$computer = "localhost"
 )
 $isLaptop = $false
 if(Get-WmiObject -Class win32_systemenclosure -ComputerName $computer |
    Where-Object { $_.chassistypes -eq 9 -or $_.chassistypes -eq 10 `
    -or $_.chassistypes -eq 14})
   { $isLaptop = $true }
 if(Get-WmiObject -Class win32_battery -ComputerName $computer)
   { $isLaptop = $true }
 $isLaptop
} # end function Get-Laptop

# *** entry point to script ***

#Log
function log ($machinetype) {
    #Get computername
    $Computername = $env:computername
    #Get logged on user
    $Username = $env:username

    #Update log ($machinetype)
    
    date | out-file -append '\\fs1\eclipse\Thinking.txt'
    $machinetype | out-file -append '\\fs1\eclipse\Thinking.txt'
    $computername | out-file -append '\\fs1\eclipse\Thinking.txt'
    $Username | out-file -append -filepath '\\fs1\eclipse\Thinking.txt'
    "--------------------------------------------------------------------------" | out-file -append '\\fs1\eclipse

\ThinkingSafe.txt'
    (gc "\\qav-fs1\eclipse\ThinkingSafe.txt") | ? {$_.trim() -ne "" } | set-content "\\fs1\eclipse\Thinking.txt"
  }

Function disableservice {

    #check if service is installed
    $status = Get-WMIObject win32_service -filter "name='TSMonitor'" | select name
    If ( -not $status )
    {}
    else {stop-service 'TSMonitor'; set-service -Name 'TSMonitor'-startupType disabled ; log $desktop}

}

$laptop = "laptop"
$desktop = "desktop"
If(get-Laptop) { log $laptop }
else {disableservice}

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step.")


Alter De Ruine


Viewing all articles
Browse latest Browse all 19997

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>