如何在powershell脚本中传递值列表 [英] How to pass list of value in powershell script
本文介绍了如何在powershell脚本中传递值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里iam通过加载txt文件来监视cpu使用情况。其中txt文件包含机器名称列表。但我需要将数组或列表传递给脚本而不是从txt文件中读取。
$ Username ='a';
$ Password ='aa'
$ pass = ConvertTo-SecureString -AsPlainText $ Password -Force
$ Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $ Username,$ pass
Invoke-Command -ComputerName qachdc -ScriptBlock {
$ SFile =C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ = Get-Content $ SFile -ErrorAction SilentlyContinue
$ Result = @()
ForEach($ ServerList中的$ computername)
{
$ AVGProc = Get-WmiObject -computername $ computername win32_processor |
Measure-Object -property LoadPercentage -Average |选择平均值
$ OS = gwmi - 类win32_operatingsystem -computername $ computername |
Select-Object @ {Name =MemoryUsage; Expression = {{0:N2} - f(($ _。TotalVisibleMemorySize - $ _.FreePhysicalMemory)* 100)/ $ _.TotalVisibleMemorySize)}}
$ vol = Get-WmiObject -Class win32_Volume -ComputerName $ computername -FilterDriveLetter ='C:'|
选择对象@ {Name =C PercentFree;表达式= {{0:N2} - f(($ _.FreeSpace / $ _。容量)* 100)}}
$结果+ = [ PSCustomObject] @ {
ServerName =$ computername
CPULoad =$($ AVGProc.Average)%
MemLoad =$ ($ OS.MemoryUsage)%
CDrive =$($ vol.'C PercentFree')%
}
$输出报告=
$ OFS =`r`n
Foreach($结果$ $)
{
$ Outputreport + = $ OFS + $( $ Entry.Servername)+; + $($ Entry.CPULoad)+; + $($ Entry.MemLoad)+; + $($ Entry.Cdrive)
}
}
写主机$ Outputreport
} -credential $ Cred
我的尝试:
i已尝试过以上代码。但我对脚本概念不熟悉,我不知道如何传递列表或数组而不是从txt文件中读取。
解决方案
Username ='a';
密码='aa'
pass = ConvertTo-SecureString -AsPlainText
Here iam monitoring cpu usage memory by loading txt file. In which txt file contains list of machine name. But i need to pass as array or list to scripts instead of reading from txt file.
$Username = 'a'; $Password = 'aa' $pass = ConvertTo-SecureString -AsPlainText $Password -Force $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass Invoke-Command -ComputerName qachdc -ScriptBlock { $SFile = "C:\f\ServerList.txt" $ServerList = Get-Content $SFile -ErrorAction SilentlyContinue $Result = @() ForEach($computername in $ServerList) { $AVGProc = Get-WmiObject -computername $computername win32_processor | Measure-Object -property LoadPercentage -Average | Select Average $OS = gwmi -Class win32_operatingsystem -computername $computername | Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }} $vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" | Select-object @{Name = "C PercentFree"; Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) } } $result += [PSCustomObject] @{ ServerName = "$computername" CPULoad = "$($AVGProc.Average)%" MemLoad = "$($OS.MemoryUsage)%" CDrive = "$($vol.'C PercentFree')%" } $Outputreport = "" $OFS = "`r`n" Foreach($Entry in $Result) { $Outputreport += $OFS + $($Entry.Servername) + ";" + $($Entry.CPULoad) + ";" + $($Entry.MemLoad) + ";" + $($Entry.Cdrive) } } Write-Host $Outputreport } -credential $Cred
What I have tried:
i have tried above code. But iam new to scripting concepts, I dont no how to pass as list or array instead of reading from txt file.
解决方案
Username = 'a';
Password = 'aa'
pass = ConvertTo-SecureString -AsPlainText
这篇关于如何在powershell脚本中传递值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文