Different results when running PowerShell script from share with psexec included

This topic contains 1 reply, has 1 voice, and was last updated by  Jeff 2 years ago.

  • Author
    Posts
  • #32832

    Jeff
    Participant

    I have created a form to run ipconfig /all on a remote machine via PSEXEC (don't have remoting enabled in our environment, so I can't invoke a command). When I run PowerShell script/form locally, the output of the ipconfig /all command shows up in my output box in the form without a problem. However, when I run the script from a share, it quickly opens a cmd window and runs the ipconfig /all command and quickly goes away (does not show output in the output box like it does when run local on the machine). Here is my code (below). The $Output variable shows the output of the command in my output box in the form.

    $ButtonAction = $btn8.add_click({
    
        If (!($TextBox.Text)){$TextBox.Text = $env:COMPUTERNAME}
    
        If($TextBox.Text -like "*,*"){[array]$TextBoxData = $TextBox.Text.Trim() -split "," -replace '\s',''}Else{$TextBoxData = $TextBox.Text.Trim() -replace '\s',''}
    
        foreach ($Computer in $TextBoxData) {
            
            $Output = "Current Computer: $Computer`n" | Out-String
            $OutputBox.AppendText($Output)
    
            if(Test-Connection -ComputerName $Computer -Count 2 -Quiet) {
                
                $Output = "Running ipconfig /all on $Computer `n" | Out-String
                $OutputBox.AppendText($Output)
    
                Try {
                    If (Test-Path \\SERVER\Scripts\Development\Get-ComputerInfo\psexec.exe -ea stop) {
                        $ipconfig = New-Object System.Diagnostics.ProcessStartInfo
                        $ipconfig.FileName = "\\SERVER\Scripts\Development\Get-ComputerInfo\psexec.exe"
                        $ipconfig.RedirectStandardError = $true
                        $ipconfig.RedirectStandardOutput = $true
                        $ipconfig.UseShellExecute = $false
                        $ipconfig.Arguments = "\\$Computer ipconfig /all"
                        $p = New-Object System.Diagnostics.Process
                        $p.StartInfo = $ipconfig
                        $p.Start() | Out-Null
                        $p.WaitForExit()
                        $stdout = $p.StandardOutput.ReadToEnd()
                        $stderr = $p.StandardError.ReadToEnd()
                        [string]$Output = "$stdout `n"
                        $OutputBox.AppendText($Output)
                    }
                    Else {
                         $Output = "Cannot connect to share that includes psexec. `n" | Out-String
                         $OutputBox.AppendText($Output)
                    }
                         
                }
                Catch {
                      $Output = "Error: $($_.Exception.Message)`r`n" | Out-String
                      $OutputBox.AppendText($Output)
                      [string]$Output = "$stderr `n"
                      $OutputBox.AppendText($Output)
                }
            }
            Else {
            $Output = "$Computer is currently offline.`r`n" | Out-String
            $OutputBox.AppendText($Output)
            }
        }
    })
  • #32837

    Jeff
    Participant

    Think I figured it out. The issue wasn't running the script remotely from a share. The output would not show up when clicking the button on the form for "ipconfig /all" when using the local machine. psexec doesn't like using the local machine so I kept the psexec part for remote macahines and if the machine is $env:COMPUTERNAME, then it just starts the process for ipconfig.exe. Good to go!

You must be logged in to reply to this topic.