Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

powershell remote variables

Posted on 2013-12-30
2
Medium Priority
?
362 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 41

Accepted Solution

by:
footech earned 800 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 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