Need to Run Exe in Server machine from Domain joined VM

Anant Bera 171 Reputation points
2023-12-14T20:03:07.14+00:00

Hello Experts

We are monitoring several Domain Controllers using the ConnectionLooger application, to get the report of the domain we need to run an exe file named Connectionlogexport after that it generates an CSV file. Since we have several DC we need to login in each DC and run the exe file to get the report as CSV,

Is there is any way by which we can run these exe files using Powershell, we have tried running the file by making the folder network shared folder in which the exe file was kept but didn't work

Thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,598 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,730 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 35,036 Reputation points
    2023-12-15T14:55:21.2666667+00:00

    If this is something that you need to run on a regular basis, like every month, then I would recommend setting up a scheduled task on each machine to run the program.

    If you only need to run this when requested, then you can use Powershell's Invoke-Command to execute the program.

    $servers = get-content -Path c:\temp\ServerList.txt     # read in the list of servers 
    
    # If the account that the script is running as does not have admin access on the server,
    # Then you will need to use a credential object in order to authenticate 
    $User = ".\admin"                              
    $PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force         # the account's password
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    
    #This is the script block that runs on each remote machine
    $sb = {
        # Run the logger program on the remote machine and save the file to its c drive. 
        c:\LogTool\Connectionlogexport.exe -output "c:\LogTool\$env:COMPUTERNAME.csv" 
    }
    
    #Process our list of servers
    foreach ($Name in $servers) {
        Invoke-Command -ComputerName $Name -ScriptBlock $sb -Credential $Credential
        $dataFile = "\\$Name\SomeSharedFolder\$Name.csv"
        if (test-path $dataFile) {  
            # We have to initiate the file copy on the server where the script is running because 
            # we can't push it with the scriptblock due to the double hop problem.  
            Copy-Item $dataFile -Destination "C:\Data\" 
            "We got the data for $name"
        } else {
            "Error, we did not find $dataFile"   
        }
    }
    

    If you have not configured remoting, then on each machine you will need to run "winrm quickconfig" to do that.

    https://www.bing.com/search?q=winrm+quickconfig

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ian Xue 38,846 Reputation points Microsoft Vendor
    2023-12-18T08:49:36.6233333+00:00

    Hello,

    About your issue, I think you can use powershell remote commands to achieve it. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command

    For example

    $DCS=Get-ADDomainController -filter * #get all the DC in the domain
    foreach ($DC in $DCS.name)
    { 
      $credential= Get-Credential
      Invoke-Command -ComputerName $DC -Credential $credential -ScriptBlock { 
       Copy-Item  //sharepath -Destination //destinationpath ; #copy the exe file to the destination DC server 
       //destinationpath.exe }
    }
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.