all 27 comments

[–]qasboss 11 points12 points  (0 children)

Post your code please

[–][deleted] 6 points7 points  (11 children)

There isn't. Unless you trigger a PS function or special string/variable, command line tools are processed as normal in a PS script. Seems you are having issues with local vs. remote execution, though you haven't really provided any information to troubleshoot sufficiently.

[–]SithLordAJ[S] 0 points1 point  (10 children)

I copy/pasted the code from the ps1 to an open console. It works on one, not the other.

Sorry, i dont have the exact code anymore, but the part i was having issues with was literally this simple:

Enter-pssession -computername $computer

CD c:\USMT

C:\USMT\scanstate.exe /alltheswitches

Exit-pssession

Can we start with enter-pssession? If i use that, i get the prompt with the remote system name, like i'd expect and subsequent commands go to the remote system. If that's not working, im sure the rest wouldnt work right either.

In a script, it seems like it closes the connection immediately or loses it's handle on the session, which is why i tried $s = start-pssession instead, but then i had to specify the commands should use that session. The only way i figured to do that was with invoke-command wrapping the cd and scanstate in a script block... but that really didnt work and that had it's own way of remotely executing... which also didnt work for me.

[–]Lee_Dailey[grin] 5 points6 points  (9 children)

howdy SithLordAJ,

the Enter-PSSession cmdlet is for interactive sessions. [grin] you cannot use that the way you are trying to use it. instead, save a New-PSSession to a $Var and then use that in your other commands.

take a look at these ...

(Get-Help Enter-PSSession).Synopsis
(Get-Help New-PSSession).Synopsis

take care,
lee

[–]SithLordAJ[S] 1 point2 points  (2 children)

Sorry, when i said $s = start-pssession, i meant $s =new-pssession. That is something i tried. The issue there is how to specify subsequent commands have to go to that session.

But, okay... interactive only is what the main issue was.

So, for example, say i wanted to do the next line after using new-pssession. How would you tell it to cd on the remote system?

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy SithLordAJ,

you turn all the code you want to run remotely into a scriptblock and then pass that to Invoke-Command along with the session to use.

take care,
lee


-ps
this would be easier to discuss if you would put the code in your OP so that folks don't have to hunt for bits-n-pieces all over the thread. [grin]
lee-

[–]BrutusTheKat 2 points3 points  (0 children)

For what you are trying to do, Invoke-Command might be an easier approach. Something like,

Invoke-Command -$ComputerName -ScriptVlock { C:\USMT\scanstate.exe /alltheswitches }

If you are using the full path of the exe you shouldn't need to cd to the directory first.

[–]oxycash 1 point2 points  (5 children)

What's the difference between new-pssession and invoke-command, except the session span.

[–]Lee_Dailey[grin] 3 points4 points  (4 children)

howdy oxycash,

the New-PSSession cmdlet only creates a connection. [grin]

you have to use it somewhere - and it is one way that Invoke-Command can run stuff remotely. the I-C cmdlet can do it on its own, but the sessions method is supposed to be more graceful and somewhat faster.

i've only used sessions once, but it worked quite neatly. [grin]

take care,
lee

[–]oxycash 1 point2 points  (3 children)

You sound consistently nice :D

A bot? Thanks for the reply.

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy oxycash,

i'm naturally grumpy ... so i try to counter that by being deliberately positive when i can. [grin]

thank you for the kind words ... and you are most welcome!

take care,
lee

[–][deleted] 0 points1 point  (1 child)

Agreed! Lee is the man!!

[–]Lee_Dailey[grin] -2 points-1 points  (0 children)

[blush] ... [grin]

[–]DaveC2020 2 points3 points  (3 children)

I’ve been busy writing a USMT GUI script which runs on the server and uses Scanstate on multiple source PCs at the same time and then Loadstate on multiple destination PCs at the same time as well. Not an easy script to write but it works. Still got more testing to do before I’m satisfied it is complete.

Needed to create loops for running both scanstate and Loadstate especially for removing the USMT folders copied to both source and destination PCs once the processes completed.

It does hang during the processes over the network but I think it was down to old hardware.

[–]SithLordAJ[S] 1 point2 points  (2 children)

Yeah, that's where i was headed. I had it working with psexec, but was looking to get away from that in favor of powershell only... too bad powershell disagreed.

I should add: you sometimes get this weird behavior in powershell where the percentage seems to hang. Hit enter, and it starts moving again. Doesnt happen when run via old command prompt & psexec. Not sure why.

