kyb
kyb

Reputation: 8201

Could powershell text output be passed to powershell cmdlet

Consider next powershell dialog:

> Get-VpnConnection -AllUserConnection

Name                  : VPN-StackoverflowQuestion
ServerAddress         : x.x.x.4
AllUserConnection     : True
Guid                  : {XXX2C9DE-58F0-41E4-XXXX-9809E90DCXXX}
TunnelType            : Pptp
AuthenticationMethod  : {Chap, MsChapv2, Pap}
EncryptionLevel       : Optional
L2tpIPsecAuth         :
UseWinlogonCredential : False
EapConfigXmlStream    :
ConnectionStatus      : Disconnected
RememberCredential    : False
SplitTunneling        : False
DnsSuffix             :
IdleDisconnectSeconds : 0

Such serialization looks nice and readable and editable. I can save it in a text file (dictionary) with

Get-VpnConnection > VPN-StackoverflowQuestion.dic

Now I want to pass this text file as args to Add-VpnConnection. Possible?

I know about Export-Clixml, it's result file is unreadable, uneditable and very cumbersome.

Upvotes: 0

Views: 438

Answers (2)

saftargholi
saftargholi

Reputation: 980

yes use this command :

Get-VpnConnection -AllUserConnection |out-string | out-file "c:\path\filename.txt"

and u can parse this to return value .

another way :

 Get-VpnConnection -AllUserConnection |out-string | out-file "c:\path\filename.csv"

then you can return it into powershell

check this out i set my vpn like this :

$vpn = Get-VpnConnection
$vpn
$splitter = "SpliTtEr" #write custom string that you think unique 
$CustomStructure = $vpn.Name+$splitter+$vpn.ServerAddress+$splitter+$vpn.AllUserConnection+$SpliTtEr+$vpn.Guid ...


$CustomStructure | Out-File C:\path\filename.txt


$getcontent = Get-Content -Path C:\path\filename.txt


$items = $getcontent -split $splitter
Set-VpnConnection -Name $items[0] -ServerAddress $items[1] ...

Upvotes: 1

autosvet
autosvet

Reputation: 880

The result is an object, export and save the object (CliXML, json) and when you need it convert the exported document into object again.

Upvotes: 0

Related Questions