what is wrong with this Start-Job command? - powershell

Edit: Ok, that's weird. Maybe I just screwed up my testing. The command that I indicated does work. What doesn't is using it within a Foreach loop within the ISE (version 4). Within a regular Powershell console, it also does work.
foreach ($computer in #("2008R2-test","anotherServer")) { Start-Job -name $computer -ScriptBlock { Invoke-Command -computerName $computer -scriptBlock {$?} } }
The job never stops running, never is "completed". I don't get the cursor back. It hangs and I have to kill the ISE with task manager to get it back. The "$?" is just an example of running anything. I'm trying to do it this way because I want to run some commands that will display stuff to the screen, then do stuff that can happen in the background including a command that runs at the remote machine. I guess it's just another case of not being able to use the ISE.

Related

PowerShell Enter Session find path bug

I have some automation scripts, but I had to split them down because of what appears to be an interesting bug. I've stripped it to its simplest form below:
Enter-PSSession [SERVER]
cd D:\
If I run the above in one go, I get the below error
cd : Cannot find drive. A drive with the name 'D' does not exist.
However, if I run the lines individually, they run fine. I have tried putting a sleep in for a second, a pause line, but still no luck. Is anyone else aware of this, and the way around it?
Use Invoke-Command instead of enter-pssession.
Example:
$ReturnValue = Invoke-Command -ComputerName $Server -ScriptBlock{
Set-Location D:
# DO STUFF
Return $ReturnValue # Return your stuff
}

Difference between - command and ScriptBlock Powershell

ps1 is on remote machine . I am running below command from some other mahcine.
Is there any difference between using both below script ---
invoke-command -computer $MachineName -command { C:\hello.ps1 }
invoke-command -computer $MachineName -scriptblock{ C:\hello.ps1 }
Also, I am going to use for loop for multiple machine having same script name but having diff sequence of work that is located on each remote machine only . Want to understand the execution for second machine will go only if first get completed . Correct ?
Difference between -command and -scriptblock
There is no difference in execution. -commandis merely an alias for scriptblock. You can verify this by getting the parameter info for the invoke-command command
(Get-Command -Name Invoke-Command).Parameters.Values | select name, aliases
Sequential execution
Yes, the execution is sequential. The command you specify will execute on the second machine after the command has completed on the first machine.
According to the help
These commands run synchronously (one at a time). When the commands
complete, the output of the commands from all of the computers is
saved in the $version variable. The output includes the name of the
computer from which the data originated.
To answer your second question: Invoke-Command works in parallel. It runs what ever is mentioned in the script block to all the machines in parallel. By default, powershell will talk upto 32 computers at once. If you specify more than that, it will queue them up so that as one computer completes, the next one in line will begin. However, I believe, you can increase that number by specifying the -throttlelimit parameter of the Invoke-Command.

Executing invoke-item remotely in PowerShell

I am trying to execute this code willing to execute the program server.exe in the remote machine NODO-R01.Command completes, but nothing happens:
Invoke-Command -ComputerName NODO-R01 {Invoke-item C:\server.exe}
If I use any other *-Item cmdlet it works; like deleting, renaming, etc.

Start a program or filepath remotely

With XP machines and eventually win7 machines. I am trying to find a way to start a program remotely from the commandline or even powershell if possible. Right now we can kill tasks using the "taskkill" command, but there doesn't seem to be an easy way to start them without extra programs. I want to be able to do it without deploying anything. I tried that Psexec but that didnt work.
Invoke-Command -ComputerName server01 -ScriptBlock { yourprogram.exe }
Check out technet:
The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. With a single Invoke-Command command, you can run commands on multiple computers.
http://technet.microsoft.com/en-us/library/hh849719.aspx

Powershell's Invoke-Command not working when called from Windows Task Scheduler

I have a Powershell script, which is working just fine, when called from command line, but only partially fine when executed by the Windows Task Scheduler. The script looks as follows:
# Do things, which are always working.
$session = new-pssession -computername SRV
Invoke-Command -session $session -scriptblock { D:\script.bat }
# Do things, which are always working.
The task defined in the Task Scheduler is completed without errors. As you see, all parts before and after Invoke-Command are working, also when called by the Task Scheduler. Only the Invoke-Command itself is only working when called from the command line.
My only guess is, that the Powershell script is exiting prematurely, but I didn't find any way to confirm this or even solve the issue.
it is likely to be a permissions issue - as a test, run the task using the same credentials as when it is run at the command line...
I had the same issue with Invoke-Command when I called a script from Task Scheduler to run certain scriptblocks on multiple computers. I don't know why, but setting the "Start in" folder of the action to the folder of the script solved it.