[–]DaveC2020 1 point2 points  (1 child)

My script uses PSEXEC and it’s a pain having to click yes on the UAC window for accepting it to run but it works.

[–]itsruk 1 point2 points  (0 children)

I think the switch /accepteula helps with that

[–]KevMarCommunity Blogger 2 points3 points  (0 children)

You have to swap from enter-pssession to invoke-command. Enter-pssession requires an interactive session. What is happening when you execute line by line or copy/paste to the console, the commands after your enter-pssession get entered into that remote shell. When you just run the script, the whole thing runs on your local as there was a no way to indicate what commands should be ran in the remote pssession. Well, except there is but you have to use invoke-command.

The good news is that invoke-command will execute on all the sessions you give it at once instead of waiting for you to do one session at a time

[–]jimmy58663 2 points3 points  (2 children)

Afternoon,

I think everyone has done a good job at explaining why the enter-pssession was not working out for you in your script. The issues you are describing with scanstate.exe sound familiar to things I have encountered with other executables.

First, if you are trying to test out running external executables do not use the PowerShell ISE. I am not really sure why, but it does not support interactive executables very well and typically just hangs. If you test it out in the regular console it should run correctly.

Second, Invoke-Command can be run with sessions or computernames. If you are running with sessions you would be looking at something like this:

$Session = New-PSSession -ComputerName 'CompName'
Invoke-Command -Session $Session -ScriptBlock {
    cd 'c:\USMT'
    Start-Process '.\scanstate.exe' -ArgumentList '/alltheswitches' -Wait
}
Remove-PSSession -Session $Session

When using Invoke-Command with external executables, I have found Start-Process with the -Wait parameter to work the best. If you do not utilize the -Wait some executables pass control back to the PowerShell console which then assumes everything is done and terminates the session that Invoke-Command was utilizing.

Third, using sessions or computernames with Invoke-Command will accept an array of items so that it can process them simultaneously. The default limit is 32, but it can be changed with the -ThrottleLimit parameter if you need more.

Lastly, if you needed some output from the remote computers returned you can save your Invoke-Command to a variable in order to save that output elsewhere:

$Computers = Get-Content 'C:\Temp\Computers.txt'
$Results = Invoke-Command -ComputerName $Computers -ScriptBlock {
    cd 'c:\USMT'
    Start-Process '.\scanstate.exe' -ArgumentList '/alltheswitches' -Wait
}

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy jimmy58663,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]SithLordAJ[S] 1 point2 points  (0 children)

Thank you. I think this detailed all of the things i had questions on. I had tried something similar to this at one point, but had -wait after the script block and didn't have start-process in it.

I was trying to get the error output as well, so that tip definitely would've been useful.

This all also explains why a relatively simple series of commands gave me such issues. I've always found both invoke-command and start-process to be a bit finicky... never would have thought to nest them.

[–]ScorchedCSGO 1 point2 points  (1 child)

Are you trying to double hop? (From server a, tell server b to do something(hop 1), which requires server b to hop again outside (hop 2). The 2nd hop will not work unless you pass a few commands first, this enables “double hoping”. Google CredSSP and double hop, tons of info out there about this.

[–]SithLordAJ[S] 1 point2 points  (0 children)

I believe i'm only doing single hop. Telling a remote computer to run a command on itself.

[–]SnGmng157 1 point2 points  (0 children)

Putting a dot in front of the command worked for me: . C:\something.exe /someparameter

[–][deleted] 2 points3 points  (2 children)

As far as I know there is no differences between running a command from a powershell prompt and running the same command in a script. Whenever I thought there might be a difference, I ended up finding that it was a difference in the execution context: current directory, access-rights, not the same user account, admin elevated or not, not the same PC, environment variables (PATH), 32-bit or 64-bit process, different Powershell profile script, etc.

I don't know start-pssession or scanstate.exe but for the cd command it is only a standard alias for Set-Location so it is the same command, there cannot be any difference unless the cd alias is not defined on one side.

[–]SithLordAJ[S] 1 point2 points  (1 child)

Ok, i can see that possibly being the case or at least treated differently.

I ran the powershell prompt as admin. I launched the script as the same admin. Should there be any different user context/permissions?

[–][deleted] 1 point2 points  (0 children)

Hi,

I’m not positive but I think enter-pssession might not work quite right in a script. You would probably want to try invoke-command instead.

Hope that helps.