Solved

powershell remote variables

Posted on 2013-12-30
2
352 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
2 Comments
 
LVL 39

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now