1
+ # region WinRM Links
2
+
3
+ # Running Remote Commands
4
+ # https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-6
5
+
6
+ # Windows Remote Management
7
+ # https://docs.microsoft.com/en-us/windows/win32/winrm/portal
8
+
9
+ # Installation and Configuration for Windows Remote Management
10
+ # https://docs.microsoft.com/en-us/windows/win32/winrm/installation-and-configuration-for-windows-remote-management
11
+
12
+ # Making the second hop in PowerShell Remoting
13
+ # https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-6
14
+
15
+ # PowerShell remoting over SSH
16
+ # https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-core?view=powershell-6
17
+
18
+ # How to configure WinRM for HTTPS manually
19
+ # https://www.visualstudiogeeks.com/devops/how-to-configure-winrm-for-https-manually
20
+
21
+ # endregion
22
+
23
+ # region ComputerName
24
+
25
+ # Per PowerShell documentation you can find a list of cmdlets that support ComputerName with the following:
26
+ Get-Command | Where-Object { $_.parameters.keys -contains " ComputerName" -and $_.parameters.keys -notcontains " Session" }
27
+
28
+ # this will prompt you to enter your access credentials. the creds will be securely stored in the variable
29
+ $creds = Get-Credential
30
+
31
+ # restart a single computer
32
+ Restart-Computer - ComputerName RemoteDevice - Credential $creds
33
+
34
+ # restart several computers
35
+ Restart-Computer - ComputerName RemoteDevice1, RemoteDevice2, RemoteDevice3 - Credential $creds
36
+
37
+ # restart an entire list of computers
38
+ $devices = Get-Content - Path C:\listOfServers.txt
39
+ Restart-Computer - ComputerName $devices - Credential $Creds - Force
40
+
41
+ # endregion
42
+
43
+ # region WinRM
44
+
45
+ # basic WinRM configuration with default settings
46
+ winrm quickconfig
47
+
48
+ # example of WinRM configuration with more specific settings
49
+ winrm quickconfig - transport:https
50
+
51
+ # check winrm settings
52
+ winrm get winrm/ config/ client
53
+ winrm get winrm/ config/ service
54
+
55
+ PS C:\> winrm enumerate winrm/ config/ listener
56
+ Listener
57
+ Address = *
58
+ Transport = HTTP
59
+ Port = 5985
60
+ Hostname
61
+ Enabled = true
62
+ URLPrefix = wsman
63
+ CertificateThumbprint
64
+ ListeningOn = 10.0 .3.253 , 127.0 .0.1 , 192.168 .1.253 , ::1 ,
65
+
66
+ # verify that WinRM is setup and configured locally
67
+ Test-WSMan
68
+
69
+ # verify that WinRM is setup and responding on a remote device
70
+ # you must specify the authentication type when testing a remote device.
71
+ # if you are unsure about the authentication, set it to Negotiate
72
+ $credential = Get-Credential
73
+ Test-WSMan RemoteDeviceName - Authentication Negotiate - Credential $credential
74
+
75
+ # verify local device is listening on WinRM port
76
+ Get-NetTCPConnection - LocalPort 5985
77
+
78
+ # verify a remote device is listening on WinRM port
79
+ Test-NetConnection - Computername 192.168 .34.13 - Port 5985
80
+
81
+ # establish an interactive remote session
82
+ $credential = Get-Credential
83
+ Enter-PSSession - ComputerName RemoteDeviceName - Credential $credential
84
+
85
+ # basic session opened to remote device
86
+ $session = New-PSSession - ComputerName RemoteDeviceName - Credential domain\user
87
+
88
+ # session opened to device over SSL
89
+ $credential = Get-Credential
90
+ $sessionHTTPS = New-PSSession - ComputerName RemoteDeviceName - Credential $credential - UseSSL
91
+
92
+ # establish sessions to multiple devices
93
+ $credential = Get-Credential
94
+ $multiSession = New-PSSession - ComputerName RemoteDeviceName1, RemoteDeviceName2, RemoteDeviceName3 - Credential $credential
95
+
96
+ # establish session to an entire list of devices
97
+ $devices = Get-Content - Path C:\listOfServers.txt
98
+ $credential = Get-Credential
99
+ $multiSession = New-PSSession - ComputerName $devices - Credential $credential
100
+
101
+ # session opened with advanced session options configured
102
+ $sessionOptions = New-PSSessionOption - SkipCNCheck - SkipCACheck - SkipRevocationCheck
103
+ $advancedSession = New-PSSession - ComputerName 10.0 .3.27 - Credential user - UseSSL - SessionOption $so
104
+
105
+ # endRegion
106
+
107
+ # region Invoke-Command examples
108
+
109
+ # get the number of CPUs for each remote device
110
+ Invoke-Command - Session $sessions - ScriptBlock {(Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors}
111
+
112
+ # get the amount of RAM for each remote device
113
+ Invoke-Command - Session $sessions - ScriptBlock {Get-CimInstance Win32_OperatingSystem | Measure-Object - Property TotalVisibleMemorySize - Sum | ForEach-Object {[Math ]::Round($_.sum / 1024 / 1024 )}}
114
+
115
+ # get the amount of free space on the C: drive for each remote device
116
+ Invoke-Command - Session $sessions - ScriptBlock {
117
+ $driveData = Get-PSDrive C | Select-Object Used, Free
118
+ $total = $driveData.Used + $driveData.Free
119
+ $calc = [Math ]::Round($driveData.Free / $total , 2 )
120
+ $perFree = $calc * 100
121
+ return $perFree
122
+ }
123
+
124
+ # stop the BITS service on all remote devices
125
+ # get the number of CPUs for each remote device
126
+ Invoke-Command - Session $sessions - ScriptBlock {(Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors}
127
+
128
+ # get the amount of RAM for each remote device
129
+ Invoke-Command - Session $sessions - ScriptBlock {Get-CimInstance Win32_OperatingSystem | Measure-Object - Property TotalVisibleMemorySize - Sum | ForEach-Object {[Math ]::Round($_.sum / 1024 / 1024 )}}
130
+
131
+ # get the amount of free space on the C: drive for each remote device
132
+ Invoke-Command - Session $sessions - ScriptBlock {
133
+ $driveData = Get-PSDrive C | Select-Object Used, Free
134
+ $total = $driveData.Used + $driveData.Free
135
+ $calc = [Math ]::Round($driveData.Free / $total , 2 )
136
+ $perFree = $calc * 100
137
+ return $perFree
138
+ }
139
+
140
+ # stop the BITS service on all remote devices
141
+ Invoke-Command - Session $sessions - ScriptBlock {Stop-Service BITS - Force}
142
+
143
+ # endregion
144
+
145
+ # region advanced WinRM
146
+
147
+ # add server to trusted hosts
148
+ ls WSMan:\localhost\Client\TrustedHosts
149
+ winrm s winrm/ config/ client ' @{TrustedHosts="673448-RAXDC01"}'
150
+ winrm s winrm/ config/ client ' @{TrustedHosts="579188-HYP1"}'
151
+
152
+ # domain to domain (http)
153
+ New-PSSession - ComputerName Test-Join - Credential domain\user
154
+ $domainToDomainHTTP = New-PSSession - ComputerName RemoteDeviceName - Credential domain\account
155
+
156
+ # domain to domain (requires https listener and certificates pre-configured)
157
+ New-PSSession - ComputerName Test-Join - Credential domain\user - UseSSL
158
+ $domainToDomainHTTPS = New-PSSession - ComputerName PDC2 - Credential domain\account - UseSSL
159
+
160
+ # by IP self-signed cert
161
+ $so = New-PSSessionOption - SkipCNCheck - SkipCACheck - SkipRevocationCheck
162
+ $test = New-PSSession - ComputerName 10.0 .3.27 - Credential domain/ account - UseSSL - SessionOption $so
163
+
164
+ # change port WinRM listens on
165
+ winrm/ config/ Listener?Address=*+ Transport= HTTP ' @{Port="8888"}'
166
+
167
+ # check WinRM settings
168
+ Get-WSManInstance - ResourceURI winrm/ config/ service/ Auth
169
+ Get-WSManInstance - ResourceURI winrm/ config/ client/ Auth
170
+ Get-WSManInstance - ResourceURI winrm/ config/ client
171
+
172
+ # endregion
173
+
174
+ # region PowerShell-Linux-Remote-Access
175
+
176
+ # install openssh
177
+ sudo apt install openssh- client
178
+ sudo apt install openssh- server
179
+
180
+ # Edit the sshd_config file at location /etc/ssh
181
+ # Make sure password authentication is enabled:
182
+ PasswordAuthentication yes
183
+
184
+ # Add a PowerShell subsystem entry:
185
+ Subsystem powershell / usr/ bin/ pwsh - sshs - NoLogo - NoProfile
186
+
187
+ # Optionally, enable key authentication:
188
+ PubkeyAuthentication yes
189
+
190
+ # Restart the sshd service.
191
+ sudo service sshd restart
192
+
193
+ # establish an interactive session to a remote Linux device
194
+ $session = New-PSSession - HostName RemoteDevice - UserName user - SSHTransport
195
+ Enter-PSSession $session
196
+
197
+ # execute commmands on a remote Linux device
198
+ $session = New-PSSession - HostName RemoteDevice - UserName user - SSHTransport
199
+ Invoke-Command - Session $session - ScriptBlock {Get-Process }
200
+
201
+ # alternative to running Invoke-Command in parallel
202
+ # foreach forces sequential connection and return for each server in the list
203
+ # establish session to an entire list of devices
204
+ $devices = Get-Content - Path C:\listOfServers.txt
205
+ $credential = Get-Credential
206
+ foreach ($server in $devices ) {
207
+ Invoke-Command - ComputerName $server - ScriptBlock {$env: COMPUTERNAME } - Credential $credential
208
+ }
209
+
210
+ # endregion
0 commit comments