Solved

powershell remote variables

Posted on 2013-12-30
2
354 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

772 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