-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGetPSRemoteSession.ps1
More file actions
24 lines (20 loc) · 972 Bytes
/
GetPSRemoteSession.ps1
File metadata and controls
24 lines (20 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
param (
[string]$ComputerName = $(throw "-ComputerName is required."),
[string]$CertificateSubject = $(throw "-CertificateSubject is required.")
)
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
[System.Security.Cryptography.X509Certificates.StoreName]::My,
[System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
foreach($cert in $store.Certificates) {
if ($cert.Subject -eq $CertificateSubject) {
$sessionCert = $cert
break
}
}
if (!$sessionCert) {
throw "An X509 certificate matching subject `"$CertificateSubject`" could not be found."
}
$opt = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session = New-PSSession -ComputerName $ComputerName -UseSSL -CertificateThumbprint $sessionCert.Thumbprint -SessionOption $opt
Enter-PSSession $session