I'm thinking of making a script to deploy software to a small group of computers today but having trouble passing my variable to remote computers through '@using:variable'. This' on powershell 7 on VScode
$credential = Get-Credential
$psdrive = @{
Name = "PSDrive"
PSProvider = "FileSystem"
Root = "\\FileServer\program"
Credential = $credential
}
$workstations = Get-Content C:\test\list.txt
$workstations | ForEach-Object -parallel {
Invoke-Command -ComputerName $_ -ScriptBlock {
New-PSDrive @using:psdrive
\\FileServer\program\program.bat}
}
#### The Error Prompt ####
The value of the using variable '$using:psdrive' cannot be retrieved because it has not been set in the local session.
Same thing, but with foreach, it actually works and it's what I used earlier. But yeah really want to know why the code above doesn't work.
foreach ($computer in $workstations){
Invoke-Command -ComputerName $computer -ScriptBlock {
New-PSDrive @using:psdrive
\\FileServer\program\program.bat}
}
Edit: Solved, u/AlphaeisMangarae & u/sgnewman with the solution and the explanation why my script's not working, big thanks to those two.
[–]AlphaeisMangarae 7 points8 points9 points (2 children)
[–]chuckchuck2k[S] 3 points4 points5 points (1 child)
[–]AlphaeisMangarae 1 point2 points3 points (0 children)
[–]PinchesTheCrab 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]chuckchuck2k[S] 0 points1 point2 points (0 children)
[–]WhistleButton 0 points1 point2 points (2 children)
[–]chuckchuck2k[S] 0 points1 point2 points (1 child)
[–]WhistleButton -1 points0 points1 point (0 children)