PowerShell 脚本为所有用户创建 VPN 连接

PowerShell 脚本为所有用户创建 VPN 连接

我有以下 PowerShell 脚本,它可以创建 VPN 连接和到 Rasphone 的桌面快捷方式。

#
# Powershell script to create a client VPN connection to a Meraki MX.
#
# Configuration Parameters
$ProfileName = 'P*** VPN'
$DnsSuffix = 'int.nomoist.net'
$ServerAddress = 'cisco-******-*****rdvvm.dynamic-m.com'
$L2tpPsk = 'Mypassword'

#
# Build client VPN profile
# https://docs.microsoft.com/en-us/windows/client-management/mdm/vpnv2-csp
#

# Define VPN Profile XML
$ProfileNameEscaped = $ProfileName -replace ' ', '%20'
$ProfileXML =
    '<VPNProfile>
        <RememberCredentials>false</RememberCredentials>
        <DnsSuffix>'+$dnsSuffix+'</DnsSuffix>
        <NativeProfile>
            <Servers>' + $ServerAddress + '</Servers>
            <RoutingPolicyType>SplitTunnel</RoutingPolicyType>
            <NativeProtocolType>l2tp</NativeProtocolType>
            <L2tpPsk>'+$L2tpPsk+'</L2tpPsk>
        </NativeProfile>
'

# Routes to include in the VPN
$ProfileXML += "  <Route><Address>10.69.11.0</Address><PrefixSize>24</PrefixSize><ExclusionRoute>false</ExclusionRoute></Route>`n"

$ProfileXML += '</VPNProfile>'

# Convert ProfileXML to Escaped Format
$ProfileXML = $ProfileXML -replace '<', '&lt;'
$ProfileXML = $ProfileXML -replace '>', '&gt;'
$ProfileXML = $ProfileXML -replace '"', '&quot;'

# Define WMI-to-CSP Bridge Properties
$nodeCSPURI = './Vendor/MSFT/VPNv2'
$namespaceName = 'root\cimv2\mdm\dmmap'
$className = 'MDM_VPNv2_01'

# Define WMI Session
$session = New-CimSession

# Detect and Delete Previous VPN Profile
try
{
    $deleteInstances = $session.EnumerateInstances($namespaceName, $className, $options)
    foreach ($deleteInstance in $deleteInstances)
    {
        $InstanceId = $deleteInstance.InstanceID
        if ("$InstanceId" -eq "$ProfileNameEscaped")
        {           $session.DeleteInstance($namespaceName, $deleteInstance, $options)
            Write-Host "Removed '$ProfileName' profile"
        }
    }
}
catch [Exception]
{
    Write-Host "Unable to remove existing outdated instance(s) of $ProfileName profile: $_"
    exit
}

#
# Create VPN Profile
#

try
{
    $newInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
    $property = [Microsoft.Management.Infrastructure.CimProperty]::Create('ParentID', "$nodeCSPURI", 'String', 'Key')
    $newInstance.CimInstanceProperties.Add($property)
    $property = [Microsoft.Management.Infrastructure.CimProperty]::Create('InstanceID', "$ProfileNameEscaped", 'String', 'Key')
    $newInstance.CimInstanceProperties.Add($property)
    $property = [Microsoft.Management.Infrastructure.CimProperty]::Create('ProfileXML', "$ProfileXML", 'String', 'Property')
    $newInstance.CimInstanceProperties.Add($property)

    $session.CreateInstance($namespaceName, $newInstance, $options) | Out-Null
    Write-Host "Created '$ProfileName' profile."
}
catch [Exception]
{
    Write-Host "Unable to create $ProfileName profile: $_"
    exit
}

# Create a desktop shortcut
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut("$env:Public\Desktop\Polygon VPN.lnk")
$ShortCut.IconLocation = "C:\WINDOWS\system32\SHELL32.dll, 135"
$Shortcut.TargetPath = "rasphone.exe"
$Shortcut.Save()

问题是,当我运行它时,它确实为所有用户创建了快捷方式,但仅为当前会话创建了 VPN 配置文件。我希望为所有用户创建此 VPN 配置文件。

谢谢。

相关内容