Invoke-command to Display machine name

<p style=“text-align: left;”>I’m performing an iisreset on multiple machines using the following statement. I would like to have it display the machine name after each iisreset. Please help?? Thanks</p>
Invoke-command -computername $servers -scriptblock {iisreset /restart}

Something like,

Invoke-Command -ComputerName $ServerList {"Resetting on $env:computername $((iisreset) -join '->')"}

Kvprasoon Thanks for your reply…this Works Perfectly but can you provide me a switch to add to prompt for “confirmation” to continue after it’s executed?

As for this…

switch to add to prompt for "confirmation" to continue after it's executed?

… as the ‘Confirm’ parameter is a built-in item for many, cmdlet/functions that support it. You can just hit up the help files to see which ones support the parameter / switch you are after. So go online, or do this…:

# Find all cmdlets / functions with a target parameter
Get-Command -CommandType Function | 
Where-Object { $_.parameters.keys -match 'confirm'} | 
Out-GridView -PassThru -Title 'Available functions which has a specific parameter'

Get-Command -CommandType Cmdlet | 
Where-Object { $_.parameters.keys -match 'confirm'} | 
Out-GridView -PassThru -Title 'Results for cmdlets which has a specific parameter'

Yet, you are calling and external tool. Thus that is not applicable. You have to do your own, using either if/then or try/catch. Meaning just check for the service state and take whatever further action you need.

If ((Get-Service -Name W3SVC).Status -eq 'Running')
{'IIS Service is running'}
Else {Write-Warning -Message 'IIS Service is not running'}

Or use this discussion if you really want to dig at this for custom stuff.

Confirm, $ConfirmPreference, and ConfirmImpact in PowerShell

Yet, doing this confirm stuff, means a human has to sitting at the keyboard to respond to each prompt. So, not very automated. It’s more prudent, just to check the state and report on it and try again, if the start was not successful.

You could use Get-Service and Restart-Service to get confirmation, but as stated by @postanote it is not very automation friendly.

Get-Service -ComputerName $serverlist -Name iisadmin,w3svc |Restart-Service -Confirm -WarningAction Ignore -Passthru |
    Select-Object MachineName, Status, Name, DisplayName

iisadmin service will restart was and http service.


No need to use invoke-command iisreset takes -computername
so you can just use

$servers = gc .\serverlist.txt
foreach ($server in $servers) { iisreset $server /restart}
foreach ($server in $servers) { iisreset $server /status}