Closed
Description
Summary of the new feature / enhancement
Problem
In the case I have multiple servers where I want to run a scriptblock, including the server I am connected on, I am forced to write this over explicit code :
Note : the powershell session is running on ServerB
for ($computer in "ServerA", "ServerB", "ServerC") {
if ($computer -like $env:computername ) {
# Computer = ServerB
$Jobs += start-job -ScriptBlock $ScripBlock
} else {
# Computer = ServerA or ServerC
$Jobs += Invoke-Command -ComputerName $computer -AsJob -ScriptBlock $ScripBlock
}
}
Reason
- Start-job does not allow to run jobs on remote host
- Invoke-Command -ComputerName $env:computername -asjob -ScriptBlock $ScripBlock
- Requires to be "ran as administrator" to function
- OR
- throws an error
OpenError: [Hostname] Connecting to remote server Hostname failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
- Requires to be "ran as administrator" to function
Invoke-Command -AsJob -ScriptBlock { dir }
does not match required parameter set
Conclusion
This is not new to powershell and I have always found complexity into writing scriptblocks that could run on multiple servers, including the local server I am connected on
Proposed technical implementation details (optional)
As start-job
does not require to "run as administrator" I feel like there shouldn't be any restrictions into being able to run
Invoke-Command -AsJob -ScriptBlock { dir }
which could be, somehow, an alias ofstart-job
Invoke-Command -AsJob -ScriptBlock { dir } -computername $env:computername
without having to use the 'log on as adaministrator' switch