让我们用PowerShell和Psgraph建立一些图形。
我喜欢想象的东西。只有一些模式在数据表中可以在可视化时跳出来。今天,我将与我的新模块Psgraph一起使用。
#指数
什么是psgraph.
它是一组辅助函数,可以轻松生成所使用的点文件 图形viz. 发动机制作图表。它们处理语言规范,以便您可以制作很棒的图表。
在我们开始之前快速的图表
看看这个例子。
图形 basic {
edge -From start -To middle
edge -From middle -To end
}
We define that we are creating a graph. We draw edges between the nodes start
,middle
and end
in that order.
安装psgraph.
我将此模块发布到PowerShell Gallery,以使其易于入门。只有一个其他依赖性并且是graphviz。这是我的方式 开始.
# Install GraphViz from the Chocolatey repo
Find-Package graphviz | Install-Package -ForceBootstrap
# Install PSGraph from the Powershell Gallery
Find-Module PSGraph | Install-Module
# Import Module
Import-Module PSGraph
介绍新命令
我创建了一些新命令,使这些图形易于生成。以下是开始的基础。
A 图形
defines a new graph for us. A 节点
is an individual object on the graph. An 边缘
is a connection between two nodes on the graph. Then 出口-Psgraph.
turns our graph into an image.
图形 g @{rankdir='LR'} {
node a @{label='Node'}
node b @{label='Node'}
edge -from a -to b @{label='Edge'}
} | Export-PSGraph -ShowGraph
All three command allow you to specify a [hashtable]
to set various properties. I tell the graph to render left to right. I also give the nodes and the edge a label. Using a label for a node a allows you to use a shortname as a reference but still display a more verbose name in the final graph
示例:项目流程
让我映射一些真实的东西,所以你更好地了解这些命令可以做的事情。这是我的项目工作流程。
图形 g {
node -default @{shape='rectangle'}
node git @{label="Local git repo";shape='folder'}
node github @{label="GitHub..com \\master"}
edge git,github,AppVeyor.com,PowershellGallery.com
edge github -to ReadTheDocs.com
} | Export-PSGraph -ShowGraph
In this graph I set the default node shape to rectangle
. The next thing I do is define 2 new nodes. I give both of them a new label
so I can use a short name in my graph definition. Then I create all the edges to all the nodes. If an 边缘
discovers a new node, it will get created automatically with the default attributes.
I also gave the first 边缘
command a list of nodes. Those nodes are linked in order from first to last.
示例:更详细的项目流程
这是一个第二个例子,其中我添加了一些节点并使用更多属性。
图形 g {
node -default @{shape='rectangle'}
node git @{label="Local git repo";shape='folder'}
node github @{label="GitHub..com \\master";style='filled'}
edge VSCode -to git @{label='git commit'}
edge git -To github @{label='git push'}
edge github -To AppVeyor.com,ReadTheDocs.com @{label='trigger';style='dotted'}
edge AppVeyor.com -to PowershellGallery.com @{label='build/publish'}
edge ReadTheDocs.com -to psgraph.readthedocs.io @{label='publish'}
} | Export-PSGraph -ShowGraph
您可以完全访问所有边缘,节点和图形属性 DOT语言规格 allows.
脚本图形
这是手工图形的一件事。如果这就是您所做的,您可能会发现使用本机点语言更容易。当我们脚本图表时,真正的乐趣开始。这是我写这个模块的整个原因。
示例:服务器场
想象一下,您想要动态地将服务器场图。我要动态自动生成一些服务器名称,但可以从环境中拔出这些服务器名称。
# Server counts
$WebServerCount = 2
$APIServerCount = 2
$DatabaseServerCount = 2
# Server lists
$WebServer = 1..$WebServerCount | % {"Web_$_"}
$APIServer = 1..$APIServerCount | % {"API_$_"}
$DatabaseServer = 1..$DatabaseServerCount | % {"DB_$_"}
使用该名称列表,我们现在可以快速生成这样的图形。
图形 servers {
node -Default @{shape='box'}
edge LoadBalancer -To $WebServer
edge $WebServer -To $APIServer
edge $APIServer -To AvailabilityGroup
edge AvailabilityGroup -To $DatabaseServer
} | Export-PSGraph -ShowGraph
我真的很喜欢那种看起来的清洁。如果您在每个级别的服务器数量中播放,则图形将适应更改。因为我可以为边缘命令的每个部分提供阵列,所以我可以描述更高级别的关系。边缘命令自动地在各个节点之间备忘录逻辑。
示例:结构化数据
列表之间的绘图边缘是一个很好的开始,但我们经常有结构化的数据。导入员工CSV并映射org图表是一个经典用例。
对于这个例子,我们将走一个文件夹。因为图形的正文只是一个脚本块,我们可以在线PowerShell中使用更复杂的图形。
$folders = Get-ChildItem C:\workspace\PSGraph -Directory -Recurse
graph g @{rankdir='LR'} {
node -Default @{shape='folder'}
$folders | ForEach-Object{ node $_.FullName @{label=$_.Name} }
$folders | ForEach-Object{ edge (Split-Path $_.FullName) -To $_.FullName }
} | Export-PSGraph -ShowGraph
I first enumerate each node to give it a label. I want to see a short name in the graph, but I want to use the FullName
as the ID of the node. This way I can have 2 folders with the same name and not mess up the graph.
下一步是什么?
我有一整套文档和榜样发布 psgraph.readthedocs.io. 而且源也发布了 //github.com/KevinMarquette/PSGraph。我将更多的命令更详细地走,谈论我在这里没有覆盖的其他命令。这些命令有很多深度,但这绰绰有余,可以让你开始做好酷东西。