参照powershell案例快速学会基本用法

我一直认为学习其他语言就写小demo,然后根据需求深入学习,我也是第一次写powershell脚本,感觉写起来和php特别像,分享出来,(里面涵盖基本语法等知识)

$param = "nginx:1443,tomcat:8005,redis:6379,mysql:3307"
# $param = ""
$result = "参数为null"
If (![String]::IsNullOrEmpty($param)) {
    $newparam = $param.Split(",")
    # echo $newparam.GetType()
    $testArray = @()
    foreach ($item in $newparam) {
        $value = $item.Split(":")
        $length = $value.Count
        if ($length -eq 2) {
            $srv_name = $value[0]
            $srv_port = $value[1]
            $ret = cmd /C "netstat -ano |findstr $srv_port >nul 2>nul && echo True || echo False"
            $res = -Join ($srv_name, "=", $ret)
            # 添加到数组
            $testArray += $res
        }
        else {
            continue  
        }
    }

    $result = [system.String]::Join(",", $testArray)
}
echo $result

执行结果

参照shell案例快速学会基本用法

本作品采用《CC 协议》,转载必须注明作者和本文链接
MissYou-Coding
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
Coding Peasant @ 互联网
文章
193
粉丝
10
喜欢
60
收藏
63
排名:602
访问:1.3 万
私信
所有博文
博客标签
社区赞助商