tags:

views:

450

answers:

2

Just listened to Hansellminutes podcast. He had a talk with two Microsoft PS developers. They mentioned PS V2 remoting features.

I have some scripts based on PS v1. In terms of remoting commands or executions, I installed PS on local and a remote machines. Then I use PsExec.exe to push bat on remote to execute PS scripts. Now I am thinking to take advantage of PS V2.

To simple questions I have, to get a list of files on local, I can use the following codes:

$fs = Get-Item -Path $Path | Where { !$_.PSIsContainer ... } # more constrains in ...
if ( $fs -ne $null )
{
  # continue to work on each file in the collection
   ...
}

What is the equivalent command to get a collection of files from a remote? I prefer to get a similar collection of file objects back so that I can access to their properties.

The second question is how to exec a command on remote with external application? I tried to use WIM Process before, but I could not get WMI class working on a case of Windows 2008 server. Then I used PsExec.exe to push a bat to a remote to execute PS script. It works in the cases. However, the problem I have to install PS on the remote as well. I am going to working another remote. I'll try to avoid to install PS on the remote. Can I take PS V2 advantage to execute a command on a remote Windows? What's the new commands?

By the way, normally, I have to pass user name and pwd to a remote. I guess in PS I have to pass user/pwd as well.

+3  A: 

You can either put your code above in a script file and invoke it on a remote computer using V2 remoting like so:

PS> Invoke-Command remotePCName -file c:\myscript.ps1

You will need to be running with admin privs (elevated if UAC enabled) in order to use remoting. The command above will copy the script to the remote machine, execute it and return deserialized objects. These objects are essentially property bags. They are not "live" objects and setting properties on them like IsReadOnly will not affect the remote file. If you want to set properties then do it in your script that executes on the remote PC.

The option if you have a little bit of script is to use a scriptblock like so:

PS> Invoke-Command remotePCName { Get-Item C:\*.txt | Where {$_.IsReadOnly }

You can execute native commands (EXE) on the remote computer in either script or a scriptblock. You only need to make sure the EXE is available on the remote PC.

Regarding credentials, if you're on a domain and you have admin privs on the remote computer you won't need to pass credentials as your default credentials should work. If you need to run as a specific user then use the -Credential parameter on Invoke-Command like so:

PS> $cred = Get-Credential
PS> icm remotePCName { gci c:\windows\system32 -r *.sys } -credential $cred
Keith Hill
So I don't need to install PS on the remote computer, right?
David.Chu.ca
If you want to use PowerShell remoting then the remote computer requires PowerShell 2.0 and WinRM (both come in the Windows Management Framework for downlevel OSs) and you have to enable remoting on the remote PC using Enable-PSRemoting.
Keith Hill
Too bad. That's the issue. I have several remotes with various Windows such as Windows server 2008 and Windows XP. Some of them I cannot install PS v2 since SP3 is required and SP3 has not been approved by our network team.
David.Chu.ca
Yeah that's a bummer.
Keith Hill
A: 

Regarding your last comment, no PowerShell will use Windows integrated security so you should not have to pass any username or password unless you wanted to run it as a different user.

If you haven't yet enabled PS remoting, every time I've tried I've had to actually turn off UAC while I was enabling remoting (then I could re-enable UAC once remoting was enabled). Running Enable-PSRemoting from an elevated command prompt was not enough and the error message was not at all useful.

EDIT: I've just confirmed in a fresh Windows 7 VM that this is not an issue. It could have been a beta issue that I am no longer experiencing as I've been using beta/rc/ctp of PowerShell and Windows 7 for a long time.

Josh Einstein
Interesting. I've never had a problem enabling remoting with UAC enabled as long as I ran the command from an elevated prompt. Of course, most of my experience has been in a workgroup environment (ie home network).
Keith Hill
Same here regarding workgroup. I don't know why. I thought I would have seen more about it. Even on Win2008R2 I couldn't enable remote management from the GUI without disabling UAC first.
Josh Einstein
You don't have to disable UAC. I've got a tiny domain at home, and enabled remoting on the server and all my boxes without messing with UAC -- apart from making sure the console was elevated.I have seen some UAC things fail when the elevated account wasn't the same as the logged in account (ie: being logged in as "usera" and elevating as "admin1" causes problems for certain tasks, like BITS stuff).
Jaykul
I've just confirmed in a fresh Windows 7 VM that this is not an issue. It could have been a beta issue that I am no longer experiencing as I've been using beta/rc/ctp of PowerShell and Windows 7 for a long time.
Josh Einstein