Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Param (
  2.     [Parameter(Mandatory = $true)][String]$InputFile, #This is where the .csv file is stored
  3.     [Parameter(Mandatory = $true)][String]$OutputFile #This is where the results file will appear.
  4. )
  5.  
  6. #Variables
  7. $ComputerName = Get-Content $InputFile -ErrorAction SilentlyContinue
  8.  
  9. ###Enter Tasks Below as Remarks###
  10.  
  11. #Test if the input file exists.
  12. if (Test-Path -path $InputFile) {
  13.  
  14.     #If the file exists continue.
  15.     #Run the ForEach command, display each one and write to a file.
  16.     Write-Host "-------------------------------------"
  17.     Write-Host "-------------------------------------"
  18.     Write-Host "-------------------------------------"
  19.     foreach ($Computer in $ComputerName) {
  20.         if (Test-Connection -ComputerName $Computer -Count 2 -Quiet) {
  21.             Try {
  22.                 Write-Host -ForegroundColor Magenta "Scanning $Computer..."
  23.                 Invoke-Command -ComputerName $Computer -ScriptBlock {
  24.                     Get-WmiObject Win32_Service | Where-Object { $_.Name -like '*CWGMonitor*' } } -ErrorAction SilentlyContinue | #This line can be amended to check for anything required.
  25.                 Select-Object PSComputerName |
  26.                 Tee-Object -FilePath $OutputFile -Append
  27.             }
  28.             Catch {
  29.                 Write-Warning "Unable to access service information from $Computer"
  30.             }
  31.         }
  32.         else {
  33.             Write-Warning "Unable to ping $Computer"
  34.             #Display when the script is complete.
  35.             Write-Host -ForegroundColor Yellow "The Script is complete, the file has been stored here: $Outputfile"
  36.         }
  37.         #If the file doesnt exist display a message and exit the script.
  38.     }
  39. }
  40. else {
  41.     Write-Host -ForegroundColor Red "Unable to locate file at location - $InputFile"
  42.     Write-Host -ForegroundColor Red "Now ending script"
  43.     Return
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement