Pages - Menu

2019年9月30日 星期一

[PowerShell]多參數傳遞

前言

先前寫了一個定時檢查server 硬碟空間大小的程式,但沒有寫目前的使用量幾%
今天要補上的時候,才發現Powershell的多參數傳遞方式跟別人不一樣。




正文

直接用程式碼解釋一切

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Function line_notify($Disk,$Name,$Percent)
{
     $msg = $Name +' Server : ' + $Disk + ' 使用比例已達' + "{0:N0} %" -f $Percent
     $Header = @{
         'Authorization' = 'Bearer 123456456
     }
     $postParams = @{
         'message' =$msg
     }
    
     Invoke-WebRequest -Uri https://notify-api.line.me/api/notify -Method POST -ContentType 'application/x-www-form-urlencoded' -Headers $Header -Body $postParams 
}
$ServerName = Get-SilComputer
line_notify("C")($ServerName.Name)($Percent)

一般使用  , (逗點) ,分隔參數
但在PS裡面,(逗點)表示數組的運算,所以會當成同一個參數
ref.  How do I pass multiple parameters into a function in PowerShell?

沒有留言:

張貼留言