powershell文本输出是否可以传递给powershell cmdlet

时间:2016-11-01 09:24:20

标签: xml powershell serialization pipe deserialization

考虑下一个powershell对话框:

> 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

这种序列化看起来很好,可读性和可编辑性。我可以使用

将其保存在文本文件(字典)中
Get-VpnConnection > VPN-StackoverflowQuestion.dic

现在我想将此文本文件作为args传递给Add-VpnConnection。可能的?

我知道Export-Clixml,它的结果文件不可读,不可编辑且非常繁琐。

2 个答案:

答案 0 :(得分:1)

是使用此命令:

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

你可以解析它以返回值。

另一种方式:

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

然后你可以把它归还给powershell

检查一下我设置我的vpn是这样的:

$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] ...

答案 1 :(得分:0)

结果是一个对象,导出并保存对象(CliXML,json),并在需要时将导出的文档再次转换为对象。

相关问题