hi i have found this script but not running<\/p>\n
Function Set-RemoteService\n{\n<#\n\t.SYNOPSIS\n\t\tSets the state and Start Mode for a Service on Remote Machine\n\n\t.DESCRIPTION\n\t\tUses Win32_Service to set the state and Start Mode of the Service on Remote Machine.\n Correctly traps all the WMI Return Values and logs them in an Error File\n\n\t.PARAMETER ComputerName\n\t Enter a ComputerName accepts multiple ComputerNames \n\n\t.PARAMETER Name\n\t\tEnter the name of the Service (Will accept WQL Keywords)\n\t\n\t.PARAMETER DisplayName\n\t\tEnter the DisplayName of the target Service (Will accept WQL wildacard)\n\t\n\t.PARAMETER State\n\t\tSpecify the State, the state of the service on the Targetmachine will be changed to this\n Valid Values Are: Running, Stopped\n\t\n\t.PARAMETER startupType\n\t\tSpecify Service Startmode, the service on the target machine will have it's startmode changed to this. \n Valid Values are: Auto, Disabled, and Manual\n\t\n\t.PARAMETER ErrorFile\n\t\tLogs Error in the file specified. \n Default ErrorFile is ServiceError.txt at the Desktop of the User running Script.\n\t\n .EXAMPLE\n On the local machine set the \n \n PS C:\\> Set-RemoteService -Name bits -state Running -startupType Auto\n\n ComputerName ServiceName StartMode State \n ------------ ----------- --------- ----- \n DexterPC2143 BITS Auto Running \n\n\t.EXAMPLE\n In the below example the WMI Return Code 21 (Device is not ready) is returned and that comes up as a warning \n\t\t\n PS C:\\> Set-RemoteService -Name ccmexec -state Running -startupType Auto\n WARNING: DexterPC couldn't change state to Running\n WMI Call Returned : The device is not ready\" \n \n ComputerName ServiceName StartMode State \n ------------ ----------- --------- ----- \n DexClient1 BITS Auto Stopped \n\n .EXAMPLE\n Set the bits service state & startmode on the remote machine , verbose swicth turned on\n\n PS C:\\> Set-RemoteService -ComputerName DexClient2420 -Name bits -state Running -startupType Automatic -Verbose\n VERBOSE: Starting the Function...Checking if ErrorFile exists- C:\\Users\\ddha002\\Desktop\\ServiceError.txt\n VERBOSE: ErrorFile exists & logging time to it - C:\\Users\\ddha002\\Desktop\\ServiceError.txt\n VERBOSE: Checking if DexClient2420 is online\n VERBOSE: DexClient2420 is online\n\n ComputerName ServiceName StartMode State \n ------------ ----------- --------- ----- \n DexClient2420 BITS Auto Running \n VERBOSE: Stopping the Function\n\n .INPUTS\n System.String[]\n\n\t.OUTPUTS\n\t\tSelected.System.Management.ManagementObject[]\n\n\t.NOTES\n\t\t\n Reused Code from : \n \n Author - Jason Morgan\n Script - Set-ServiceStartMode [http://gallery.technet.microsoft.com/Set-ServiceStartMode-18a6e13d]\n Used the DisplayName parameter after seeing this Script \n\n Author - Sitaram Pamarthi\n URL - http://techibee.com/powershell/powershell-find-services-that-failed-to-start-after-server-reboot/2036\n Used Net helpmsg to interpret WMI Return codes after this article\n\n Written by - DexterPOSH\n Blog Url - http://dexterposgh.blogspot.com\n#>\n#requires -version 3.0\n[CmdletBinding(DefaultParameterSetName='Name')]\n[OutputType([PSObject])]\nparam(\n\t[Parameter(Position=0, Mandatory=$false,\n\t\t\t\thelpmessage=\"Enter the ComputerNames to Chec & Fix Services on\",\n\t\t\t\tValueFromPipeline=$true,\n\t\t\t\tValueFromPipelineByPropertyName=$true\n\t\t\t\t)]\n\t#[ValidateScript({try{[system.net.dns]::Resolve(\"$_\")|out-null; return $true} catch { throw \"could not resolve the machine name\"}})]\n\t[String[]]\n\t$ComputerName=$env:COMPUTERNAME,\n\n [Parameter(Mandatory=$true,\n ParameterSetName=\"Name\",\n helpmessage=\"Enter the Service Name (Accepts WQL wildacard)\")]\n [string]$Name,\n\n [Parameter(Mandatory=$True, \n ParameterSetName=\"DisplayName\", \n HelpMessage=\"Enter the DisplayName of the target Service(Accepts WQL wildacard)\" )] \n [String]$DisplayName, \n\n [Parameter(Mandatory=$true,helpmessage=\"Enter the state of the Service to be set\")]\n [ValidateSet(\"Running\",\"Stopped\")]\n [string]$state,\n\n [Parameter(Mandatory=$true,helpmessage=\"Enter the Startup Type of the Service to be set\")]\n [ValidateSet(\"Automatic\",\"Manual\",\"Disabled\")]\n [string]$startupType,\n\n [Parameter(Mandatory=$false,helpmessage=\"Enter the Startup Type of the Service to be set\")]\n [string]$ErrorFile=\"$([System.Environment]::GetFolderPath(\"Desktop\"))\\ServiceError.txt\"\n\t)\n BEGIN\n {\n Write-Verbose -Message \"Starting the Function...Checking if ErrorFile exists- $ErrorFile\"\n #CReate the Errofile ...it will log Offline machines, Machines with issues\n if (!(Test-Path -Path $ErrorFile -PathType Leaf))\n {\n Write-Verbose -Message \"Creating ErrorFile & logging time to it - $ErrorFile\"\n New-Item -Path $ErrorFile -ItemType file \n Add-Content -Value \"$(\"#\"*40)$(Get-Date)$(\"#\"*40)\" -Path $ErrorFile\n \n }\n else\n {\n Write-Verbose -Message \" ErrorFile exists & logging time to it - $ErrorFile\"\n Add-Content -Value \"$(\"#\"*40)$(Get-Date)$(\"#\"*40)\" -Path $ErrorFile\n }\n \n \n }\n\t\tPROCESS \n\t\t{\n\t\t\tforeach ($computer in $computername )\n\t\t\t{\n\t\t\t\tWrite-Verbose -Message \"Checking if $Computer is online\"\n\t\t\t\tif (Test-Connection -ComputerName $Computer -Count 2 -Quiet)\n {\n Write-Verbose -message \"$Computer is online\"\n #region try to set the required state and StartupType of the Service\n try\n {\n #based on the Parameter Set used create the Filter\n Switch -Exact ($PSCmdlet.ParameterSetName)\n {\n\t \"Name\" {$Filter = \"Name LIKE '$Name'\" ; break}\n \"DisplayName\"{$Filter = \"Name LIKE '$DisplayName'\"}\n\n }\n\n $service = Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter $Filter -ErrorAction Stop\n \n #Check the State and set it\n if ( $service.State -ne \"$state\")\n {\n #Set the State of the Remtoe Service\n switch -exact ($state)\n {\n 'Running' \n {\n $changestateaction = $service.startService()\n Start-Sleep -Seconds 2 #it will require some time to process action\n if ($changestateaction.ReturnValue -ne 0 )\n {\n $err = Invoke-Expression \"net helpmsg $($changestateaction.ReturnValue)\" \n Write-Warning -message \"$Computer couldn't change state to $state `nWMI Call Returned :$err\" \n \n }\n break\n \n }\n \n 'Stopped' \n {\n $changestateaction = $service.stopService()\n Start-Sleep -Seconds 2 \n if ($changestateaction.ReturnValue -ne 0 )\n {\n $err = Invoke-Expression \"net helpmsg $($changestateaction.ReturnValue)\" \n Write-Warning -message \"$Computer couldn't change state to $state `nWMI Call Returned :$err\" \n }\n break\n }\n \n } #end switch\n } #end if\n\n #Check the StartMode and set it\n if ($service.startMode -ne $startupType)\n {\n \n #set the Start Mode of the Remote Service\n $changemodeaction = $service.ChangeStartMode(\"$startupType\")\n Start-Sleep -Seconds 2\n if ($changemodeaction.ReturnValue -ne 0 )\n {\n $err = Invoke-Expression \"net helpmsg $($changemodeaction.ReturnValue)\" \n Write-Warning -message \"$Computer couldn't change startmode to $startupType `nWMI Call Returned :$err\" \n }\n \n } #end if\n \n \n #Write the Object to the Pipeline\n Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter \"Name='$name'\" -ErrorAction Stop | Select-Object -Property @{Label=\"ComputerName\";Expression={\"$($_.__SERVER)\"}},@{Label=\"ServiceName\";Expression={$_.Name}},StartMode,State \n \n }#end try\n catch\n {\n \n Write-Warning -Message \"$Computer :: $_.exception...logging\"\n Add-Content -Value \"$computer :: $_.exception\" -Path $ErrorFile\n } #end catch\n\n #endregion try to set the required state and StartupType of the Service\t\n \t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t}\n else\n {\n Write-Verbose -Message \"$Computer is Offline..Logging\"\n Add-Content -Value \"$computer :: Offline\" -Path $ErrorFile\n }\n\t\t\t\t\t\t\n\t\t} #end foreach ($computer in $Computername)\n\t}#end PROCESS\n END\n {\n Write-Verbose -Message \"Stopping the Function\"\n $ErrorActionPreference = 'Continue' #Setting it back, Just in case someone is running this from ISE\n }\n\n}\n<\/code><\/pre>","upvoteCount":5,"answerCount":9,"datePublished":"2023-06-02T04:15:12.000Z","author":{"@type":"Person","name":"spiceuser-wrauu","url":"https://community.spiceworks.com/u/spiceuser-wrauu"},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
hi i have found this script but not running<\/p>\n
Function Set-RemoteService\n{\n<#\n\t.SYNOPSIS\n\t\tSets the state and Start Mode for a Service on Remote Machine\n\n\t.DESCRIPTION\n\t\tUses Win32_Service to set the state and Start Mode of the Service on Remote Machine.\n Correctly traps all the WMI Return Values and logs them in an Error File\n\n\t.PARAMETER ComputerName\n\t Enter a ComputerName accepts multiple ComputerNames \n\n\t.PARAMETER Name\n\t\tEnter the name of the Service (Will accept WQL Keywords)\n\t\n\t.PARAMETER DisplayName\n\t\tEnter the DisplayName of the target Service (Will accept WQL wildacard)\n\t\n\t.PARAMETER State\n\t\tSpecify the State, the state of the service on the Targetmachine will be changed to this\n Valid Values Are: Running, Stopped\n\t\n\t.PARAMETER startupType\n\t\tSpecify Service Startmode, the service on the target machine will have it's startmode changed to this. \n Valid Values are: Auto, Disabled, and Manual\n\t\n\t.PARAMETER ErrorFile\n\t\tLogs Error in the file specified. \n Default ErrorFile is ServiceError.txt at the Desktop of the User running Script.\n\t\n .EXAMPLE\n On the local machine set the \n \n PS C:\\> Set-RemoteService -Name bits -state Running -startupType Auto\n\n ComputerName ServiceName StartMode State \n ------------ ----------- --------- ----- \n DexterPC2143 BITS Auto Running \n\n\t.EXAMPLE\n In the below example the WMI Return Code 21 (Device is not ready) is returned and that comes up as a warning \n\t\t\n PS C:\\> Set-RemoteService -Name ccmexec -state Running -startupType Auto\n WARNING: DexterPC couldn't change state to Running\n WMI Call Returned : The device is not ready\" \n \n ComputerName ServiceName StartMode State \n ------------ ----------- --------- ----- \n DexClient1 BITS Auto Stopped \n\n .EXAMPLE\n Set the bits service state & startmode on the remote machine , verbose swicth turned on\n\n PS C:\\> Set-RemoteService -ComputerName DexClient2420 -Name bits -state Running -startupType Automatic -Verbose\n VERBOSE: Starting the Function...Checking if ErrorFile exists- C:\\Users\\ddha002\\Desktop\\ServiceError.txt\n VERBOSE: ErrorFile exists & logging time to it - C:\\Users\\ddha002\\Desktop\\ServiceError.txt\n VERBOSE: Checking if DexClient2420 is online\n VERBOSE: DexClient2420 is online\n\n ComputerName ServiceName StartMode State \n ------------ ----------- --------- ----- \n DexClient2420 BITS Auto Running \n VERBOSE: Stopping the Function\n\n .INPUTS\n System.String[]\n\n\t.OUTPUTS\n\t\tSelected.System.Management.ManagementObject[]\n\n\t.NOTES\n\t\t\n Reused Code from : \n \n Author - Jason Morgan\n Script - Set-ServiceStartMode [http://gallery.technet.microsoft.com/Set-ServiceStartMode-18a6e13d]\n Used the DisplayName parameter after seeing this Script \n\n Author - Sitaram Pamarthi\n URL - http://techibee.com/powershell/powershell-find-services-that-failed-to-start-after-server-reboot/2036\n Used Net helpmsg to interpret WMI Return codes after this article\n\n Written by - DexterPOSH\n Blog Url - http://dexterposgh.blogspot.com\n#>\n#requires -version 3.0\n[CmdletBinding(DefaultParameterSetName='Name')]\n[OutputType([PSObject])]\nparam(\n\t[Parameter(Position=0, Mandatory=$false,\n\t\t\t\thelpmessage=\"Enter the ComputerNames to Chec & Fix Services on\",\n\t\t\t\tValueFromPipeline=$true,\n\t\t\t\tValueFromPipelineByPropertyName=$true\n\t\t\t\t)]\n\t#[ValidateScript({try{[system.net.dns]::Resolve(\"$_\")|out-null; return $true} catch { throw \"could not resolve the machine name\"}})]\n\t[String[]]\n\t$ComputerName=$env:COMPUTERNAME,\n\n [Parameter(Mandatory=$true,\n ParameterSetName=\"Name\",\n helpmessage=\"Enter the Service Name (Accepts WQL wildacard)\")]\n [string]$Name,\n\n [Parameter(Mandatory=$True, \n ParameterSetName=\"DisplayName\", \n HelpMessage=\"Enter the DisplayName of the target Service(Accepts WQL wildacard)\" )] \n [String]$DisplayName, \n\n [Parameter(Mandatory=$true,helpmessage=\"Enter the state of the Service to be set\")]\n [ValidateSet(\"Running\",\"Stopped\")]\n [string]$state,\n\n [Parameter(Mandatory=$true,helpmessage=\"Enter the Startup Type of the Service to be set\")]\n [ValidateSet(\"Automatic\",\"Manual\",\"Disabled\")]\n [string]$startupType,\n\n [Parameter(Mandatory=$false,helpmessage=\"Enter the Startup Type of the Service to be set\")]\n [string]$ErrorFile=\"$([System.Environment]::GetFolderPath(\"Desktop\"))\\ServiceError.txt\"\n\t)\n BEGIN\n {\n Write-Verbose -Message \"Starting the Function...Checking if ErrorFile exists- $ErrorFile\"\n #CReate the Errofile ...it will log Offline machines, Machines with issues\n if (!(Test-Path -Path $ErrorFile -PathType Leaf))\n {\n Write-Verbose -Message \"Creating ErrorFile & logging time to it - $ErrorFile\"\n New-Item -Path $ErrorFile -ItemType file \n Add-Content -Value \"$(\"#\"*40)$(Get-Date)$(\"#\"*40)\" -Path $ErrorFile\n \n }\n else\n {\n Write-Verbose -Message \" ErrorFile exists & logging time to it - $ErrorFile\"\n Add-Content -Value \"$(\"#\"*40)$(Get-Date)$(\"#\"*40)\" -Path $ErrorFile\n }\n \n \n }\n\t\tPROCESS \n\t\t{\n\t\t\tforeach ($computer in $computername )\n\t\t\t{\n\t\t\t\tWrite-Verbose -Message \"Checking if $Computer is online\"\n\t\t\t\tif (Test-Connection -ComputerName $Computer -Count 2 -Quiet)\n {\n Write-Verbose -message \"$Computer is online\"\n #region try to set the required state and StartupType of the Service\n try\n {\n #based on the Parameter Set used create the Filter\n Switch -Exact ($PSCmdlet.ParameterSetName)\n {\n\t \"Name\" {$Filter = \"Name LIKE '$Name'\" ; break}\n \"DisplayName\"{$Filter = \"Name LIKE '$DisplayName'\"}\n\n }\n\n $service = Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter $Filter -ErrorAction Stop\n \n #Check the State and set it\n if ( $service.State -ne \"$state\")\n {\n #Set the State of the Remtoe Service\n switch -exact ($state)\n {\n 'Running' \n {\n $changestateaction = $service.startService()\n Start-Sleep -Seconds 2 #it will require some time to process action\n if ($changestateaction.ReturnValue -ne 0 )\n {\n $err = Invoke-Expression \"net helpmsg $($changestateaction.ReturnValue)\" \n Write-Warning -message \"$Computer couldn't change state to $state `nWMI Call Returned :$err\" \n \n }\n break\n \n }\n \n 'Stopped' \n {\n $changestateaction = $service.stopService()\n Start-Sleep -Seconds 2 \n if ($changestateaction.ReturnValue -ne 0 )\n {\n $err = Invoke-Expression \"net helpmsg $($changestateaction.ReturnValue)\" \n Write-Warning -message \"$Computer couldn't change state to $state `nWMI Call Returned :$err\" \n }\n break\n }\n \n } #end switch\n } #end if\n\n #Check the StartMode and set it\n if ($service.startMode -ne $startupType)\n {\n \n #set the Start Mode of the Remote Service\n $changemodeaction = $service.ChangeStartMode(\"$startupType\")\n Start-Sleep -Seconds 2\n if ($changemodeaction.ReturnValue -ne 0 )\n {\n $err = Invoke-Expression \"net helpmsg $($changemodeaction.ReturnValue)\" \n Write-Warning -message \"$Computer couldn't change startmode to $startupType `nWMI Call Returned :$err\" \n }\n \n } #end if\n \n \n #Write the Object to the Pipeline\n Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter \"Name='$name'\" -ErrorAction Stop | Select-Object -Property @{Label=\"ComputerName\";Expression={\"$($_.__SERVER)\"}},@{Label=\"ServiceName\";Expression={$_.Name}},StartMode,State \n \n }#end try\n catch\n {\n \n Write-Warning -Message \"$Computer :: $_.exception...logging\"\n Add-Content -Value \"$computer :: $_.exception\" -Path $ErrorFile\n } #end catch\n\n #endregion try to set the required state and StartupType of the Service\t\n \t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t}\n else\n {\n Write-Verbose -Message \"$Computer is Offline..Logging\"\n Add-Content -Value \"$computer :: Offline\" -Path $ErrorFile\n }\n\t\t\t\t\t\t\n\t\t} #end foreach ($computer in $Computername)\n\t}#end PROCESS\n END\n {\n Write-Verbose -Message \"Stopping the Function\"\n $ErrorActionPreference = 'Continue' #Setting it back, Just in case someone is running this from ISE\n }\n\n}\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2023-06-02T04:15:12.000Z","url":"https://community.spiceworks.com/t/i-am-trying-to-start-services-to-a-remotes-computers/952970/1","author":{"@type":"Person","name":"spiceuser-wrauu","url":"https://community.spiceworks.com/u/spiceuser-wrauu"}},{"@type":"Answer","text":"
Advertisement
What OS are these machines ?? Do they have firewall and/or AV etc installed ?<\/p>\n
The reason is that due to security reasons, there are many security tools & features that disable remote starting of services if the machines themselves do not start these services (or stop the services).<\/p>\n
Also what credentials are you using to run the script as you need the local administrators of the remote machines (not the machine you running on) and/or the remote machine’s respective authorized service creds.<\/p>","upvoteCount":0,"datePublished":"2023-06-02T06:38:00.000Z","url":"https://community.spiceworks.com/t/i-am-trying-to-start-services-to-a-remotes-computers/952970/2","author":{"@type":"Person","name":"adrian_ych","url":"https://community.spiceworks.com/u/adrian_ych"}},{"@type":"Answer","text":"
Welcome to the community!<\/p>\n