question for get-service and resetiis powershell commands

Hello everyone,
I just wanted to ask you if we are doing the most efficient way of resetting IIS and validating services on multiple computers.
 
Is this command could be simplified? We only have two servers where we are resetting IIS and 5 computers where we need to validate and set services.
 
Perhaps what we are doing here is NOT the most efficient process.
 
Let me know. Thank you so much!
 
1. At the elevated PowerShell prompt, run the commands below:
      a. Reset IIS
      $pors = (get-content "f:\software\servers.txt") | select-string "por0"
      Foreach ($por in $pors) {Invoke-command –ComputerName $por –ScriptBlock {iisreset}}

      b. Set all DEV services to Automatic - Running
      $servers = get-content -path "f:\software\servers.txt"
      Foreach ($server in $servers) {Invoke-command –ComputerName $server –ScriptBlock {get-service –displayname *DEV* | set-service –startuptype automatic –status running}}

      c. Validate all DEV services status
      $services = Foreach ($server in $servers) {Invoke-command –ComputerName $server –ScriptBlock {get-service –displayname *adms* | select pscomputername,displayname,status,starttype}}
      $services |format-table -autosize
 
creative555Asked:
Who is Participating?
 
oBdACommented:
There's basically no need to use the big hammer Invoke-Command. The Get-/Set-Service cmdlets support remote operation natively.
If you have iisreset.exe on the machine where you're running the command, you don't need Invoke-Command, either.
Then be careful with Select-String; it returns MatchInfo objects, not the matching strings passed initially; can have unexpected side effects if you continue with the results. You should reference the Line property explicitly for good measure, or use a Where-Object.
a. Reset IIS
If iisreset.exe is locally available:
## One-liner for typing:
gc "f:\software\servers.txt" | sls "por0" | % {& iisreset.exe $_.Line}
## One-liner for typing using Where-Object (alias '?'):
gc "f:\software\servers.txt" | ? {$_ -match "por0"} | % {& iisreset.exe $_}

## Full version for scripting (you should not use aliases in scripts):
Get-Content -Path "f:\software\servers.txt" |
	Select-String "por0" |
	ForEach-Object {& iisreset.exe $_.Line}

Open in new window

If iisreset.exe is only available on the remote servers:
## One-liner for typing:
gc "f:\software\servers.txt" | sls "por0" | % {icm {& iisreset.exe} -cn $_.Line}

## Full version for scripting (you should not use aliases in scripts):
Get-Content -Path "f:\software\servers.txt" |
	Select-String "por0" |
	ForEach-Object {Invoke-Command {& iisreset.exe} -ComputerName $_.Line}

Open in new window


b. Set all DEV services to Automatic - Running
## One-liner for typing:
gc "f:\software\servers.txt" | % {gsv -DisplayName *DEV* -cn $_} | Set-Service -st Automatic -Status Running

## Full version for scripting (you should not use aliases in scripts):
Get-Content -Path "f:\software\servers.txt" |
	ForEach-Object {Get-Service -DisplayName *DEV* -ComputerName $_} |
	Set-Service -StartupType Automatic -Status Running

Open in new window


c. Validate all DEV services status
Based on the display name passed, I guess you meant "Validate all ADMS services status"?
Format-Table supports the -Properties parameter, so you don't need the Select-Object.
Property names of Select-Object and Format-* support wildcards, so when in the PS console, you can save some typing. In a script, you should flesh it out - other people might read and try to understand it.
## One-liner for typing:
gc "f:\software\servers.txt" | % {gsv -DisplayName *ADMS* -cn $_} | ft -au -Property M*, Di*, St*

## Full version for scripting (you should not use aliases in scripts):
Get-Content "f:\software\servers.txt" |
	ForEach-Object {Get-Service -DisplayName *ADMS* -ComputerName $_} |
	Format-Tabe -AutoSize -Property MachineName, DisplayName, Status, StartType

Open in new window

1
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
0
 
creative555Author Commented:
Thank you so much!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.