Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 7-Feb-17 3:39am

1 solution

You can pass them on the command line:
myscriptname "server1" "server2" "server3"

Then remove the Get-Content line and replace the foreach loop reading from the file by
foreach ($computername in $args)
{
  # Do the work here
}


If the cmdlet is called from another cmdlet where the list is stored in an array you can execute it as:
& myscriptname @ServerList
or
Invoke-Expression "myscriptname $ServerList"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900