Solved

powershell remote variables

Posted on 2013-12-30
2
357 Views
Last Modified: 2013-12-30
Can anyone tell me why the following command creates the text file but does not populate it with any data?

Invoke-Command -ComputerName server1 -ScriptBlock {Set-Variable -Name wind -Value 'C:\Windows'}
Invoke-Command -ComputerName server1 -ScriptBlock {Get-ChildItem -Path $wind -name} | Out-File -FilePath C:\test\test.txt

but the following works fine and creates the data

Invoke-Command -ComputerName server1 -ScriptBlock {Get-ChildItem -Path C:\Windows -name} | Out-File -FilePath C:\test\test.txt
0
Comment
Question by:Dead_Eyes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 200 total points
ID: 39747352
When you use Invoke-Command with the -computername parameter, it establishes a remote session and then terminates it as soon as the scriptblock is complete.  So the variable that you create in the first command no longer exists when you run the second command.  There are a few ways around this.  One is to create a session that will be used for each Invoke-command.  The session will persist until it is manually removed.
$s = New-PSSession -computername server1
Invoke-Command -session $s -ScriptBlock {Set-Variable -Name wind -Value 'C:\Windows'}
Invoke-Command -session $s -ScriptBlock {Get-ChildItem -Path $wind -name} | Out-File -FilePath C:\test\test.txt

Open in new window


Another way is to create a scriptblock which contains all the commands you wish to run, and is executed by a single Invoke-Command command.
$block = { 
  Set-Variable -Name wind -Value 'C:\Windows'
  Get-ChildItem -Path $wind -name
}
Invoke-Command -ComputerName server1 -ScriptBlock $block | Out-File -FilePath C:\test\test.txt

Open in new window


Or if the variable is from the local session and needs to be passed to the remote session.
Set-Variable -Name wind -Value 'C:\Windows'
Invoke-Command -ComputerName server1 -ScriptBlock {param ($wind) Get-ChildItem -Path $wind -name} -ArgumentList $wind | Out-File -FilePath C:\test\test.txt

Open in new window

0
 

Author Closing Comment

by:Dead_Eyes
ID: 39747525
Excellent explanation and very helpful thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question