title: Why will PsExec not execute a .ps1 script remotely?
body: <p>I am trying to write a script in PowerShell that gathers free space on the C: drive and then appends it to a .csv file on a network drive. </p>
  
  <p>Our company does not have PowerShell enabled by default. Remote PowerShell commands are restricted. Because of this, I have to script things out, then kick them off using PsExec. </p>
  
  <p>I have two scripts for this process. The first one, is the one I run from my machine, this script asks for my credentials and then uses a foreach loop to cycle through a .txt file with IP addresses telling each IP to launch the second script from a network drive. The second script gathers the free space, performs a math function on it, then appends the hostname and free space to a .csv file on the network.</p>
  
  <p>However, the second script never runs. After the connection is established using PsExec, my console just sits there. It should kick off the script and then loop again to the next IP. I have tested the second script manually on some of the target machines with no issues.</p>
  
  <p>Locally executed script:</p>
  
  <pre><code>$username = Read-Host "Username"
  
  $outputPath = "\\network\directory\sub-directory\FileName.csv"
  $IPpath = Read-Host "Enter the path for the list of IP addresses"
  $IPs = Get-Content -Path $IPpath
  Add-Content -Path $outputPath -Value """IP"",""Free Space"""
  
  foreach ($IP in $IPs)
  {
      if (Test-Connection -Count 1 -ComputerName $IP -Quiet)
      {
          \\network\directory\subdirectory\psexec.exe -accepteula \\$IP -u domain\$username -h PowerShell -ExecutionPolicy Bypass -Command \\network\directory\sub-directory\diskSpaceCheck.ps1
      }
      else
      {
          Add-Content -Path $outputPath -Value """$IP"",""Offline"""
      }
  }
  </code></pre>
  
  <p>Remotely executed script (diskSpaceCheck.ps1):</p>
  
  <pre><code>$outputPath = "\\network\directory\sub-directory\FreeSpace.csv"
  $hostname = hostname
  
  $disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
  $freeSpace = [System.Math]::Round((($disk.FreeSpace) / 1GB))
  Add-Content -Path $outputPath -Value "$($hostname), $($freeSpace)"
  </code></pre>
  
  <p>I don't think you can have an if statement inside a foreach also but I've tried it without the if test and got the same results. I can't figure out why the remote machine won't execute this script. I have another script that is copied to these machines and then executed in the same manner that works correctly.</p>
  
  <p>Also, the target machines are locked down to limit their use for only specific purposes. These machines use a generic auto logon and generally have access to a few applications. Everything else is inaccessible, including Windows Explorer, Control Panel, etc. I tested this script on some of the machines local to my office, and it worked fine.</p>
  
  <p>I was thinking maybe a networking latency issue? Can that impact PsExec or .ps1 scripts? I can remote onto the machine, log out, log in with my credentials and run the script while PsExec just sits at running the command I gave it.</p>
  
  <p>Any advice would be appreciated.</p>