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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
creative555Author Commented:
Thank you so much!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.