locked
how to run script by joining two text files RRS feed

  • Question

  • Hi Team,

    I am trying to join get-service script with my existing health check script  for remote computers to get date,cpu usage,memory usage and free space

    $data = get-content c:\scripts\comps-services.txt
    foreach($line in $data) {
    $computer = $line.split(":")[0]
    $ServiceName = $line.split(":")[1,2,3]
    Get-Service -ComputerName $Computer -Name $ServiceName | select name,status 
     }

    Please help me to join both scripts for one output

    Regards

    Ravi Kumar B

    Thursday, August 3, 2017 8:05 PM

Answers

  • Here is a quick example of what of need to learn.

    $computers = Get-Content C:\scripts\mulity.txt
    ForEach ($computername in $computers) {
    	$AVGProc = Get-WmiObject  win32_processor -computername $computername |
    		Measure-Object -property LoadPercentage -Average | Select-Object Average
    	$OS = Get-WmiObject win32_operatingsystem -computername $computername
    	$vol = Get-WmiObject win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'"
    	[pscustomobject]@{
    		ComputerName = $computername
    		TotalAverage = $AVGProc.Average
    		MemoryUsage =  '{0:N2}' -f ((($os.TotalVisibleMemorySize - $os.FreePhysicalMemory) * 100)/ $os.TotalVisibleMemorySize)
    		'C PercentFree' = '{0:N2}' -f ($vol.FreeSpace / $vol.Capacity * 100)
        }
    }
    
    There is no way the code you posted ever worked even once.  You cannot use PowerShell without learning PowerShell.


    \_(ツ)_/


    • Edited by jrv Thursday, August 31, 2017 5:25 PM
    • Marked as answer by Ravi Kumar Bandari Thursday, August 31, 2017 5:37 PM
    Thursday, August 31, 2017 5:25 PM

All replies

  • We cannot rewrite scripts for you. You need to ask a specific question. What you have posted is too vague.

    To combine multiple queries into one object search for blogs and examples that show you how to do this.  Search for psobject and pscustomobject for examples.


    \_(ツ)_/

    Thursday, August 3, 2017 8:16 PM
  • Hi 

    I worked on psobject  and pscustomobject and written below script, here I  have combined multiple cmdlets but it is working fine only on my pc, where as when i am trying to get information for multiple remote servers, throwing an error "already exist"  Kindly help  me how to make this script work for multiple remote servers.

    $object = New-Object –TypeName PSObject

    $computer = (Get-Content "C:\scripts\mulity.txt")
    ForEach($computername in $computer) {
    $AVGProc = Get-WmiObject -computername $computer win32_processor |  
    Measure-Object -property LoadPercentage -Average | Select Average 
    $OS = gwmi -Class win32_operatingsystem -computername $computer | 
    Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}  
    $vol = Get-WmiObject -Class win32_Volume -ComputerName $computer -Filter "DriveLetter = 'C:'" | 
    Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } } 
    $object | Add-Member –MemberType NoteProperty –Name computer –Value $computername 
    $object | Add-Member –MemberType NoteProperty –Name TotalAverage –Value $AVGProc.Average
    $object | Add-Member –MemberType NoteProperty –Name MemoryUsage –Value $OS.MemoryUsage
    $object | Add-Member –MemberType NoteProperty –Name "C PercentFree" –Value $vol."C PercentFree"}

    Write-Output $object 

     
    Thursday, August 31, 2017 5:07 PM
  • I recommend starting by correctly indenting and formatting your code so that it is readable.

    It appears you have just pasted together some code you found on the Internet.

    When learning PowerShell you should not try to use or modify code that you do not understand.  Start by learning basic PowerShell.

    Just saying the code "throws an error" is not in anyway helpful.  Read the complete error and try to understand what it is telling you;

    The first thing anyone using PowerShell has to learn is how to use help and how to read an error message.

    You are not using a "PsCustomObject"  You are just creating a PsObject and adding to it.  This is an old and faulty method.

    [pscustomobject]@{
          Name1=$value1
          Name2=$value2
          ...
    }


    \_(ツ)_/


    • Edited by jrv Thursday, August 31, 2017 5:16 PM
    Thursday, August 31, 2017 5:13 PM
  • Here is a quick example of what of need to learn.

    $computers = Get-Content C:\scripts\mulity.txt
    ForEach ($computername in $computers) {
    	$AVGProc = Get-WmiObject  win32_processor -computername $computername |
    		Measure-Object -property LoadPercentage -Average | Select-Object Average
    	$OS = Get-WmiObject win32_operatingsystem -computername $computername
    	$vol = Get-WmiObject win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'"
    	[pscustomobject]@{
    		ComputerName = $computername
    		TotalAverage = $AVGProc.Average
    		MemoryUsage =  '{0:N2}' -f ((($os.TotalVisibleMemorySize - $os.FreePhysicalMemory) * 100)/ $os.TotalVisibleMemorySize)
    		'C PercentFree' = '{0:N2}' -f ($vol.FreeSpace / $vol.Capacity * 100)
        }
    }
    
    There is no way the code you posted ever worked even once.  You cannot use PowerShell without learning PowerShell.


    \_(ツ)_/


    • Edited by jrv Thursday, August 31, 2017 5:25 PM
    • Marked as answer by Ravi Kumar Bandari Thursday, August 31, 2017 5:37 PM
    Thursday, August 31, 2017 5:25 PM
  • Thank You Very much for correcting my script it's working as i expected. 

    The code which I posted, since couple of days i am working on it in different ways and the code is working for me but not working as expected.

    Thanks for the inputs.


    Thursday, August 31, 2017 5:47 PM