Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ff05b1

Browse files
added final example
1 parent d21c8f9 commit 6ff05b1

File tree

1 file changed

+77
-29
lines changed

1 file changed

+77
-29
lines changed

‎LearnPowerShell/EP9 - PowerShell Remoting.ps1

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,6 @@ Invoke-Command -Session $sessions -ScriptBlock {Stop-Service BITS -Force}
146146

147147
#endregion
148148

149-
#region advanced WinRM
150-
151-
#add server to trusted hosts
152-
ls WSMan:\localhost\Client\TrustedHosts
153-
winrm s winrm/config/client '@{TrustedHosts="673448-RAXDC01"}'
154-
winrm s winrm/config/client '@{TrustedHosts="579188-HYP1"}'
155-
156-
#domain to domain (http)
157-
New-PSSession -ComputerName Test-Join -Credential domain\user
158-
$domainToDomainHTTP = New-PSSession -ComputerName RemoteDeviceName -Credential domain\account
159-
160-
#domain to domain (requires https listener and certificates pre-configured)
161-
New-PSSession -ComputerName Test-Join -Credential domain\user -UseSSL
162-
$domainToDomainHTTPS = New-PSSession -ComputerName PDC2 -Credential domain\account -UseSSL
163-
164-
#by IP self-signed cert
165-
$so = New-PSSessionOption -SkipCNCheck -SkipCACheck -SkipRevocationCheck
166-
$test = New-PSSession -ComputerName 10.0.3.27 -Credential domain/account -UseSSL -SessionOption $so
167-
168-
#change port WinRM listens on
169-
winrm/config/Listener?Address=*+Transport=HTTP '@{Port="8888"}'
170-
171-
#check WinRM settings
172-
Get-WSManInstance -ResourceURI winrm/config/service/Auth
173-
Get-WSManInstance -ResourceURI winrm/config/client/Auth
174-
Get-WSManInstance -ResourceURI winrm/config/client
175-
176-
#endregion
177-
178149
#region PowerShell-Linux-Remote-Access
179150

180151
#install openssh
@@ -211,4 +182,81 @@ foreach ($server in $devices) {
211182
Invoke-Command -ComputerName $server -ScriptBlock {$env:COMPUTERNAME} -Credential $credential
212183
}
213184

185+
#endregion
186+
187+
#region advanced WinRM
188+
189+
#add server to trusted hosts
190+
ls WSMan:\localhost\Client\TrustedHosts
191+
winrm s winrm/config/client '@{TrustedHosts="673448-RAXDC01"}'
192+
winrm s winrm/config/client '@{TrustedHosts="579188-HYP1"}'
193+
194+
#domain to domain (http)
195+
New-PSSession -ComputerName Test-Join -Credential domain\user
196+
$domainToDomainHTTP = New-PSSession -ComputerName RemoteDeviceName -Credential domain\account
197+
198+
#domain to domain (requires https listener and certificates pre-configured)
199+
New-PSSession -ComputerName Test-Join -Credential domain\user -UseSSL
200+
$domainToDomainHTTPS = New-PSSession -ComputerName PDC2 -Credential domain\account -UseSSL
201+
202+
#by IP self-signed cert
203+
$so = New-PSSessionOption -SkipCNCheck -SkipCACheck -SkipRevocationCheck
204+
$test = New-PSSession -ComputerName 10.0.3.27 -Credential domain/account -UseSSL -SessionOption $so
205+
206+
#change port WinRM listens on
207+
winrm/config/Listener?Address=*+Transport=HTTP '@{Port="8888"}'
208+
209+
#check WinRM settings
210+
Get-WSManInstance -ResourceURI winrm/config/service/Auth
211+
Get-WSManInstance -ResourceURI winrm/config/client/Auth
212+
Get-WSManInstance -ResourceURI winrm/config/client
213+
214+
#endregion
215+
216+
#region final Example
217+
218+
#declare servers we will connect to remotely
219+
$servers = 'Server1','Server2','Server3','Server4'
220+
#capture credentials used for remote access
221+
$creds = Get-Credential
222+
223+
#declare array to hold remote command results
224+
$remoteResults = @()
225+
226+
#declare a splat for our Invoke-Command parameters
227+
$invokeSplat = @{
228+
ComputerName = $servers
229+
Credential = $creds
230+
ErrorVariable = 'connectErrors'
231+
ErrorAction = 'SilentlyContinue'
232+
}
233+
234+
#execute remote command with splatted parameters.
235+
#store results in variable
236+
#errors will be stored in connectErrors
237+
$remoteResults = Invoke-Command @invokeSplat -ScriptBlock {
238+
#declare a custom object to store result in and return
239+
$obj = [PSCustomObject]@{
240+
Name = $env:COMPUTERNAME
241+
CPUs = "-------"
242+
Memory = "-------"
243+
FreeSpace = "-------"
244+
}
245+
#retrieve the CPU / Memory / Hard Drive information
246+
$obj.CPUs = (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors
247+
$obj.Memory = Get-CimInstance Win32_OperatingSystem `
248+
| Measure-Object -Property TotalVisibleMemorySize -Sum `
249+
| ForEach-Object { [Math]::Round($_.sum / 1024 / 1024) }
250+
$driveData = Get-PSDrive C | Select-Object Used, Free
251+
$total = $driveData.Used + $driveData.Free
252+
$calc = [Math]::Round($driveData.Free / $total, 2)
253+
$obj.FreeSpace = $calc * 100
254+
return $obj
255+
}
256+
257+
#capture any connection errors
258+
$remoteFailures = $connectErrors.CategoryInfo `
259+
| Where-Object {$_.Reason -eq 'PSRemotingTransportException'} `
260+
| Select-Object TargetName,@{n = 'ErrorInfo'; E = {$_.Reason} }
261+
214262
#endregion

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /