0

I need to Run this PowerShell script in different servers remotely with credentials & collect data in single csv and send to email. i Include the Invoke Command, but getting error.

$servers = Get-Content "c:\serverlist\servers.txt"
$Report = foreach ($server in $servers){
    Invoke-Command -ComputerName $server -ScriptBlock
    }
    {

$props = @(
    @{
        N='ServerName'
        E={ $env:COMPUTERNAME }
    }
    'StartDate'
    'EndDate'
    'ConnectorName'
    'RunProfileName'
    'StepResult'
)

$profiles = @(
    'Delta Import'
    'Delta Synchronization'
    'Full Import'
    'Full Synchronization'
) -join '|'

Get-ADSyncToolsRunstepHistory | Group-Object RunProfileName |
    Where-Object Name -Match $profiles | ForEach-Object {

        $_.Group | Select-Object -First 2
    } }  | Select-Object $props | Export-Csv c:\serverlist\export.csv -NoTypeInformation
hacrks
  • 19
  • 5
  • Does this answer your question: https://stackoverflow.com/questions/74562151/how-to-combining-powershell-output-from-multiple-server-to-single-csv (i.e. `Invoke-Command`). – Bitcoin Murderous Maniac Dec 19 '22 at 16:08
  • You need to use ANY : Where-Object { $name = $_.Name; [Linq.Enumerable]::Any([string[]]$profiles,[Func[object,bool]] {param($x) $name = $x })} – jdweng Dec 19 '22 at 16:09
  • I tried with Invoke-Command but getting error, I update the code in Question. – hacrks Dec 20 '22 at 15:29
  • What's within the `-scriptblock { }` curly brackets should be the logic to run on each machine. You have a blank scriptblock from what I see. Also for the `-ComputerName $server` just have that be an array of the machines without any header column just an array of machine name outside of the `foreach()` logic. Just run the logic as you'd run it on a single local machine within the script block. Just make array with machine names `$m = "machine1","mchine2","m3"` and then pass that as `Invoke-Command -ComputerName $m -scriptblock {}` – Bitcoin Murderous Maniac Dec 24 '22 at 16:38
  • You might also play with the `-AsJob` parameter passing it off to the machine to finish up and just submitting it for execution on the machine to run and moving on to make it faster. I don't think you need the `foreach()` but if you need values from csv to pass arguments for values, `invoke-command` also has a `-ArgumentList` to pass arguments as well as the `-asjob` I mentioned. If `Get-ADSyncToolsRunstepHistory` but into ADConnect or is that a github tool to run on ADConnect servers? I can test if I had the module on an ADConnect server I maintain. – Bitcoin Murderous Maniac Dec 24 '22 at 16:41
  • `$props = @( @{ N='ServerName' E={ $env:COMPUTERNAME } } 'StartDate' 'EndDate' 'ConnectorName' 'RunProfileName' 'StepResult' ) $profiles = @( 'Delta Import' 'Delta Synchronization' 'Full Import' 'Full Synchronization' ) -join '|' Get-ADSyncToolsRunstepHistory | Group-Object RunProfileName | Where-Object Name -Match $profiles | ForEach-Object { $_.Group | Select-Object -First 2 } | Select-Object $props | Export-Csv c:\serverlist\export.csv -NoTypeInformation` – hacrks Dec 26 '22 at 14:50
  • Hi @BitcoinMurderousManiac, the Above comment code will go inside the script block, this script help to pull the recent sync cycle data. i never used before `-Asjob`.This script should run in 4 different servers and get the data in single csv file. this will run in ADconnect server we have a environment of prod, test and DR. you can test this code in your environment, help me where to incudle `-ASJob' in this script. – hacrks Dec 26 '22 at 15:03
  • I believe in this case, you will not want to use the `-asjob` parameter then. I'm not sure where you are getting `Get-ADSyncToolsRunstepHistory` command because on the ADConnect servers I run, that is not a valid command. I can only test what I gave so help me understand what tool that is, where you got it from, then I'll look it over and potentially run it to test for multiple servers with similar logic as you provided. – Bitcoin Murderous Maniac Dec 28 '22 at 16:05

1 Answers1

0

I followed the steps to Send the date of the csv file to the email In PowerShell ISE I have created a file and replaced the code belowenter image description here

the other method is a powerShell

    $CSVFile ="C:\Users\Public\Downloads\users.csv"
$userList = import-csv $CSVFile
$subject = " Here is your Details"
$from ="yourmail"
$passwrod ="your password"|convertTo-secureString -asPlainText -force
$creds = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList $from ,$passwrod


foreach ($user in $userList){
$body = "your Login Id is" +$user.loginname
$body += "your password is" + $user.Passwrod
$body +="your email id is " + $user.mail
 


Send-MailMessage -From $from $user.mail -Subject $subject -body $body - SmtpServer "pujarisampath80@gmail.com" -UseSs1 -port 587 -Credential $creds
}
  • I have created csv file In Excel With .csv extension enter image description here

Provide your Email address and password and click on Run and check the mail

enter image description here

Naveen Sharma
  • 349
  • 2
  • 4