Advertisement
Guest User

VPN_Profile.ps1

a guest
Sep 13th, 2018
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ProfileName = 'AlwaysOnVPN'
  2. $ProfileNameEscaped = $ProfileName -replace ' ', '%20'
  3.  
  4. $ProfileXML = '<VPNProfile>
  5.  <DnsSuffix>local.domain.tld</DnsSuffix>
  6.  <NativeProfile>
  7. <Servers>vpn.domain.tld</Servers>
  8. <NativeProtocolType>IKEv2</NativeProtocolType>
  9. <Authentication>
  10.  <UserMethod>Eap</UserMethod>
  11.  <Eap>
  12.   <Configuration>
  13. <EapHostConfig xmlns="http://www.microsoft.com/provisioning/EapHostConfig"><EapMethod><Type xmlns="http://www.microsoft.com/provisioning/EapCommon">25</Type><VendorId xmlns="http://www.microsoft.com/provisioning/EapCommon">0</VendorId><VendorType xmlns="http://www.microsoft.com/provisioning/EapCommon">0</VendorType><AuthorId xmlns="http://www.microsoft.com/provisioning/EapCommon">0</AuthorId></EapMethod><Config xmlns="http://www.microsoft.com/provisioning/EapHostConfig"><Eap xmlns="http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1"><Type>25</Type><EapType xmlns="http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV1"><ServerValidation><DisableUserPromptForServerValidation>true</DisableUserPromptForServerValidation><ServerNames>nps.local.domain.tld</ServerNames><TrustedRootCA>3d 8a 6d f8 7f 28 f9 7c 38 62 e9 eb fc 44 5a 76 f4 36 85 94 </TrustedRootCA></ServerValidation><FastReconnect>true</FastReconnect><InnerEapOptional>false</InnerEapOptional><Eap xmlns="http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1"><Type>13</Type><EapType xmlns="http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV1"><CredentialsSource><CertificateStore><SimpleCertSelection>true</SimpleCertSelection></CertificateStore></CredentialsSource><ServerValidation><DisableUserPromptForServerValidation>true</DisableUserPromptForServerValidation><ServerNames>nps.local.domain.tld</ServerNames><TrustedRootCA>3d 8a 6d f8 7f 28 f9 7c 38 62 e9 eb fc 44 5a 76 f4 36 85 94 </TrustedRootCA></ServerValidation><DifferentUsername>false</DifferentUsername><PerformServerValidation xmlns="http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV2">true</PerformServerValidation><AcceptServerName xmlns="http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV2">true</AcceptServerName></EapType></Eap><EnableQuarantineChecks>false</EnableQuarantineChecks><RequireCryptoBinding>false</RequireCryptoBinding><PeapExtensions><PerformServerValidation xmlns="http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV2">true</PerformServerValidation><AcceptServerName xmlns="http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV2">true</AcceptServerName></PeapExtensions></EapType></Eap></Config></EapHostConfig>
  14.   </Configuration>
  15.  </Eap>
  16. </Authentication>
  17. <RoutingPolicyType>SplitTunnel</RoutingPolicyType>
  18.  </NativeProfile>
  19. <AlwaysOn>true</AlwaysOn>
  20. <RememberCredentials>true</RememberCredentials>
  21. <TrustedNetworkDetection>local.domain.tld</TrustedNetworkDetection>
  22.  <DomainNameInformation>
  23. <DomainName>.local.domain.tld</DomainName>
  24. <DnsServers>192.5.10.5,192.5.10.3</DnsServers>
  25. <DnsSuffix>local.domain.tld</DnsSuffix>
  26. </DomainNameInformation>
  27. </VPNProfile>'
  28.  
  29. $ProfileXML = $ProfileXML -replace '<', '&lt;'
  30. $ProfileXML = $ProfileXML -replace '>', '&gt;'
  31. $ProfileXML = $ProfileXML -replace '"', '&quot;'
  32.  
  33. $nodeCSPURI = './Vendor/MSFT/VPNv2'
  34. $namespaceName = 'root\cimv2\mdm\dmmap'
  35. $className = 'MDM_VPNv2_01'
  36.  
  37. try
  38. {
  39. $username = Gwmi -Class Win32_ComputerSystem | select username
  40. $objuser = New-Object System.Security.Principal.NTAccount($username.username)
  41. $sid = $objuser.Translate([System.Security.Principal.SecurityIdentifier])
  42. $SidValue = $sid.Value
  43. $Message = "User SID is $SidValue."
  44. Write-Host "$Message"
  45. }
  46. catch [Exception]
  47. {
  48. $Message = "Unable to get user SID. User may be logged on over Remote Desktop: $_"
  49. Write-Host "$Message"
  50. exit
  51. }
  52.  
  53. $session = New-CimSession
  54. $options = New-Object Microsoft.Management.Infrastructure.Options.CimOperationOptions
  55. $options.SetCustomOption('PolicyPlatformContext_PrincipalContext_Type', 'PolicyPlatform_UserContext', $false)
  56. $options.SetCustomOption('PolicyPlatformContext_PrincipalContext_Id', "$SidValue", $false)
  57.  
  58.     try
  59. {
  60.     $deleteInstances = $session.EnumerateInstances($namespaceName, $className, $options)
  61.     foreach ($deleteInstance in $deleteInstances)
  62.     {
  63.         $InstanceId = $deleteInstance.InstanceID
  64.         if ("$InstanceId" -eq "$ProfileNameEscaped")
  65.         {
  66.             $session.DeleteInstance($namespaceName, $deleteInstance, $options)
  67.             $Message = "Removed $ProfileName profile $InstanceId"
  68.             Write-Host "$Message"
  69.         } else {
  70.             $Message = "Ignoring existing VPN profile $InstanceId"
  71.             Write-Host "$Message"
  72.         }
  73.     }
  74. }
  75. catch [Exception]
  76. {
  77.     $Message = "Unable to remove existing outdated instance(s) of $ProfileName profile: $_"
  78.     Write-Host "$Message"
  79.     exit
  80. }
  81.  
  82. try
  83. {
  84.     $newInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
  85.     $property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", "$nodeCSPURI", 'String', 'Key')
  86.     $newInstance.CimInstanceProperties.Add($property)
  87.     $property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", "$ProfileNameEscaped", 'String', 'Key')
  88.     $newInstance.CimInstanceProperties.Add($property)
  89.     $property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ProfileXML", "$ProfileXML", 'String', 'Property')
  90.     $newInstance.CimInstanceProperties.Add($property)
  91.     $session.CreateInstance($namespaceName, $newInstance, $options)
  92.     $Message = "Created $ProfileName profile."
  93.  
  94.     Write-Host "$Message"
  95. }
  96. catch [Exception]
  97. {
  98.     $Message = "Unable to create $ProfileName profile: $_"
  99.     Write-Host "$Message"
  100.     exit
  101. }
  102.  
  103. $Message = "Script Complete"
  104. Write-Host "$Message"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement