Different results when running PowerShell script from share with psexec included

Welcome Forums General PowerShell Q&A Different results when running PowerShell script from share with psexec included

This topic contains 1 reply, has 1 voice, and was last updated by

 
Participant
3 years, 5 months ago.

  • Author
    Posts
  • #32832

    Participant
    Topics: 31
    Replies: 42
    Points: 45
    Rank: Member

    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

    Participant
    Topics: 31
    Replies: 42
    Points: 45
    Rank: Member

    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!

The topic ‘Different results when running PowerShell script from share with psexec included’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort