Solved

powershell remote variables

Posted on 2013-12-30
2
353 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

15 Experts available now in Live!

Get 1:1 Help Now