Hi All,<\/p>\n
I have pieced together below script which enables PSRemoting for selected computers.<\/p>\n
Does anyone know how can improve on this, any suggestions ?<\/p>\n
I.e<\/p>\n
Is the use of<\/p>\n
Function\n<\/code><\/pre>\nany good in my case ? if not, how can get rid of it ?<\/p>\n
Also, I want the script to export failures telling me which computers it could not enable PSRemoting on via a csv file.<\/p>\n
$ComputersArray = Get-Content computers.txt\n\nFunction enableWinRM {\n\nforeach ($Computer in $ComputersArray)\n{\n\n $result = winrm id -r:$Computer 2>$null\n\n Write-Host \n Write-Host \"Enabling WinRM on\" $Computer \"...\" -ForegroundColor cyan\n C:\\PSTools\\PsExec.exe \\\\$Computer -s powershell enable-PSRemoting -Force\n if ($LastExitCode -eq 0) {\n C:\\PSTools\\PsService.exe \\\\$Computer restart WinRM\n $result = winrm id -r:$Computer 2>$null\n \n if ($LastExitCode -eq 0) {Write-Host 'WinRM successfully enabled!' -ForegroundColor green}\n else {Write-Host 'WinRM failed to be enabled!' -ForegroundColor red}\n } \n \n }\n}\n\nenableWinRM\nexit 0\n<\/code><\/pre>\nMany thanks<\/p>\n
Regards<\/p>\n
Kash<\/p>\n
@alexw<\/a> @jitensh<\/a><\/p>","upvoteCount":5,"answerCount":14,"datePublished":"2016-12-29T16:44:58.000Z","author":{"@type":"Person","name":"kashifriaz","url":"https://community.spiceworks.com/u/kashifriaz"},"acceptedAnswer":{"@type":"Answer","text":"Do something like this, very roughly<\/p>\n
ForEach ($COMPUTER in (Get-Content c:\\computers.txt\n))\n{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n \n{write-host \"cannot reach $computer\" -f red}\n \n \n \nelse {\n \n\n\t\n\t\tWrite-Host \"Enabling WinRM on\" $Computer \"...\" -ForegroundColor cyan\n\t\tC:\\PSTools\\PsExec.exe \\\\$Computer -s powershell enable-PSRemoting -Force\n\t\tif ($LastExitCode -eq 0) {\n\t\t\tC:\\PSTools\\PsService.exe \\\\$Computer restart WinRM\n\t\t\t$result = winrm id -r:$Computer 2>$null\n\t\t\t\n\t\t\tif ($LastExitCode -eq 0) {Write-Host 'WinRM successfully enabled! on $computer' -ForegroundColor green}\n\n\t\t\telse{ $result | Add-Content c:\\failed.txt \"`n$computer\"\n\n -append}\n\t\t} \n\t\t\n\t}\n\n}\n}\n \n<\/code><\/pre>","upvoteCount":1,"datePublished":"2017-01-04T11:42:13.000Z","url":"https://community.spiceworks.com/t/powershell-enable-winrm-psremoting-for-selected-computers/549634/11","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi All,<\/p>\n
I have pieced together below script which enables PSRemoting for selected computers.<\/p>\n
Does anyone know how can improve on this, any suggestions ?<\/p>\n
I.e<\/p>\n
Is the use of<\/p>\n
Function\n<\/code><\/pre>\nany good in my case ? if not, how can get rid of it ?<\/p>\n
Also, I want the script to export failures telling me which computers it could not enable PSRemoting on via a csv file.<\/p>\n
$ComputersArray = Get-Content computers.txt\n\nFunction enableWinRM {\n\nforeach ($Computer in $ComputersArray)\n{\n\n $result = winrm id -r:$Computer 2>$null\n\n Write-Host \n Write-Host \"Enabling WinRM on\" $Computer \"...\" -ForegroundColor cyan\n C:\\PSTools\\PsExec.exe \\\\$Computer -s powershell enable-PSRemoting -Force\n if ($LastExitCode -eq 0) {\n C:\\PSTools\\PsService.exe \\\\$Computer restart WinRM\n $result = winrm id -r:$Computer 2>$null\n \n if ($LastExitCode -eq 0) {Write-Host 'WinRM successfully enabled!' -ForegroundColor green}\n else {Write-Host 'WinRM failed to be enabled!' -ForegroundColor red}\n } \n \n }\n}\n\nenableWinRM\nexit 0\n<\/code><\/pre>\nMany thanks<\/p>\n
Regards<\/p>\n
Kash<\/p>\n
@alexw<\/a> @jitensh<\/a><\/p>","upvoteCount":5,"datePublished":"2016-12-29T16:44:58.000Z","url":"https://community.spiceworks.com/t/powershell-enable-winrm-psremoting-for-selected-computers/549634/1","author":{"@type":"Person","name":"kashifriaz","url":"https://community.spiceworks.com/u/kashifriaz"}},{"@type":"Answer","text":"Maybe have a look into this:<\/p>\n